<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Offers\Providers;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Maestro\Core\Modules\Offers\Console\Commands\SyncOfferExpiryCommand;
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
    {
        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');

        $this->commands([SyncOfferExpiryCommand::class]);

        $this->callAfterResolving(Schedule::class, function (Schedule $schedule): void {
            $schedule->command(SyncOfferExpiryCommand::class)->daily();
        });
    }
}
