<?php

namespace Tests\Feature\Auth;

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

class GuardSeparationTest extends TestCase
{
    use RefreshDatabase;

    public function test_admin_guard_does_not_make_homepage_treat_admin_as_frontend_user(): void
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $response = $this
            ->actingAs($admin, 'admin')
            ->get(route('landing'));

        $response
            ->assertOk()
            ->assertSee(route('auth.google.redirect', ['popup' => 1]), false)
            ->assertDontSee('href="'.route('jobs.index').'"', false);

        $this->assertAuthenticatedAs($admin, 'admin');
        $this->assertGuest('web');
    }

    public function test_frontend_logout_does_not_log_out_admin_guard(): void
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);
        $candidate = User::factory()->create([
            'role' => 'user',
        ]);

        $response = $this
            ->actingAs($admin, 'admin')
            ->actingAs($candidate, 'web')
            ->post(route('logout'));

        $response->assertRedirect(route('landing'));

        $this->assertGuest('web');
        $this->assertAuthenticatedAs($admin, 'admin');
    }

    public function test_admin_logout_does_not_log_out_frontend_guard(): void
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);
        $candidate = User::factory()->create([
            'role' => 'user',
        ]);

        $response = $this
            ->actingAs($candidate, 'web')
            ->actingAs($admin, 'admin')
            ->post(route('filament.admin.auth.logout'));

        $response->assertRedirect();

        $this->assertGuest('admin');
        $this->assertAuthenticatedAs($candidate, 'web');
    }
}
