<?php

namespace Tests\Feature;

use App\Campaigns\Targeting\LocationCatalogImporter;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\SeedsVerifiedCountries;
use Tests\TestCase;

class LocationSuggestionTest extends TestCase
{
    use RefreshDatabase;
    use SeedsVerifiedCountries;

    public function test_typeahead_filters_catalog_by_platform_kind_country_and_name(): void
    {
        $this->seedVerifiedCountries();
        $this->actingAs(User::factory()->create());
        app(LocationCatalogImporter::class)->store([
            ['platform' => 'meta', 'external_id' => '2490299', 'kind' => 'city', 'name' => 'New York',
                'label' => 'New York, New York, US', 'country_code' => 'US', 'parent_external_id' => null],
            ['platform' => 'google', 'external_id' => 'geoTargetConstants/1023191', 'kind' => 'city', 'name' => 'New York',
                'label' => 'New York, New York, United States', 'country_code' => 'US', 'parent_external_id' => null],
            ['platform' => 'meta', 'external_id' => '3847', 'kind' => 'region', 'name' => 'New York',
                'label' => 'New York State, US', 'country_code' => 'US', 'parent_external_id' => null],
            ['platform' => 'meta', 'external_id' => '2410000', 'kind' => 'city', 'name' => 'York',
                'label' => 'York, England, GB', 'country_code' => 'GB', 'parent_external_id' => null],
        ]);

        $this->getJson(route('chat.location-suggestions', [
            'platform' => 'meta', 'kind' => 'city', 'country' => 'US', 'q' => 'New',
        ]))->assertOk()->assertJsonCount(1, 'places')->assertJsonPath('places.0.label', 'New York, New York, US');
    }

    public function test_typeahead_rejects_invalid_search_and_requires_authentication(): void
    {
        $this->getJson(route('chat.location-suggestions', [
            'platform' => 'meta', 'kind' => 'city', 'country' => 'US', 'q' => 'New',
        ]))->assertUnauthorized();

        $this->actingAs(User::factory()->create());
        $this->getJson(route('chat.location-suggestions', [
            'platform' => 'meta', 'kind' => 'city', 'country' => 'US', 'q' => '%',
        ]))->assertUnprocessable();
        $this->getJson(route('chat.location-suggestions', [
            'platform' => 'meta', 'kind' => 'city', 'country' => 'US', 'q' => '   ',
        ]))->assertUnprocessable();
    }

    public function test_typeahead_can_find_city_outside_initial_checkbox_limit(): void
    {
        $this->seedVerifiedCountries();
        $this->actingAs(User::factory()->create());
        $candidates = [];
        for ($index = 0; $index < 101; $index++) {
            $candidates[] = [
                'platform' => 'meta', 'external_id' => (string) (2000000 + $index), 'kind' => 'city',
                'name' => sprintf('Alpha%03d', $index), 'label' => sprintf('Alpha%03d, US', $index),
                'country_code' => 'US', 'parent_external_id' => null,
            ];
        }
        app(LocationCatalogImporter::class)->store($candidates);

        $this->getJson(route('chat.location-suggestions', [
            'platform' => 'meta', 'kind' => 'city', 'country' => 'US', 'q' => 'Alpha100',
        ]))->assertOk()->assertJsonCount(1, 'places')->assertJsonPath('places.0.label', 'Alpha100, US');
    }

    public function test_typeahead_suggests_known_city_name_when_meta_id_is_not_imported(): void
    {
        $this->seedVerifiedCountries();
        $this->actingAs(User::factory()->create());
        app(LocationCatalogImporter::class)->store([[
            'platform' => 'google', 'external_id' => 'geoTargetConstants/1014565', 'kind' => 'city',
            'name' => 'Denver', 'label' => 'Denver, Colorado, United States',
            'country_code' => 'US', 'parent_external_id' => null,
        ]]);

        $this->getJson(route('chat.location-suggestions', [
            'platform' => 'meta', 'kind' => 'city', 'country' => 'US', 'q' => 'Den',
        ]))->assertOk()->assertJsonCount(0, 'places')->assertJsonPath('names.0', 'Denver');
    }
}
