<?php

namespace App\Providers;

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;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * Typically, users are redirected here after authentication.
     *
     * @var string
     */
    public const HOME = '/';

    /**
     * Define your route model bindings, pattern filters, and other route configuration.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::middleware('api')
                ->prefix('api')
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->group(base_path('routes/web.php'));
        });
    }

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });
        RateLimiter::for('verify', function (Request $request) {
            return Limit::perMinute(VERIFY_SEND_LIMIT)->response(function () {
                return response([
                    'message' => '너무 많은 요청을 하였습니다. 잠시 후 시도해주세요.'
                ]);
            });
        });
        RateLimiter::for('commentWrite', function (Request $request) {
            return Limit::perMinute(3)->response(function () {
                return response([
                    'message' => '댓글/답글은 60초 내에 한 개만 등록하실 수 있습니다.'
                ]);
            });
        });
    }
}
