<?php

declare(strict_types=1);

namespace Tests\Feature;

use App\Enums\PropertyProposalStatus;
use App\Enums\PropertyRequestStatus;
use App\Enums\UserAccountStatus;
use App\Models\City;
use App\Models\PropertyProposal;
use App\Models\PropertyProposalPhoto;
use App\Models\PropertyRequest;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class PropertyProposalIndexTest extends TestCase
{
    use RefreshDatabase;

    public function test_requester_lists_proposals_with_public_image_urls(): void
    {
        Storage::fake('public');

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

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

        $proposal = PropertyProposal::query()->create([
            'request_id' => $request->id,
            'proposed_by_user_id' => $proposer->id,
            'title' => 'Villa avec piscine',
            'property_type' => 'villa',
            'city_id' => $city->id,
            'price' => 850_000,
            'surface' => 200,
            'description' => 'Belle villa avec vue mer et piscine chauffée.',
            'status' => PropertyProposalStatus::Pending,
        ]);

        $path = 'proposals/test-photo.jpg';
        Storage::disk('public')->put($path, 'fake-image');
        PropertyProposalPhoto::query()->create([
            'property_proposal_id' => $proposal->id,
            'file_path' => $path,
            'sort_order' => 0,
        ]);

        Sanctum::actingAs($owner);

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

        $response->assertOk()
            ->assertJsonCount(1, 'data')
            ->assertJsonPath('data.0.id', $proposal->id)
            ->assertJsonCount(1, 'data.0.images')
            ->assertJsonPath('data.0.images.0', Storage::disk('public')->url($path));
    }

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

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

        Sanctum::actingAs($other);

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