<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
use App\Models\AdminUser;
use App\Models\Jugador;
use App\Models\Aficionado;
use Illuminate\Support\Facades\Hash;

class AuthTest extends TestCase
{
    use DatabaseTransactions;

    const TURNSTILE_BYPASS = '1x00000000000000000000AA';

    public function test_admin_login_success()
    {
        AdminUser::create([
            'username' => 'superadmin',
            'password' => Hash::make('password123'),
            'role'     => 1
        ]);

        $response = $this->post('/login', [
            'tipo'                  => 'admin',
            'username'              => 'superadmin',
            'password'              => 'password123',
            'cf-turnstile-response' => self::TURNSTILE_BYPASS
        ]);

        $response->assertRedirect('/');
        $response->assertSessionHas('admin_logged_in', true);
        $response->assertSessionHas('admin_username', 'superadmin');
        $response->assertSessionHas('admin_role', 1);
    }

    public function test_admin_login_failure()
    {
        AdminUser::create([
            'username' => 'superadmin',
            'password' => Hash::make('password123'),
            'role'     => 1
        ]);

        $response = $this->post('/login', [
            'tipo'                  => 'admin',
            'username'              => 'superadmin',
            'password'              => 'wrongpassword',
            'cf-turnstile-response' => self::TURNSTILE_BYPASS
        ]);

        $response->assertSessionHas('login_error', 'Usuario o contraseña incorrectos');
    }

    public function test_jugador_login_success()
    {
        Jugador::create([
            'id_jugador'     => 'TESTJ01',
            'nombre'         => 'Emanuel',
            'apellido'       => 'Ginobili',
            'documento'      => '25123456',
            'fecha_nacimiento' => '1977-07-28',
            'activo'         => true,
            'password'       => Hash::make('secreta123')
        ]);

        $response = $this->post('/login', [
            'tipo'                  => 'player',
            'dni'                   => '25123456',
            'password'              => 'secreta123',
            'cf-turnstile-response' => self::TURNSTILE_BYPASS
        ]);

        $response->assertRedirect('/');
        $response->assertSessionHas('user_logged_in', true);
        $response->assertSessionHas('user_tipo', 'jugador');
        $response->assertSessionHas('user_documento', '25123456');
    }

    public function test_aficionado_login_success()
    {
        Aficionado::create([
            'nombre'         => 'Juan',
            'apellido'       => 'Pérez',
            'dni'            => '12345678',
            'email'          => 'juan@example.com',
            'password'       => Hash::make('fan123')
        ]);

        $response = $this->post('/login', [
            'tipo'                  => 'player',
            'dni'                   => '12345678',
            'password'              => 'fan123',
            'cf-turnstile-response' => self::TURNSTILE_BYPASS
        ]);

        $response->assertRedirect('/');
        $response->assertSessionHas('user_logged_in', true);
        $response->assertSessionHas('user_tipo', 'aficionado');
    }

    public function test_logout_clears_session()
    {
        // Simulamos admin logged in
        $this->withSession(['admin_logged_in' => true, 'admin_username' => 'testadmin']);
        
        $response = $this->post('/logout');
        
        $response->assertSessionMissing('admin_logged_in');
        $response->assertSessionMissing('admin_username');
        $response->assertRedirectContains('logout_msg=');
    }
}
