<?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.
 *
 * @package App\Providers
 */
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.
     *
     * @return void
     */
    public function boot(): void
    {
        $this->configureRateLimiting();

        $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();
    }

    /**
     * Configure the rate limiters for the application.
     *
     * Sets up rate limiting for API routes - 60 requests per minute per user or IP address.
     *
     * @return void
     */
    protected function configureRateLimiting(): void
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
        });
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless and are prefixed with '/api'.
     *
     * @return void
     */
    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.
     *
     * @return void
     */
    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.
     *
     * @return void
     */
    protected function registerBreadcrumbs(): void
    {
        if (class_exists('Breadcrumbs')) {
            require base_path('routes/backend/breadcrumbs.php');
        }
    }
}
