<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Invoices\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Maestro\Core\Modules\Invoices\Models\Invoice;
use Maestro\Core\Modules\Invoices\Models\VatRate;
use Maestro\Core\Support\CorePaths;
use Maestro\Core\Support\ModuleRuntimeState;

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

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

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

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

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