<?php

namespace App\Modules\Integration;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use App\Modules\Integration\Middleware\TenantIdentificationMiddleware;

class IntegrationServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->loadRoutesFrom(__DIR__ . '/Routes/api.php');
        $this->loadRoutesFrom(__DIR__ . '/Routes/web.php');
        $this->loadMigrationsFrom(__DIR__ . '/Database/Migrations');

        // 註冊 Middleware 別名
        Route::aliasMiddleware('integration.tenant', TenantIdentificationMiddleware::class);

        // 定義 Integration API 速率限制（每分鐘 60 次，依 Token 使用者識別）
        RateLimiter::for('integration', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });
    }

    public function register()
    {
        //
    }
}
