<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Customers\Providers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Maestro\Core\Modules\Customers\Models\Customer;

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

        Route::bind('customer', function (string $value): Customer {
            $user = Auth::user();
            abort_if(! $user || ! $user->organization_id, 404);

            return Customer::query()
                ->where('organization_id', $user->organization_id)
                ->whereKey($value)
                ->firstOrFail();
        });

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

        $packageRoot = dirname(__DIR__, 4);
        $this->loadRoutesFrom($packageRoot.'/routes/modules/customers.php');
    }
}
