<?php

namespace Tests\Feature;

use App\Models\Listing;
use App\Models\Partner;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Tests\TestCase;

class ListingTest extends TestCase
{
    use RefreshDatabase;

    public function test_index_filters_published_listings(): void
    {
        $partner = $this->partner();
        $matching = $this->listing($partner, [
            'title' => 'Lagos Lagoon Villa',
            'slug' => 'lagos-lagoon-villa',
            'city' => 'Lagos',
            'max_guests' => 6,
            'status' => 'PUBLISHED',
        ]);
        $this->listing($partner, [
            'title' => 'Small Lagos Flat',
            'slug' => 'small-lagos-flat',
            'city' => 'Lagos',
            'max_guests' => 2,
            'status' => 'PUBLISHED',
        ]);
        $this->listing($partner, [
            'title' => 'Hidden Lagos Home',
            'slug' => 'hidden-lagos-home',
            'city' => 'Lagos',
            'max_guests' => 8,
            'status' => 'DRAFT',
        ]);

        $this->getJson('/api/v1/listings?q=lagos&minGuests=4')
            ->assertOk()
            ->assertJsonPath('total', 1)
            ->assertJsonPath('data.0.id', $matching->id);
    }

    public function test_featured_and_detail_endpoints_return_published_listing_data(): void
    {
        $partner = $this->partner();
        $listing = $this->listing($partner, [
            'slug' => 'featured-villa',
            'status' => 'PUBLISHED',
            'is_featured' => true,
        ]);

        $this->getJson('/api/v1/listings/featured')
            ->assertOk()
            ->assertJsonPath('0.id', $listing->id);

        $this->getJson('/api/v1/listings/'.$listing->id)
            ->assertOk()
            ->assertJsonPath('id', $listing->id);

        $this->assertDatabaseHas('listings', ['id' => $listing->id, 'views_count' => 1]);
    }

    private function partner(): Partner
    {
        $user = User::factory()->create(['role' => 'PARTNER']);

        return Partner::query()->create([
            'user_id' => $user->id,
            'business_name' => 'Test Stays',
            'joined_year' => 2026,
        ]);
    }

    private function listing(Partner $partner, array $attributes = []): Listing
    {
        return Listing::query()->create(array_merge([
            'partner_id' => $partner->id,
            'title' => 'Test Villa',
            'slug' => 'test-villa-'.Str::uuid(),
            'description' => 'A listing used by the feature test.',
            'category' => 'VILLA',
            'address' => '1 Test Street',
            'city' => 'Abuja',
            'country' => 'Nigeria',
            'latitude' => 9.0765,
            'longitude' => 7.3986,
            'base_price_usd' => 100,
            'base_price_eur' => 90,
            'base_price_gbp' => 80,
            'base_price_ngn' => 150000,
            'max_guests' => 4,
            'bedrooms' => 2,
            'beds' => 2,
            'bathrooms' => 2,
            'status' => 'DRAFT',
        ], $attributes));
    }
}