<?php

namespace Mtc\Plugins\BlockManager\Providers;

use Illuminate\Support\ServiceProvider;
use Mtc\Plugins\BlockManager\Services\BlockShipDiffService;
use Mtc\Plugins\BlockManager\Services\BlockShipService;
use Mtc\Plugins\BlockManager\Services\DesignTokenService;
use Mtc\Plugins\BlockManager\Services\ElementCatalogueService;
use Mtc\Plugins\BlockManager\Services\PropertySchemaService;
use Mtc\Plugins\BlockManager\Services\TwigGenerator;
use Mtc\Plugins\BlockManager\Services\VariantConflictDetector;
use Mtc\Plugins\BlockManager\Services\VariantImportService;
use Mtc\Plugins\BlockManager\Services\VariantLessGenerator;
use Mtc\Plugins\BlockManager\Services\VariantLessParser;
use Mtc\Plugins\BlockManager\Services\VariantManifestPatcher;
use Mtc\Plugins\BlockManager\Services\VariantPreviewService;
use Mtc\Plugins\BlockManager\Services\VariantPublishService;
use Mtc\Plugins\BlockManager\Services\VariantRawValueAuditor;
use Mtc\Plugins\BlockManager\Services\VariantStyleAggregator;
use Mtc\Plugins\BlockManager\Services\VariantTwigGenerator;

class BlockManagerServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        // Mirror loader.php's marker via the modern provider path. The
        // legacy plugins/*\/loader.php glob in core/includes/header.inc.php
        // is fragile under CLI bootstrap (twig.php is transitively required
        // by a provider before header.inc.php's own require_once runs, so
        // the loader-path collection ends up empty). Defining the constant
        // here keeps the discovery signal observable in tests and CLI.
        if (!defined('BLOCK_MANAGER_LOADED')) {
            define('BLOCK_MANAGER_LOADED', true);
        }

        $this->app->singleton(ElementCatalogueService::class);
        $this->app->singleton(TwigGenerator::class);
        $this->app->singleton(BlockShipService::class);
        $this->app->singleton(BlockShipDiffService::class);
        $this->app->singleton(DesignTokenService::class);
        $this->app->singleton(PropertySchemaService::class);
        $this->app->singleton(VariantPreviewService::class);
        $this->app->singleton(VariantTwigGenerator::class);
        $this->app->singleton(VariantLessGenerator::class);
        $this->app->singleton(VariantManifestPatcher::class);
        $this->app->singleton(VariantPublishService::class);
        $this->app->singleton(VariantLessParser::class);
        $this->app->singleton(VariantImportService::class);
        $this->app->singleton(VariantConflictDetector::class);
        $this->app->singleton(VariantRawValueAuditor::class);
        $this->app->singleton(VariantStyleAggregator::class);
    }

    public function boot(): void
    {
        $this->loadRoutesFrom(__DIR__ . '/../routes/api.php');
        $this->loadRoutesFrom(__DIR__ . '/../routes/web.php');
        $this->loadMigrationsFrom(__DIR__ . '/../database/migrations');

        // The Twig-loader path for the element catalogue is registered in
        // plugins/BlockManager/loader.php — that file runs AFTER
        // core/includes/twig.php has bound the `twig` instance, whereas this
        // boot() method runs before it (BootProviders precedes the twig.php
        // require_once in header.inc.php).
    }
}
