<?php
namespace Tests\Unit\Services;

use App\Models\Candidat;
use App\Models\Ville;
use App\Models\User;
use App\Enums\StatutCandidat;
use App\Services\CandidatService;
use App\Services\ReferenceGeneratorService;
use App\Services\StatusLogService;
use App\Services\NotificationService;
use App\Exceptions\TransitionStatutInvalideException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CandidatServiceTest extends TestCase
{
    use RefreshDatabase;

    private CandidatService $service;

    protected function setUp(): void
    {
        parent::setUp();
        $this->service = new CandidatService(
            new ReferenceGeneratorService(),
            new StatusLogService(),
            new NotificationService()
        );
    }

    public function test_update_profil_ne_modifie_pas_le_statut(): void
    {
        $ville = Ville::factory()->active()->create();
        $user  = User::factory()->candidat()->create();
        $candidat = Candidat::factory()->create([
            'user_id'  => $user->id,
            'ville_id' => $ville->id,
            'statut'   => StatutCandidat::SOUMIS->value,
        ]);

        $this->service->updateProfil($candidat, [
            'nom'    => 'Nouveau nom',
            'statut' => StatutCandidat::DISPONIBLE->value, // Tentative de modification
        ]);

        $this->assertDatabaseHas('candidats', [
            'id'     => $candidat->id,
            'nom'    => 'Nouveau nom',
            'statut' => StatutCandidat::SOUMIS->value, // Statut inchangé
        ]);
    }

    public function test_transition_statut_valide_reussit(): void
    {
        $ville = Ville::factory()->active()->create();
        $user  = User::factory()->candidat()->create();
        $candidat = Candidat::factory()->create([
            'user_id'  => $user->id,
            'ville_id' => $ville->id,
            'statut'   => StatutCandidat::SOUMIS->value,
        ]);

        // SOUMIS → ATTENTE_PAIEMENT est une transition valide
        $this->assertTrue($candidat->peutTransitionnerVers(StatutCandidat::ATTENTE_PAIEMENT));
    }

    public function test_transition_statut_invalide_echoue(): void
    {
        $ville = Ville::factory()->active()->create();
        $user  = User::factory()->candidat()->create();
        $candidat = Candidat::factory()->create([
            'user_id'  => $user->id,
            'ville_id' => $ville->id,
            'statut'   => StatutCandidat::SOUMIS->value,
        ]);

        // SOUMIS → DISPONIBLE est une transition invalide
        $this->assertFalse($candidat->peutTransitionnerVers(StatutCandidat::DISPONIBLE));
    }

    public function test_dossier_incomplet_si_pieces_manquantes(): void
    {
        $ville = Ville::factory()->active()->create();
        $user  = User::factory()->candidat()->create();
        $candidat = Candidat::factory()->create([
            'user_id'  => $user->id,
            'ville_id' => $ville->id,
        ]);

        $this->assertFalse($candidat->dossierEstComplet());
    }
}
