<?php

namespace App\Jobs;

use App\Models\Town;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class SyncTowns implements ShouldQueue
{
    use Queueable;

    private const API_URL = 'https://services-ap1.arcgis.com/ypkPEy1AmwPKGNNv/arcgis/rest/services/Town_Point/FeatureServer/0/query';

    private const PAGE_SIZE = 1000;

    public int $timeout = 300;

    public function handle(): void
    {
        $offset = 0;
        $synced = 0;

        do {
            $response = Http::timeout(30)->get(self::API_URL, [
                'outFields' => 'town_pid,town_name,state,population,town_class,date_retired',
                'where' => '1=1',
                'f' => 'json',
                'resultOffset' => $offset,
                'resultRecordCount' => self::PAGE_SIZE,
                'orderByFields' => 'objectid ASC',
            ]);

            if ($response->failed()) {
                Log::error('SyncTowns: API request failed', ['offset' => $offset, 'status' => $response->status()]);
                break;
            }

            $features = $response->json('features', []);

            if (empty($features)) {
                break;
            }

            $records = collect($features)->map(fn (array $feature) => [
                'town_pid' => $feature['attributes']['town_pid'],
                'town_name' => $feature['attributes']['town_name'],
                'state' => $feature['attributes']['state'],
                'population' => $feature['attributes']['population'],
                'town_class' => $feature['attributes']['town_class'],
                'date_retired' => isset($feature['attributes']['date_retired'])
                    ? Carbon::createFromTimestampMs($feature['attributes']['date_retired'])
                    : null,
                'updated_at' => now(),
                'created_at' => now(),
            ])->toArray();

            Town::upsert($records, uniqueBy: ['town_pid'], update: [
                'town_name',
                'state',
                'population',
                'town_class',
                'date_retired',
                'updated_at',
            ]);

            $synced += count($features);
            $offset += self::PAGE_SIZE;
        } while (count($features) === self::PAGE_SIZE);

        Log::info("SyncTowns: synced {$synced} towns.");
    }
}
