<?php

use App\Modules\HR\Compensation\Controllers\CompensationController;
use App\Modules\HR\Compensation\Requests\CompensationRequest;
use App\Modules\HR\Employees\Models\Employee;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::middleware(['module:hr', 'can:compensation.view'])->group(function (): void {
    foreach (['allowance' => 'allowances', 'deduction' => 'deductions'] as $kind => $uri) {
        Route::get("hr/$uri", fn (Request $req, CompensationController $c) => $c->index($req, $kind))->name("hr.$uri.index");
        Route::post("hr/$uri", fn (CompensationRequest $r, CompensationController $c) => $c->store($r, $kind))
            ->middleware('can:compensation.create')->name("hr.$uri.store");
        Route::put("hr/$uri/{id}", fn (CompensationRequest $r, CompensationController $c, int $id) => $c->update($r, $kind, $id))
            ->middleware('can:compensation.update')->name("hr.$uri.update");
        Route::delete("hr/$uri/{id}", fn (CompensationController $c, int $id) => $c->destroy($kind, $id))
            ->middleware('can:compensation.manage')->name("hr.$uri.destroy");
        Route::post("hr/$uri/employees/{employee}", fn (CompensationRequest $r, CompensationController $c, Employee $employee) => $c->attach($r, $kind, $employee))
            ->middleware('can:compensation.manage')->name("hr.$uri.employees.attach");
        Route::delete("hr/$uri/employees/{employee}/{item}", fn (Request $r, CompensationController $c, Employee $employee, int $item) => $c->detach($r, $kind, $employee, $item))
            ->middleware('can:compensation.manage')->name("hr.$uri.employees.detach");
    }
});
