<?php

use Illuminate\Support\Facades\Route;

use App\Modules\HR\Payroll\Http\Controllers\AdditionController;
use App\Modules\HR\Payroll\Http\Controllers\DeductionController;
use App\Modules\HR\Payroll\Http\Controllers\LoanController;
use App\Modules\HR\Payroll\Http\Controllers\EmployeeLoanController;
use App\Modules\HR\Payroll\Http\Controllers\PayrollGroupController;
use App\Modules\HR\Payroll\Http\Controllers\PayrollAdjustmentController;
use App\Modules\HR\Payroll\Http\Controllers\PayrollProcessController;


Route::middleware(['auth','branch','module:payroll'])
    ->prefix('payroll')
    ->name('payroll.')
    ->group(function () {
        Route::resource('payroll-groups', PayrollGroupController::class);
        Route::post('payroll-groups/{payroll_group}/status', [PayrollGroupController::class, 'status'])
            ->name('payroll-groups.status');
        Route::resource('additions', AdditionController::class);
        Route::post('additions/{addition}/status', [AdditionController::class, 'status'])
            ->name('additions.status');
        Route::resource('deductions', DeductionController::class);
        Route::post('deductions/{deduction}/status', [DeductionController::class, 'status'])
            ->name('deductions.status');
        Route::resource('loans', LoanController::class);
        Route::post('loans/{loan}/status', [LoanController::class, 'status'])
            ->name('loans.status');
        // Static employee-loan routes must come before the resource to avoid {employeeLoan} capturing them
        Route::get('employee-loans/import-template', [EmployeeLoanController::class, 'downloadTemplate'])
            ->name('employee-loans.import-template');
        Route::post('employee-loans/import', [EmployeeLoanController::class, 'importData'])
            ->name('employee-loans.import');
        Route::get('employee-loans/employee/{employee}/info', [EmployeeLoanController::class, 'getEmployeeInfo'])
            ->name('employee-loans.employee-info');
        Route::get('employee-loans/loan/{loan}/info', [EmployeeLoanController::class, 'getLoanInfo'])
            ->name('employee-loans.loan-info');
        Route::post('employee-loans/calculate-installment', [EmployeeLoanController::class, 'calculateInstallment'])
            ->name('employee-loans.calculate-installment');
        Route::resource('employee-loans', EmployeeLoanController::class);

        // Additional actions
        Route::post('employee-loans/{employeeLoan}/approve', [EmployeeLoanController::class, 'approve'])
            ->name('employee-loans.approve');
        Route::post('employee-loans/{employeeLoan}/reject', [EmployeeLoanController::class, 'reject'])
            ->name('employee-loans.reject');
        Route::post('employee-loans/{employeeLoan}/activate', [EmployeeLoanController::class, 'activate'])
            ->name('employee-loans.activate');

        Route::resource('payroll-adjustments', PayrollAdjustmentController::class);
        Route::get('payroll-adjustments/{payroll_adjustment}/employee-adjustments', [PayrollAdjustmentController::class, 'getEmployeeAdjustments'])
            ->name('payroll-adjustments.employee-adjustments');
        Route::post('payroll-adjustments/{payroll_adjustment}/save-adjustments', [PayrollAdjustmentController::class, 'saveAdjustments'])
            ->name('payroll-adjustments.save-adjustments');
        Route::get('payroll-adjustments/{payroll_adjustment}/import-template', [PayrollAdjustmentController::class, 'downloadTemplate'])
            ->name('payroll-adjustments.import-template');
        Route::post('payroll-adjustments/{payroll_adjustment}/import-data', [PayrollAdjustmentController::class, 'importData'])
            ->name('payroll-adjustments.import-data');
        Route::get('payroll-processes', [PayrollProcessController::class, 'index'])
            ->name('payroll-processes.index');
        Route::get('payroll-processes/create', [PayrollProcessController::class, 'create'])
            ->name('payroll-processes.create');
        Route::post('payroll-processes', [PayrollProcessController::class, 'store'])
            ->name('payroll-processes.store');
        Route::get('payroll-processes/{payroll_process}', [PayrollProcessController::class, 'show'])
            ->name('payroll-processes.show');
        Route::delete('payroll-processes/{payroll_process}', [PayrollProcessController::class, 'destroy'])
            ->name('payroll-processes.destroy');
        Route::post('payroll-processes/{payroll_process}/process', [PayrollProcessController::class, 'processPayroll'])
            ->name('payroll-processes.process');
        Route::post('payroll-processes/{payroll_process}/freeze', [PayrollProcessController::class, 'freeze'])
            ->name('payroll-processes.freeze');
        Route::post('payroll-processes/{payroll_process}/unfreeze', [PayrollProcessController::class, 'unfreeze'])
            ->name('payroll-processes.unfreeze');
    });

