<?php

declare(strict_types=1);

namespace Tests\Feature;

use App\Enums\PropertyRequestStatus;
use App\Models\City;
use App\Models\PropertyRequest;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CleanupExpiredRequestsCommandTest extends TestCase
{
    use RefreshDatabase;

    public function test_command_marks_active_requests_past_expires_at_as_expired(): void
    {
        $user = User::factory()->create();
        $city = City::query()->create(['name' => 'Testville', 'country' => 'TN']);

        $expired = PropertyRequest::query()->create([
            'reference' => 'REQ-EXP-1',
            'created_by_user_id' => $user->id,
            'transaction_type' => 'achat',
            'property_type' => 'villa',
            'city_id' => $city->id,
            'zone_id' => null,
            'title' => 'Demande expirée',
            'budget_min' => 1,
            'budget_max' => 2,
            'surface_min' => 10,
            'rooms' => 2,
            'bedrooms' => 1,
            'urgency_level' => 'normal',
            'description' => 'Description suffisamment longue pour la demande.',
            'status' => PropertyRequestStatus::Active,
            'expires_at' => now()->subDay(),
        ]);

        $stillActive = PropertyRequest::query()->create([
            'reference' => 'REQ-EXP-2',
            'created_by_user_id' => $user->id,
            'transaction_type' => 'achat',
            'property_type' => 'villa',
            'city_id' => $city->id,
            'zone_id' => null,
            'title' => 'Demande valide',
            'budget_min' => 1,
            'budget_max' => 2,
            'surface_min' => 10,
            'rooms' => 2,
            'bedrooms' => 1,
            'urgency_level' => 'normal',
            'description' => 'Description suffisamment longue pour la demande.',
            'status' => PropertyRequestStatus::Active,
            'expires_at' => now()->addWeek(),
        ]);

        $this->artisan('app:cleanup-expired-requests')->assertSuccessful();

        $expired->refresh();
        $stillActive->refresh();

        $this->assertSame(PropertyRequestStatus::Expired, $expired->status);
        $this->assertSame(PropertyRequestStatus::Active, $stillActive->status);
    }
}
