<?php

namespace App\Providers;

use App\Services\QrCode\Contracts\GeoLocator;
use App\Services\QrCode\HttpGeoLocator;
use App\Services\QrCode\NullGeoLocator;
use Carbon\CarbonImmutable;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Rules\Password;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        // NullGeoLocator in testing so the suite never makes a real network
        // call; HttpGeoLocator (ip-api.com) everywhere else.
        $this->app->singleton(
            GeoLocator::class,
            fn (Application $app): GeoLocator => $app->environment('testing')
                ? new NullGeoLocator
                : new HttpGeoLocator,
        );
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        $this->configureDefaults();
    }

    /**
     * Configure default behaviors for production-ready applications.
     */
    protected function configureDefaults(): void
    {
        Date::use(CarbonImmutable::class);

        DB::prohibitDestructiveCommands(
            app()->isProduction(),
        );

        Password::defaults(fn (): ?Password => app()->isProduction()
            ? Password::min(12)
                ->mixedCase()
                ->letters()
                ->numbers()
                ->symbols()
                ->uncompromised()
            : null,
        );
    }
}
