<?php

use App\Modules\HR\Compensation\Controllers\CompensationController;
use App\Modules\HR\Compensation\Requests\CompensationRequest;
use App\Modules\HR\Employees\Models\Employee;
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 (CompensationController $c) => $c->index($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");
    }
});
