<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class InviteControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_handle_sets_cookie()
    {
        $inviter = User::factory()->create();

        $response = $this->get(route('invite.link', $inviter->id));

        $response->assertRedirect(route('home'));
        $response->assertCookie('inviter_id', $inviter->id);
    }

    public function test_handle_ignores_invalid_inviter()
    {
        $response = $this->get(route('invite.link', 99999));

        $response->assertRedirect(route('home'));
        $response->assertCookieMissing('inviter_id');
    }

    public function test_leaderboard_displays_inviters()
    {
        $inviter = User::factory()->create();
        $user = User::factory()->create();

        User::factory()->count(2)->create(['inviter_id' => $inviter->id]);

        $response = $this->actingAs($user)->get(route('invite.leaderboard'));

        $response->assertStatus(200);
        $response->assertViewIs('invite.leaderboard');
    }
}
