<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Offers\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Maestro\Core\Modules\Offers\Models\Offer;
use Maestro\Core\Modules\Offers\Models\OfferServiceCatalogItem;
use Maestro\Core\Support\CorePaths;

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

        Route::bind('offer', function (string $value): Offer {
            return Offer::query()->whereKey((int) $value)->firstOrFail();
        });

        Route::bind('catalog_item', fn (string $value): OfferServiceCatalogItem => OfferServiceCatalogItem::query()
            ->whereKey((int) $value)
            ->firstOrFail());

        $this->loadMigrationsFrom(dirname(__DIR__).'/database/migrations');

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