<?php

declare(strict_types=1);

namespace Tests\Feature;

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

class PropertyRequestShowTest extends TestCase
{
    use RefreshDatabase;

    public function test_guest_cannot_view_property_request(): void
    {
        $this->getJson('/api/property-requests/1')->assertUnauthorized();
    }

    public function test_returns_not_found_for_missing_request(): void
    {
        $user = User::factory()->create(['status' => UserAccountStatus::Approved]);
        Sanctum::actingAs($user);

        $this->getJson('/api/property-requests/99999')->assertNotFound();
    }

    public function test_returns_not_found_for_inactive_request(): void
    {
        $user = User::factory()->create(['status' => UserAccountStatus::Approved]);
        $owner = User::factory()->create(['status' => UserAccountStatus::Approved]);
        $city = City::query()->create(['name' => 'Djerba', 'country' => 'TN']);

        $request = PropertyRequest::query()->create([
            'reference' => 'REQ-TEST-0002',
            'created_by_user_id' => $owner->id,
            'transaction_type' => 'achat',
            'property_type' => 'villa',
            'city_id' => $city->id,
            'zone_id' => null,
            'title' => 'Client cherche villa',
            'budget_min' => 800_000,
            'budget_max' => 900_000,
            'surface_min' => 150,
            'rooms' => 6,
            'bedrooms' => 4,
            'urgency_level' => 'normal',
            'description' => 'Description suffisamment longue pour la demande.',
            'status' => PropertyRequestStatus::Expired,
            'expires_at' => now()->addMonth(),
        ]);

        Sanctum::actingAs($user);

        $this->getJson("/api/property-requests/{$request->id}")->assertNotFound();
    }

    public function test_approved_user_can_view_active_request(): void
    {
        $user = User::factory()->create(['status' => UserAccountStatus::Approved]);
        $owner = User::factory()->create(['status' => UserAccountStatus::Approved]);
        $city = City::query()->create(['name' => 'Djerba', 'country' => 'TN']);

        $request = PropertyRequest::query()->create([
            'reference' => 'REQ-TEST-0003',
            'created_by_user_id' => $owner->id,
            'transaction_type' => 'achat',
            'property_type' => 'villa',
            'city_id' => $city->id,
            'zone_id' => null,
            'title' => 'Client cherche villa',
            'budget_min' => 800_000,
            'budget_max' => 900_000,
            'surface_min' => 150,
            'rooms' => 6,
            'bedrooms' => 4,
            'urgency_level' => 'normal',
            'description' => 'Description suffisamment longue pour la demande.',
            'status' => PropertyRequestStatus::Active,
            'expires_at' => now()->addMonth(),
        ]);

        Sanctum::actingAs($user);

        $this->getJson("/api/property-requests/{$request->id}")
            ->assertOk()
            ->assertJsonPath('data.id', $request->id)
            ->assertJsonPath('data.created_by_user_id', $owner->id)
            ->assertJsonPath('data.title', 'Client cherche villa');
    }

    public function test_owner_can_view_own_inactive_request(): void
    {
        $owner = User::factory()->create(['status' => UserAccountStatus::Approved]);
        $city = City::query()->create(['name' => 'Djerba', 'country' => 'TN']);

        $request = PropertyRequest::query()->create([
            'reference' => 'REQ-TEST-0004',
            'created_by_user_id' => $owner->id,
            'transaction_type' => 'achat',
            'property_type' => 'villa',
            'city_id' => $city->id,
            'title' => 'Ma demande archivée',
            'budget_max' => 900_000,
            'urgency_level' => 'normal',
            'description' => 'Description suffisamment longue pour la demande.',
            'status' => PropertyRequestStatus::Fulfilled,
            'expires_at' => now()->subDay(),
        ]);

        Sanctum::actingAs($owner);

        $this->getJson("/api/property-requests/{$request->id}")
            ->assertOk()
            ->assertJsonPath('data.id', $request->id);
    }
}
