<?php

namespace Maestro\Core;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Maestro\Core\Console\Commands\InstallCommand;
use Maestro\Core\Console\Commands\StatusCommand;
use Maestro\Core\Console\Commands\UpdateCommand;
use Maestro\Core\Providers\DataTableServiceProvider;
use Spatie\Permission\Middleware\PermissionMiddleware;
use Spatie\Permission\Middleware\RoleMiddleware;

class MaestroServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->mergeConfigFrom(__DIR__.'/../config/maestro.php', 'maestro');

        $this->app->register(DataTableServiceProvider::class);

        if ($this->app->runningInConsole()) {
            $this->commands([
                InstallCommand::class,
                UpdateCommand::class,
                StatusCommand::class,
            ]);
        }
    }

    public function boot(): void
    {
        $this->loadMigrationsFrom(__DIR__.'/../database/migrations');

        $this->publishes([
            __DIR__.'/../config/maestro.php' => config_path('maestro.php'),
        ], 'maestro-config');

        // Publish frontend stubs
        $this->publishes([
            __DIR__.'/../stubs/vite.config.js'                => base_path('vite.config.js'),
            __DIR__.'/../stubs/resources/css/app.css'         => resource_path('css/app.css'),
            __DIR__.'/../stubs/resources/js/app.js'           => resource_path('js/app.js'),
            __DIR__.'/../stubs/resources/views/app.blade.php' => resource_path('views/app.blade.php'),
            __DIR__.'/../stubs/routes/web.php'                => base_path('routes/web.php'),
            __DIR__.'/../stubs/routes/api.php'                 => base_path('routes/api.php'),
            __DIR__.'/../stubs/.env.example'                  => base_path('.env.example'),
            __DIR__.'/../stubs/.npmrc'                        => base_path('.npmrc'),
            __DIR__.'/../stubs/package.json'                  => base_path('package.json'),
        ], 'maestro-frontend');

        // Override auth model
        config(['auth.providers.users.model' => \Maestro\Core\Models\User::class]);

        // Register Spatie middleware
        $this->app['router']->aliasMiddleware('permission', PermissionMiddleware::class);
        $this->app['router']->aliasMiddleware('role', RoleMiddleware::class);

        // Register custom middleware
        $this->app['router']->aliasMiddleware('fingerprint', \Maestro\Core\Http\Middleware\ValidateSessionFingerprint::class);
        $this->app['router']->aliasMiddleware('absolute_timeout', \Maestro\Core\Http\Middleware\AbsoluteSessionTimeout::class);
        $this->app['router']->aliasMiddleware('active_user', \Maestro\Core\Http\Middleware\EnsureUserIsActive::class);

        $this->registerRoutes();
    }

    protected function registerRoutes(): void
    {
        Route::middleware(['web'])
            ->prefix('api')
            ->name('maestro.api.')
            ->group(__DIR__.'/../routes/api.php');

        // Broadcasting channels
        if (file_exists(__DIR__.'/../routes/channels.php')) {
            require __DIR__.'/../routes/channels.php';
        }
    }
}
