<?php
namespace Tests\Feature\Candidat;

use App\Models\Candidat;
use App\Models\Parrain;
use App\Models\User;
use App\Models\Ville;
use App\Enums\StatutCompteUser;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ParrainTest extends TestCase
{
    use RefreshDatabase;

    private User $candidatUser;
    private Candidat $candidat;

    protected function setUp(): void
    {
        parent::setUp();
        $ville = Ville::factory()->active()->create();
        $this->candidatUser = User::factory()->candidat()->create(['statut' => StatutCompteUser::ACTIF]);
        $this->candidat = Candidat::factory()->create([
            'user_id'  => $this->candidatUser->id,
            'ville_id' => $ville->id,
        ]);
    }

    public function test_candidat_peut_ajouter_un_parrain(): void
    {
        $response = $this->actingAs($this->candidatUser)
            ->postJson('/api/candidat/parrain', [
                'nom_complet'         => 'Jean-Paul Mballa',
                'lien_avec_candidat'  => 'parent',
                'telephone_principal' => '+237 677 000 111',
            ]);

        $response->assertStatus(201)
            ->assertJsonPath('parrain.nom_complet', 'Jean-Paul Mballa');

        $this->assertDatabaseHas('parrains', ['nom_complet' => 'Jean-Paul Mballa']);
    }

    public function test_candidat_peut_mettre_a_jour_son_parrain(): void
    {
        Parrain::factory()->create(['candidat_id' => $this->candidat->id]);

        $response = $this->actingAs($this->candidatUser)
            ->putJson('/api/candidat/parrain', [
                'nom_complet'         => 'Nouveau Parrain',
                'lien_avec_candidat'  => 'voisin',
                'telephone_principal' => '+237 699 000 222',
            ]);

        $response->assertStatus(200)
            ->assertJsonPath('parrain.nom_complet', 'Nouveau Parrain');
    }

    public function test_candidat_ne_peut_avoir_quun_seul_parrain(): void
    {
        Parrain::factory()->create(['candidat_id' => $this->candidat->id]);

        $this->actingAs($this->candidatUser)
            ->postJson('/api/candidat/parrain', [
                'nom_complet'         => 'Deuxième parrain',
                'lien_avec_candidat'  => 'ami',
                'telephone_principal' => '+237 677 000 333',
            ]);

        $this->assertEquals(1, Parrain::where('candidat_id', $this->candidat->id)->count());
    }

    public function test_candidat_peut_voir_son_parrain(): void
    {
        Parrain::factory()->create(['candidat_id' => $this->candidat->id]);

        $response = $this->actingAs($this->candidatUser)
            ->getJson('/api/candidat/parrain');

        $response->assertStatus(200)
            ->assertJsonStructure(['parrain' => ['id', 'nom_complet', 'lien_avec_candidat']]);
    }

    public function test_parrain_retourne_null_si_absent(): void
    {
        $response = $this->actingAs($this->candidatUser)
            ->getJson('/api/candidat/parrain');

        $response->assertStatus(200)
            ->assertJsonPath('parrain', null);
    }

    public function test_lien_avec_candidat_invalide_retourne_erreur(): void
    {
        $response = $this->actingAs($this->candidatUser)
            ->postJson('/api/candidat/parrain', [
                'nom_complet'         => 'Test',
                'lien_avec_candidat'  => 'inconnu',
                'telephone_principal' => '+237 677 000 444',
            ]);

        $response->assertStatus(422);
    }
}
