<?php

use App\Http\Controllers\Admin\ActivityLogController;
use App\Http\Controllers\Admin\Auth\AuthController;
use App\Http\Controllers\Admin\BrandingController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\RoleController;
use App\Http\Controllers\Admin\UserController;
use Illuminate\Support\Facades\Route;

Route::middleware('guest')->prefix('admin')->name('admin.')->group(function () {
    Route::get('login', [AuthController::class, 'showLogin'])->name('login');
    Route::post('login', [AuthController::class, 'storeLogin']);
});

Route::middleware(['auth', 'admin'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    Route::resource('users', UserController::class);
    Route::patch('users/{user}/status', [UserController::class, 'updateStatus'])->name('users.status');

    Route::resource('employees', \App\Http\Controllers\Admin\EmployeeController::class);
    Route::resource('departments', \App\Http\Controllers\Admin\DepartmentController::class);

    Route::get('/branding', [BrandingController::class, 'index'])->name('branding.index');
    Route::put('/branding/{brand}', [BrandingController::class, 'update'])->name('branding.update');

    Route::resource('roles', RoleController::class);

    Route::get('/activity-logs', [ActivityLogController::class, 'index'])->name('activity_logs.index');
    Route::get('/activity-logs/{activityLog}', [ActivityLogController::class, 'show'])->name('activity_logs.show');

    Route::get('/profile', [App\Http\Controllers\Admin\ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [App\Http\Controllers\Admin\ProfileController::class, 'update'])->name('profile.update');

    Route::get('/business/branding', [App\Http\Controllers\Admin\BusinessSettingController::class, 'index'])->name('business.branding');
    Route::post('/business/branding', [App\Http\Controllers\Admin\BusinessSettingController::class, 'update'])->name('business.update');

    Route::get('/business/legal', [App\Http\Controllers\Admin\LegalController::class, 'index'])->name('legal.index');
    Route::post('/business/legal', [App\Http\Controllers\Admin\LegalController::class, 'update'])->name('legal.update');

    Route::get('/business/settings', [App\Http\Controllers\Admin\BusinessLogicController::class, 'index'])->name('business.settings.index');
    Route::post('/business/settings', [App\Http\Controllers\Admin\BusinessLogicController::class, 'update'])->name('business.settings.update');

    // Notifications
    Route::get('/notifications', [App\Http\Controllers\Admin\NotificationController::class, 'index'])->name('notifications.index');
    Route::get('/notifications/recent', [App\Http\Controllers\Admin\NotificationController::class, 'recent'])->name('notifications.recent');
    Route::post('/notifications/{id}/read', [App\Http\Controllers\Admin\NotificationController::class, 'markAsRead'])->name('notifications.read');
    Route::post('/notifications/read-all', [App\Http\Controllers\Admin\NotificationController::class, 'markAllAsRead'])->name('notifications.read_all');
    Route::delete('/notifications/{id}', [App\Http\Controllers\Admin\NotificationController::class, 'destroy'])->name('notifications.destroy');

    // Attendance
    Route::get('/attendance/export', [\App\Http\Controllers\Admin\AttendanceExportController::class, 'export'])->name('attendance.export');

    // Attendance Approvals (must come before parameterized routes)
    Route::get('/attendance/approvals', [App\Http\Controllers\Admin\AttendanceApprovalController::class, 'index'])->name('attendance.approvals');
    Route::post('/attendance/{attendance}/approve', [App\Http\Controllers\Admin\AttendanceApprovalController::class, 'approve'])->name('attendance.approve');
    Route::post('/attendance/{attendance}/reject', [App\Http\Controllers\Admin\AttendanceApprovalController::class, 'reject'])->name('attendance.reject');

    Route::get('/attendance', [\App\Http\Controllers\AttendanceController::class, 'index'])->name('attendance.index');
    Route::get('/attendance/employee/{employee}', [\App\Http\Controllers\AttendanceController::class, 'employeeDetails'])->name('attendance.employee');
    Route::get('/attendance/settings', [\App\Http\Controllers\Admin\AttendanceController::class, 'settings'])->name('attendance.settings');
    Route::post('/attendance/settings', [\App\Http\Controllers\Admin\AttendanceController::class, 'updateSettings'])->name('attendance.settings.update');
    Route::get('/attendance/{attendance}', [\App\Http\Controllers\Admin\AttendanceController::class, 'show'])->name('attendance.show');
    Route::post('/attendance/{attendance}/status', [\App\Http\Controllers\Admin\AttendanceController::class, 'updateStatus'])->name('attendance.status');

    // Leave Types
    Route::resource('leave-types', \App\Http\Controllers\Admin\LeaveTypeController::class)->names([
        'index' => 'leave.types.index',
        'store' => 'leave.types.store',
        'update' => 'leave.types.update',
        'destroy' => 'leave.types.destroy',
    ]);

    // Leave Management
    Route::get('/leave/approvals', [\App\Http\Controllers\Admin\LeaveApprovalController::class, 'index'])->name('leave.approvals');
    Route::post('/leave/{leaveRequest}/approve', [\App\Http\Controllers\Admin\LeaveApprovalController::class, 'approve'])->name('leave.approve');
    Route::post('/leave/{leaveRequest}/reject', [\App\Http\Controllers\Admin\LeaveApprovalController::class, 'reject'])->name('leave.reject');
    Route::get('/leave', [\App\Http\Controllers\Admin\LeaveRequestController::class, 'index'])->name('leave.index');
    Route::get('/leave/calendar', [\App\Http\Controllers\Admin\LeaveRequestController::class, 'calendar'])->name('leave.calendar');

    // Asset Management
    Route::resource('assets', \App\Http\Controllers\Admin\AssetController::class);
    Route::resource('asset-categories', \App\Http\Controllers\Admin\AssetCategoryController::class)->names([
        'index' => 'assets.categories.index',
        'store' => 'assets.categories.store',
        'update' => 'assets.categories.update',
        'destroy' => 'assets.categories.destroy',
    ]);
    Route::post('/assets/assignments', [\App\Http\Controllers\Admin\AssetAssignmentController::class, 'store'])->name('assets.assignments.store');
    Route::post('/assets/assignments/{assignment}/return', [\App\Http\Controllers\Admin\AssetAssignmentController::class, 'return'])->name('assets.assignments.return');

    // Announcements
    Route::resource('announcements', \App\Http\Controllers\Admin\AnnouncementController::class);

    // ─── Payroll Module ────────────────────────────────────────────────────
    // Salary Components
    Route::resource('payroll/salary-components', \App\Http\Controllers\Admin\SalaryComponentController::class)
        ->names([
            'index' => 'payroll.components.index',
            'store' => 'payroll.components.store',
            'update' => 'payroll.components.update',
            'destroy' => 'payroll.components.destroy',
        ])
        ->except(['create', 'edit', 'show']);

    // Payroll Structures
    Route::resource('payroll/structures', \App\Http\Controllers\Admin\PayrollStructureController::class)
        ->names([
            'index' => 'payroll.structures.index',
            'store' => 'payroll.structures.store',
            'update' => 'payroll.structures.update',
            'destroy' => 'payroll.structures.destroy',
        ])
        ->except(['create', 'edit', 'show']);

    // Salary Assignments
    Route::get('/payroll/salary-assignments', [\App\Http\Controllers\Admin\EmployeeSalaryAssignmentController::class, 'index'])->name('payroll.assignments.index');
    Route::post('/payroll/salary-assignments', [\App\Http\Controllers\Admin\EmployeeSalaryAssignmentController::class, 'store'])->name('payroll.assignments.store');
    Route::post('/payroll/salary-assignments/preview', [\App\Http\Controllers\Admin\EmployeeSalaryAssignmentController::class, 'preview'])->name('payroll.assignments.preview');

    // Payroll Generation & Management
    Route::get('/payroll', [\App\Http\Controllers\Admin\PayrollController::class, 'index'])->name('payroll.index');
    Route::get('/payroll/generate', [\App\Http\Controllers\Admin\PayrollController::class, 'preview'])->name('payroll.preview');
    Route::post('/payroll/generate', [\App\Http\Controllers\Admin\PayrollController::class, 'generate'])->name('payroll.generate');
    Route::post('/payroll/{payroll}/send-for-approval', [\App\Http\Controllers\Admin\PayrollController::class, 'sendForApproval'])->name('payroll.send-for-approval');

    // Payroll Approvals — MUST be before /payroll/{payroll} wildcard
    Route::get('/payroll/approvals', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'index'])->name('payroll.approvals.index');
    Route::post('/payroll/approvals/payroll/{payroll}/approve', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'approvePayroll'])->name('payroll.approvals.payroll.approve');
    Route::post('/payroll/approvals/payroll/{payroll}/reject', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'rejectPayroll'])->name('payroll.approvals.payroll.reject');
    Route::post('/payroll/approvals/bonus/{bonusGeneration}/approve', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'approveBonus'])->name('payroll.approvals.bonus.approve');
    Route::post('/payroll/approvals/bonus/{bonusGeneration}/reject', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'rejectBonus'])->name('payroll.approvals.bonus.reject');
    Route::post('/payroll/approvals/increment/{salaryIncrement}/approve', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'approveIncrement'])->name('payroll.approvals.increment.approve');
    Route::post('/payroll/approvals/increment/{salaryIncrement}/reject', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'rejectIncrement'])->name('payroll.approvals.increment.reject');

    Route::post('/payroll/approvals/payroll/bulk-approve', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'bulkApprovePayrolls'])->name('payroll.approvals.payroll.bulk-approve');
    Route::post('/payroll/approvals/payroll/bulk-reject', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'bulkRejectPayrolls'])->name('payroll.approvals.payroll.bulk-reject');
    Route::post('/payroll/approvals/bonus/bulk-approve', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'bulkApproveBonuses'])->name('payroll.approvals.bonus.bulk-approve');
    Route::post('/payroll/approvals/bonus/bulk-reject', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'bulkRejectBonuses'])->name('payroll.approvals.bonus.bulk-reject');
    Route::post('/payroll/approvals/increment/bulk-approve', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'bulkApproveIncrements'])->name('payroll.approvals.increment.bulk-approve');
    Route::post('/payroll/approvals/increment/bulk-reject', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'bulkRejectIncrements'])->name('payroll.approvals.increment.bulk-reject');

    Route::post('/payroll/approvals/loan/{loan}/approve', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'approveLoan'])->name('payroll.approvals.loan.approve');
    Route::post('/payroll/approvals/loan/{loan}/reject', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'rejectLoan'])->name('payroll.approvals.loan.reject');
    Route::post('/payroll/approvals/loan/bulk-approve', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'bulkApproveLoans'])->name('payroll.approvals.loan.bulk-approve');
    Route::post('/payroll/approvals/loan/bulk-reject', [\App\Http\Controllers\Admin\PayrollApprovalController::class, 'bulkRejectLoans'])->name('payroll.approvals.loan.bulk-reject');

    Route::get('/payroll/{payroll}', [\App\Http\Controllers\Admin\PayrollController::class, 'show'])->name('payroll.show');
    Route::post('/payroll/{payroll}/approve', [\App\Http\Controllers\Admin\PayrollController::class, 'approve'])->name('payroll.approve');
    Route::post('/payroll/{payroll}/reject', [\App\Http\Controllers\Admin\PayrollController::class, 'reject'])->name('payroll.reject');

    // Payslip
    Route::get('/payroll/{payroll}/items/{payrollItem}/payslip', [\App\Http\Controllers\Admin\PayslipController::class, 'show'])->name('payroll.payslip.show');
    Route::get('/payroll/{payroll}/items/{payrollItem}/payslip/download', [\App\Http\Controllers\Admin\PayslipController::class, 'download'])->name('payroll.payslip.download');

    // Salary Sheet Export
    Route::get('/payroll/{payroll}/export', [\App\Http\Controllers\Admin\SalarySheetExportController::class, 'export'])->name('payroll.export');

    // Salary Certificate
    Route::get('/salary-certificates', [\App\Http\Controllers\Admin\SalaryCertificateController::class, 'index'])->name('payroll.certificate.index');
    Route::post('/salary-certificates/{salaryCertificateRequest}/approve', [\App\Http\Controllers\Admin\SalaryCertificateController::class, 'approve'])->name('payroll.certificate.approve');
    Route::post('/salary-certificates/{salaryCertificateRequest}/reject', [\App\Http\Controllers\Admin\SalaryCertificateController::class, 'reject'])->name('payroll.certificate.reject');
    Route::get('/salary-certificates/{employee}/download', [\App\Http\Controllers\Admin\SalaryCertificateController::class, 'download'])->name('payroll.certificate.download');

    // Bonus Generate — MUST be before the resource route to avoid wildcard conflicts
    Route::get('/bonuses/generate', [\App\Http\Controllers\Admin\BonusGenerationController::class, 'create'])->name('bonuses.generate.create');
    Route::get('/bonuses/generate/preview', [\App\Http\Controllers\Admin\BonusGenerationController::class, 'preview'])->name('bonuses.generate.preview');
    Route::post('/bonuses/generate', [\App\Http\Controllers\Admin\BonusGenerationController::class, 'generate'])->name('bonuses.generate');
    Route::get('/bonuses/generations', [\App\Http\Controllers\Admin\BonusGenerationController::class, 'index'])->name('bonuses.generations.index');
    Route::post('/bonuses/generations/{bonusGeneration}/send-for-approval', [\App\Http\Controllers\Admin\BonusGenerationController::class, 'sendForApproval'])->name('bonuses.generations.send-for-approval');
    Route::post('/bonuses/generations/{bonusGeneration}/approve', [\App\Http\Controllers\Admin\BonusGenerationController::class, 'approve'])->name('bonuses.generations.approve');

    // Bonus Setup
    Route::resource('bonuses', \App\Http\Controllers\Admin\BonusController::class)
        ->names([
            'index' => 'bonuses.index',
            'store' => 'bonuses.store',
            'update' => 'bonuses.update',
            'destroy' => 'bonuses.destroy',
        ])
        ->except(['create', 'edit', 'show']);

    // Salary Increments
    Route::get('/salary-increments', [\App\Http\Controllers\Admin\SalaryIncrementController::class, 'index'])->name('salary.increments.index');
    Route::post('/salary-increments', [\App\Http\Controllers\Admin\SalaryIncrementController::class, 'store'])->name('salary.increments.store');
    Route::put('/salary-increments/{salaryIncrement}', [\App\Http\Controllers\Admin\SalaryIncrementController::class, 'update'])->name('salary.increments.update');
    Route::delete('/salary-increments/{salaryIncrement}', [\App\Http\Controllers\Admin\SalaryIncrementController::class, 'destroy'])->name('salary.increments.destroy');

    // Tax System
    Route::resource('taxes', \App\Http\Controllers\Admin\TaxController::class);
    Route::resource('tax-slabs', \App\Http\Controllers\Admin\TaxSlabController::class);
    Route::resource('employee-taxes', \App\Http\Controllers\Admin\EmployeeTaxAssignmentController::class);
    Route::post('employee-taxes/bulk', [\App\Http\Controllers\Admin\EmployeeTaxAssignmentController::class, 'bulkAssign'])->name('employee-taxes.bulk');
    Route::post('tax-setup', [\App\Http\Controllers\Admin\TaxSetupController::class, 'update'])->name('admin.tax-setup.update');

    // Loan System
    Route::post('/loans/{loan}/manual-repayment', [\App\Http\Controllers\Admin\LoanController::class, 'manualRepayment'])->name('loans.manual-repayment');
    Route::resource('loans', \App\Http\Controllers\Admin\LoanController::class);

    // (Payroll Approvals routes are now above, before /payroll/{payroll} wildcard)
});
