<?php

namespace App\Providers;

use App\Services\ActivityLogService;
use App\Services\DashboardService;
use App\Services\NotificationService;
use App\Services\ProjectProgressService;
use App\Services\ReportService;
use App\Services\WorkloadService;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(ActivityLogService::class);
        $this->app->singleton(NotificationService::class);
        $this->app->singleton(DashboardService::class);
        $this->app->singleton(ReportService::class);
        $this->app->singleton(ProjectProgressService::class);
        $this->app->singleton(WorkloadService::class);
    }

    public function boot(): void
    {
        RateLimiter::for('login', function (Request $request) {
            $identifier = Str::lower((string) $request->input('identifier', $request->input('email')));

            return [
                Limit::perMinute(5)->by($identifier.'|'.$request->ip()),
                Limit::perMinute(20)->by($request->ip()),
            ];
        });

        RateLimiter::for('sensitive', function (Request $request) {
            return Limit::perMinute(30)->by(optional($request->user())->id ?: $request->ip());
        });
    }
}
