<?php

namespace App\Providers;

use Elceka\Laravel\HashId\Facades\Hasher;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
use Ramsey\Uuid\Uuid;

/**
 * Class RouteServiceProvider
 *
 * Handles route registration, model bindings, and rate limiting for the application.
 */
class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * @var string
     */
    public const HOME = '/';

    /**
     * Define route model bindings, pattern filters, and other route configuration.
     *
     * Registers custom route bindings for user IDs, information IDs, UUIDs,
     * and hash IDs. Configures rate limiting and maps application routes.
     */
    public function boot(): void
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });

        $this->routes(function () {
            $this->mapApiRoutes();
            $this->mapWebRoutes();
        });

        Route::bind('pouzivatelId', function ($id) {
            if (is_numeric($id)) {
                return intval($id);
            }

            return Hasher::decode($id);
        });

        Route::bind('informaciaId', function ($id) {
            if (is_numeric($id)) {
                return intval($id);
            }

            return Hasher::decode($id);
        });

        Route::bind('uuid', function ($uuid) {
            return Uuid::fromString($uuid);
        });

        Route::bind('informaciaHashId', function ($hashId) {
            if (is_numeric($hashId)) {
                return intval($hashId);
            }

            return Hasher::decode($hashId);
        });

        $this->registerBreadcrumbs();
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless and are prefixed with '/api'.
     */
    protected function mapApiRoutes(): void
    {
        Route::prefix('api')
            ->group(base_path('routes/api.php'));
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     * Backend routes require authentication and non-blocked user status.
     */
    protected function mapWebRoutes(): void
    {
        Route::middleware('web')
            ->group(base_path('routes/web.php'));

        Route::middleware(['web', 'auth', 'auth.not_blocked'])
            ->group(function () {
                require base_path('routes/backend/web.php');
            });
    }

    /**
     * Register breadcrumbs configuration for the application.
     *
     * Loads breadcrumb definitions if the Breadcrumbs class is available.
     */
    protected function registerBreadcrumbs(): void
    {
        if (class_exists('Breadcrumbs')) {
            require base_path('routes/backend/breadcrumbs.php');
        }
    }
}
