<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\Events\MigrationsEnded;
use App\Jobs\GeocodeEventLocations;

class AppServiceProvider extends ServiceProvider
{
    /**
     * register any application services
     */
    public function register(): void
    {
        //
    }

    /**
     * bootstrap any application services
     */
    public function boot(): void
    {
        // Calendar form
        Blade::component('calendars._form', 'calendar-form');

        if (app()->runningInConsole()) {
            Event::listen(MigrationsEnded::class, function () {
                if (!config('services.geocoding.after_migrate')) {
                    return;
                }

                if (!config('services.geocoding.arcgis.api_key')) {
                    Log::warning('Skipping geocode after migrate: missing ArcGIS API key');
                    return;
                }

                try {
                    GeocodeEventLocations::runNow();
                } catch (\Throwable $e) {
                    Log::warning('Geocode after migrate failed', [
                        'error' => $e->getMessage(),
                    ]);
                }
            });
        }
    }
}
