<?php

namespace CaptchaPlugin;

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

class CaptchaServiceProvider extends ServiceProvider
{
    /**
     * Register services
     */
    public function register(): void
    {
        // Merge configurația
        $this->mergeConfigFrom(__DIR__ . '/config/captcha.php', 'captcha');

        // Înregistrează singleton pentru CaptchaService
        $this->app->singleton('captcha', function ($app) {
            return new Services\CaptchaService();
        });
    }

    /**
     * Bootstrap services
     */
    public function boot(): void
    {
        // Încarcă view-urile
        $this->loadViewsFrom(__DIR__ . '/resources/views', 'captcha');

        // Înregistrează middleware
        $router = $this->app['router'];
        $router->aliasMiddleware('captcha', Http\Middleware\VerifyCaptcha::class);

        // Înregistrează Blade component
        Blade::component('captcha', View\Components\Captcha::class);

        // Înregistrează rutele admin
        $this->registerRoutes();
    }

    /**
     * Register admin routes
     */
    protected function registerRoutes(): void
    {
        Route::middleware(['web', 'auth', 'admin'])
            ->prefix('admin')
            ->group(function () {
                Route::get('/captcha', [Http\Controllers\CaptchaController::class, 'index'])
                    ->name('admin.captcha.index');
                Route::post('/captcha/update', [Http\Controllers\CaptchaController::class, 'update'])
                    ->name('admin.captcha.update');
                Route::get('/captcha/statistics', [Http\Controllers\CaptchaController::class, 'statistics'])
                    ->name('admin.captcha.statistics');
                Route::post('/captcha/test', [Http\Controllers\CaptchaController::class, 'test'])
                    ->name('admin.captcha.test');
                Route::delete('/captcha/rate-limits', [Http\Controllers\CaptchaController::class, 'clearRateLimits'])
                    ->name('admin.captcha.clear-rate-limits');
                Route::delete('/captcha/statistics', [Http\Controllers\CaptchaController::class, 'cleanStatistics'])
                    ->name('admin.captcha.clean-statistics');
            });
    }
}
