<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

class ModuleServiceProvider extends ServiceProvider
{
    /**
     * @var list<string>
     */
    private array $modules = [
        'Core',
        'SuperAdmin',
        'Tenant',
        'Subscription',
        'Notification',
        'Invoice',
        'POS',
        'Inventory',
        'Accounting',
        'CRM',
        'CustomerPortal',
        'Reports',
        'AI',
    ];

    public function boot(): void
    {
        foreach ($this->modules as $module) {
            $modulePath = app_path("Modules/{$module}");

            $this->loadModuleRoutes("{$modulePath}/Routes/api.php");
            $this->loadModuleMigrations("{$modulePath}/Database/Migrations");
        }
    }

    private function loadModuleRoutes(string $path): void
    {
        if (! file_exists($path)) {
            return;
        }

        Route::middleware('api')
            ->prefix('api/v1')
            ->group($path);
    }

    private function loadModuleMigrations(string $path): void
    {
        if (! is_dir($path)) {
            return;
        }

        $this->loadMigrationsFrom($path);
    }
}
