<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
use App\Models\AdminUser;
use App\Models\Club;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

class AdminTest extends TestCase
{
    use DatabaseTransactions;

    public function test_guest_cannot_access_admin_dashboard()
    {
        $response = $this->get('/admin');
        
        $response->assertStatus(403);
    }

    public function test_player_cannot_access_admin_dashboard()
    {
        // Simular que un jugador está logueado
        $this->withSession(['user_logged_in' => true, 'user_tipo' => 'jugador']);

        $response = $this->get('/admin');
        
        // El middleware/controlador debería redirigirlo por no ser admin
        $response->assertStatus(403);
    }

    public function test_superadmin_can_access_admin_dashboard()
    {
        AdminUser::create([
            'username' => 'supertest',
            'password' => Hash::make('123456'),
            'role' => 1
        ]);

        $this->withSession([
            'admin_logged_in' => true, 
            'admin_role' => 1,
            'admin_username' => 'supertest'
        ]);

        $response = $this->get('/admin');
        
        $response->assertStatus(200);
        $response->assertSee('Resumen General');
    }

    public function test_superadmin_can_create_a_club()
    {
        Storage::fake('public');

        $this->withSession([
            'admin_logged_in' => true, 
            'admin_role' => 1,
            'admin_username' => 'supertest'
        ]);

        // Evitamos enviar una imagen real para no depender del File System
        $response = $this->post('/admin/clubes', [
            'id_club'    => 999,
            'nombre'     => 'Club Atlético Test',
            'localidad'  => 'Testville',
            'direccion'  => 'Calle Falsa 123',
            'activo'     => 1
        ]);

        $response->assertRedirect(route('admin.clubes.index'));
        $this->assertDatabaseHas('clubes', [
            'nombre' => 'Club Atlético Test'
        ]);
    }
}
