<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Items\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Maestro\Core\Modules\Items\Models\Item;
use Maestro\Core\Support\CorePaths;
use Maestro\Core\Support\ModuleRuntimeState;

class ItemsModuleServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        if (config('core.modules.items', true) !== true) {
            return;
        }

        Route::bind('item', fn (string $value): Item => Item::query()->whereKey($value)->firstOrFail());

        if (ModuleRuntimeState::shouldLoadMigrations('items')) {
            $this->loadMigrationsFrom(dirname(__DIR__).'/database/migrations');
        }

        $this->loadRoutesFrom(CorePaths::packageRoot().'/routes/modules/items.php');
    }
}
