<?php
namespace Tests\Feature\Commands;

use App\Models\Mission;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase;

class ArchiverMissionsTermineesTest extends TestCase
{
    use RefreshDatabase;

    public function test_command_archive_missions_terminees_anciennes(): void
    {
        $mission = Mission::factory()->terminee()->create([
            'updated_at' => Carbon::now()->subDays(91),
        ]);

        $this->artisan('prodom:archiver-missions')->assertExitCode(0);

        $this->assertSoftDeleted('missions', ['id' => $mission->id]);
    }

    public function test_command_ne_touche_pas_missions_recentes(): void
    {
        $mission = Mission::factory()->terminee()->create([
            'updated_at' => Carbon::now()->subDays(30),
        ]);

        $this->artisan('prodom:archiver-missions')->assertExitCode(0);

        $this->assertDatabaseHas('missions', [
            'id'         => $mission->id,
            'deleted_at' => null,
        ]);
    }

    public function test_command_ne_touche_pas_missions_actives(): void
    {
        $mission = Mission::factory()->active()->create([
            'updated_at' => Carbon::now()->subDays(200),
        ]);

        $this->artisan('prodom:archiver-missions')->assertExitCode(0);

        $this->assertDatabaseHas('missions', [
            'id'         => $mission->id,
            'deleted_at' => null,
        ]);
    }

    public function test_dry_run_narchive_rien(): void
    {
        $mission = Mission::factory()->terminee()->create([
            'updated_at' => Carbon::now()->subDays(91),
        ]);

        $this->artisan('prodom:archiver-missions', ['--dry-run' => true])
            ->expectsOutputToContain('DRY-RUN')
            ->assertExitCode(0);

        $this->assertDatabaseHas('missions', [
            'id'         => $mission->id,
            'deleted_at' => null,
        ]);
    }

    public function test_command_avec_option_jours_personnalisee(): void
    {
        // Mission terminée il y a 31 jours → doit être archivée avec --jours=30
        $missionArchivee = Mission::factory()->terminee()->create([
            'updated_at' => Carbon::now()->subDays(31),
        ]);

        // Mission terminée il y a 29 jours → ne doit pas être touchée
        $missionConservee = Mission::factory()->terminee()->create([
            'updated_at' => Carbon::now()->subDays(29),
        ]);

        $this->artisan('prodom:archiver-missions', ['--jours' => 30])->assertExitCode(0);

        $this->assertSoftDeleted('missions', ['id' => $missionArchivee->id]);
        $this->assertDatabaseHas('missions', [
            'id'         => $missionConservee->id,
            'deleted_at' => null,
        ]);
    }
}
