<?php

namespace Tests\Feature;

use App\Models\Kategori;
use App\Models\Wisata;
use Database\Seeders\DestinationRatingSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class DestinationRatingSyncTest extends TestCase
{
    use RefreshDatabase;

    public function test_destination_rating_seeder_updates_latest_ratings(): void
    {
        $kategori = Kategori::query()->create([
            'nama' => 'Wisata Alam',
            'slug' => 'wisata-alam',
        ]);

        Wisata::query()->create([
            'kategori_id' => $kategori->id,
            'title' => 'Ranu Pani',
            'slug' => 'ranu-pani',
            'location' => 'Senduro, Lumajang',
            'rating' => 4.0,
        ]);

        Wisata::query()->create([
            'kategori_id' => $kategori->id,
            'title' => 'Air Terjun Tumpak Sewu',
            'slug' => 'air-terjun-tumpak-sewu',
            'location' => 'Pronojiwo, Lumajang',
            'rating' => 4.0,
        ]);

        $this->seed(DestinationRatingSeeder::class);

        $this->assertDatabaseHas('wisatas', [
            'slug' => 'ranu-pani',
            'rating' => 4.6,
        ]);

        $this->assertDatabaseHas('wisatas', [
            'slug' => 'air-terjun-tumpak-sewu',
            'rating' => 4.8,
        ]);
    }

    public function test_destinations_api_returns_numeric_rating_and_rating_label(): void
    {
        $kategori = Kategori::query()->create([
            'nama' => 'Danau',
            'slug' => 'danau',
        ]);

        Wisata::query()->create([
            'kategori_id' => $kategori->id,
            'title' => 'Ranu Kumbolo',
            'slug' => 'ranu-kumbolo',
            'location' => 'Lumajang, Jawa Timur',
            'rating' => 4.8,
        ]);

        $this->getJson('/api/destinations')
            ->assertOk()
            ->assertJsonPath('data.0.rating', 4.8)
            ->assertJsonPath('data.0.rating_label', '4.8 ⭐');
    }
}
