<?php

declare(strict_types=1);

namespace Builder\Core;

use Builder\Core\Foundation\Registry as ModuleRegistry;
use Builder\Core\Foundation\Resources\FieldHandlerRegistry;
use Builder\Core\Foundation\Revisions\RevisionService;
use Builder\Core\Providers\AuthServiceProvider;
use Builder\Core\Providers\BladeServiceProvider;
use Builder\Core\Providers\ConsoleServiceProvider;
use Builder\Core\Providers\ExtensionServiceProvider;
use Builder\Core\Providers\MacroServiceProvider;
use Builder\Core\Providers\ScheduleServiceProvider;
use Illuminate\Support\ServiceProvider;
use Override;

final class BuilderServiceProvider extends ServiceProvider
{
    public const string CURRENT_VERSION = '1.0.0-alpha';

    #[Override]
    public function register(): void
    {
        $this->app->singleton(ModuleRegistry::class);
        $this->app->singleton(RevisionService::class);
        $this->app->singleton(FieldHandlerRegistry::class);

        $this->mergeConfigFrom(__DIR__.'/../config/builder.php', 'builder');

        $this->app->register(MacroServiceProvider::class);
        $this->app->register(AuthServiceProvider::class);
        $this->app->register(BladeServiceProvider::class);
        $this->app->register(ScheduleServiceProvider::class);
        $this->app->register(ConsoleServiceProvider::class);
        $this->app->register(ExtensionServiceProvider::class);
    }

    public function boot(): void
    {
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'builder');
        $this->loadMigrationsFrom(__DIR__.'/../database/migrations');

        // Routes are loaded last (after all packages have booted) so that
        // static routes in other packages — e.g. GET /media — are registered
        // before core's /{resource} catch-all and take priority in matching.
        $this->app->booted(function (): void {
            $this->loadRoutesFrom(__DIR__.'/../routes/web.php');
        });

        $this->loadTranslationsFrom(__DIR__.'/../lang', 'builder');
    }
}
