<?php

namespace Tests\Feature;

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

class AuthenticationTest extends TestCase
{
    use RefreshDatabase;

    public function test_user_can_login_with_correct_credentials(): void
    {
        $user = User::factory()->create([
            'mobile' => '09121234567',
            'password' => Hash::make('Password@123'),
            'role' => 'athlete',
        ]);

        $response = $this->post('/login', [
            'mobile' => '09121234567',
            'password' => 'Password@123',
            'captcha' => 'test', // در محیط تست، اعتبارسنجی CAPTCHA باید در .env.testing غیرفعال شود
        ]);

        $this->assertAuthenticatedAs($user);
    }

    public function test_login_fails_with_wrong_password(): void
    {
        User::factory()->create([
            'mobile' => '09121234567',
            'password' => Hash::make('Password@123'),
        ]);

        $response = $this->post('/login', [
            'mobile' => '09121234567',
            'password' => 'wrong-password',
            'captcha' => 'test',
        ]);

        $response->assertSessionHasErrors('mobile');
        $this->assertGuest();
    }

    public function test_inactive_account_cannot_stay_logged_in(): void
    {
        $user = User::factory()->create(['is_active' => false]);

        $response = $this->actingAs($user)->get('/'); // یا هر route محافظت‌شده با active.account

        $this->assertGuest();
    }
}
