<?php

namespace App\Campaigns\Targeting;

use App\Models\Location;
use App\Models\PlatformLocation;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder;

class LocationCatalog
{
    /** @return list<string> */
    public function suggestedNames(string $platform, string $kind, string $countryCode, string $term): array
    {
        return Location::query()
            ->where('type', $kind)
            ->where('country_code', $countryCode)
            ->where('source', '!=', $platform)
            ->where('name', 'like', '%'.$term.'%')
            ->distinct()
            ->orderBy('name')
            ->limit(20)
            ->pluck('name')
            ->all();
    }

    /**
     * @return list<array{id: string, label: string}>
     */
    public function search(string $platform, string $kind, string $countryCode, string $term): array
    {
        return PlatformLocation::query()
            ->select('platform_locations.*')
            ->join('locations', 'locations.id', '=', 'platform_locations.location_id')
            ->where('platform_locations.platform', $platform)
            ->whereIn('platform_locations.status', ['verified', 'unverified'])
            ->whereNotNull('platform_locations.provider_name')
            ->where('locations.type', $kind)
            ->where('locations.country_code', $countryCode)
            ->where(fn (EloquentBuilder $query): EloquentBuilder => $query
                ->where('locations.name', 'like', '%'.$term.'%')
                ->orWhere('platform_locations.provider_name', 'like', '%'.$term.'%'))
            ->orderByDesc('platform_locations.verified_at')
            ->orderBy('locations.name')
            ->limit(20)
            ->get()
            ->map(fn (PlatformLocation $mapping): array => [
                'id' => (string) $mapping->getKey(),
                'label' => (string) $mapping->provider_name,
            ])
            ->all();
    }

    /**
     * @return list<array{id: string, label: string}>
     */
    public function choices(string $platform, string $kind, string $countryCode): array
    {
        return PlatformLocation::query()
            ->select('platform_locations.*')
            ->selectSub(fn (Builder $query): Builder => $query->from('targeting_locations')
                ->selectRaw('count(*)')
                ->whereColumn('targeting_locations.platform_location_id', 'platform_locations.id')
                ->where('targeting_locations.operation', 'include'), 'usage_count')
            ->join('locations', 'locations.id', '=', 'platform_locations.location_id')
            ->where('platform_locations.platform', $platform)
            ->whereIn('platform_locations.status', ['verified', 'unverified'])
            ->whereNotNull('platform_locations.provider_name')
            ->where('locations.type', $kind)
            ->where('locations.country_code', $countryCode)
            ->orderByDesc('usage_count')
            ->orderByDesc('platform_locations.catalog_priority')
            ->orderByDesc('platform_locations.verified_at')
            ->orderBy('locations.name')
            ->orderBy('platform_locations.provider_name')
            ->limit($kind === 'city' ? 100 : 500)
            ->get()
            ->map(fn (PlatformLocation $mapping): array => [
                'id' => (string) $mapping->getKey(),
                'label' => (string) $mapping->provider_name,
            ])
            ->all();
    }
}
