<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SettingController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\RoleHasPermissionController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\PermissionController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\AttendanceController;
use App\Http\Controllers\ClientController;
use App\Http\Controllers\DepartmentController;
use App\Http\Controllers\DesignationController;
use App\Http\Controllers\EmployeeController;
use App\Http\Controllers\LeaveTypeController;
use App\Http\Controllers\LeaveController;
use App\Http\Controllers\LoanController;
use App\Http\Controllers\PayrollItemController;
use App\Http\Controllers\PayrollController;
use App\Http\Controllers\SalaryStructureController;
use App\Http\Controllers\ShiftController;
use App\Http\Controllers\SkillController;
use App\Http\Controllers\DashboardController;


/*
  |--------------------------------------------------------------------------
  | Web Routes
  |--------------------------------------------------------------------------
  |
  | Here is where you can register web routes for your application. These
  | routes are loaded by the RouteServiceProvider within a group which
  | contains the "web" middleware group. Now create something great!
  |
 */
Route::fallback(function () {
    return redirect('/login');
});

Auth::routes();
if (file_exists(base_path('routes/custom-admin.php'))) {
    require base_path('routes/custom-admin.php');
}
Route::get('/forgot-password', function () {
    return view('auth.forgot-password');
});
Route::post('password/email', [ForgotPasswordController::class, 'sendResetLinkEmail'])->name('password.email');

Route::get('/clear-all', function () {

    // Delete all session files
    $sessionPath = storage_path('framework/sessions');
    foreach (glob($sessionPath . '/*') as $file) {
        if (is_file($file)) {
            unlink($file);
        }
    }
    // Clear Laravel caches
    Artisan::call('config:cache');
    Artisan::call('cache:clear');
    Artisan::call('view:clear');
    Artisan::call('route:clear');

    return "✅ All caches cleared and session files deleted successfully.";
});


Route::group(['middleware' => ['auth']], function () {
    Route::prefix('admin')->group(function () {

        Route::post('sync-device-index-view', function (Request $request) {
            $v = $request->input('view');
            if (!in_array($v, ['mobile', 'desktop'], true)) {
                return response()->json(['ok' => false], 422);
            }
            $request->session()->put('admin_index_view', $v);

            return response()->json(['ok' => true]);
        })->name('admin.sync-device-index-view');
        /* Dashboard routes */
        Route::get('/', [DashboardController::class, 'index']);
        Route::get('/dashboard', [DashboardController::class, 'index']);

        /* Profile – any authenticated user (no permission required) */
        /* Profile – any authenticated user (no permission required) */
        Route::get('profile', [ProfileController::class, 'edit'])->name('profile.edit');
        Route::put('profile/update', [ProfileController::class, 'update'])->name('profile.update');
        Route::patch('profile/update', [ProfileController::class, 'update']);

        /* Settings – specific routes before resource so they match first */
        Route::get('settings/datatable', [SettingController::class, 'datatable'])->name('settings.datatable');
        Route::any('settings/destroy/{id}', [SettingController::class, 'destroy'])->name('settings.destroy');
        Route::any('settings/restore/{id}', [SettingController::class, 'restore'])->name('settings.restore');
        Route::resource('settings', SettingController::class);

        /* Users – specific routes before resource so they match first */
        Route::get('users/datatable', [UserController::class, 'datatable'])->name('users.datatable');
        Route::get('users/export/excel', [UserController::class, 'exportExcel'])->name('users.export.excel');
        Route::post('users/import/csv', [UserController::class, 'importCsv'])->name('users.import.csv');
        Route::any('users/destroy/{id}', [UserController::class, 'destroy'])->name('users.destroy');
        Route::any('users/restore/{id}', [UserController::class, 'restore'])->name('users.restore');
        Route::resource('users', UserController::class);

        /* Role Has Permissions – specific routes before resource */
        Route::get('role-has-permissions/datatable', [RoleHasPermissionController::class, 'datatable'])->name('role-has-permissions.datatable');
        Route::any('role-has-permissions/destroy/{id}', [RoleHasPermissionController::class, 'destroy'])->name('role-has-permissions.destroy');
        Route::any('role-has-permissions/restore/{id}', [RoleHasPermissionController::class, 'restore'])->name('role-has-permissions.restore');
        Route::resource('role-has-permissions', RoleHasPermissionController::class);

        /* Roles – specific routes before resource */
        Route::get('roles/datatable', [RoleController::class, 'datatable'])->name('roles.datatable');
        Route::any('roles/destroy/{id}', [RoleController::class, 'destroy'])->name('roles.destroy');
        Route::any('roles/restore/{id}', [RoleController::class, 'restore'])->name('roles.restore');
        Route::resource('roles', RoleController::class);

        /* Permissions */
        Route::get('permissions/add', [PermissionController::class, 'create'])->name('permissions.create');
        Route::post('permissions-add', [PermissionController::class, 'store'])->name('permissions.insert');
        
        /* 8- attendances – specific routes before resource */
        Route::get('attendances/export/excel', [AttendanceController::class, 'exportExcel'])->name('attendances.export.excel');
        Route::post('attendances/import/csv', [AttendanceController::class, 'importCsv'])->name('attendances.import.csv');
        Route::any('attendances/restore/{id}', [AttendanceController::class, 'restore'])->name('attendances.restore');
        Route::resource('attendances', AttendanceController::class);

        /* 9- clients – specific routes before resource */
        Route::get('clients/export/excel', [ClientController::class, 'exportExcel'])->name('clients.export.excel');
        Route::post('clients/import/csv', [ClientController::class, 'importCsv'])->name('clients.import.csv');
        Route::any('clients/restore/{id}', [ClientController::class, 'restore'])->name('clients.restore');
        Route::resource('clients', ClientController::class);

        /* 10- departments – specific routes before resource */
        Route::get('departments/export/excel', [DepartmentController::class, 'exportExcel'])->name('departments.export.excel');
        Route::post('departments/import/csv', [DepartmentController::class, 'importCsv'])->name('departments.import.csv');
        Route::any('departments/restore/{id}', [DepartmentController::class, 'restore'])->name('departments.restore');
        Route::resource('departments', DepartmentController::class);

        /* 11- designations – specific routes before resource */
        Route::get('designations/export/excel', [DesignationController::class, 'exportExcel'])->name('designations.export.excel');
        Route::post('designations/import/csv', [DesignationController::class, 'importCsv'])->name('designations.import.csv');
        Route::any('designations/restore/{id}', [DesignationController::class, 'restore'])->name('designations.restore');
        Route::resource('designations', DesignationController::class);

        /* 12- employees – specific routes before resource */
        Route::get('employees/export/excel', [EmployeeController::class, 'exportExcel'])->name('employees.export.excel');
        Route::post('employees/import/csv', [EmployeeController::class, 'importCsv'])->name('employees.import.csv');
        Route::any('employees/restore/{id}', [EmployeeController::class, 'restore'])->name('employees.restore');
        Route::resource('employees', EmployeeController::class);

        /* 13- leave-types – specific routes before resource */
        Route::get('leave-types/export/excel', [LeaveTypeController::class, 'exportExcel'])->name('leave-types.export.excel');
        Route::post('leave-types/import/csv', [LeaveTypeController::class, 'importCsv'])->name('leave-types.import.csv');
        Route::any('leave-types/restore/{id}', [LeaveTypeController::class, 'restore'])->name('leave-types.restore');
        Route::resource('leave-types', LeaveTypeController::class);

        /* 14- leaves – specific routes before resource */
        Route::get('leaves/export/excel', [LeaveController::class, 'exportExcel'])->name('leaves.export.excel');
        Route::post('leaves/import/csv', [LeaveController::class, 'importCsv'])->name('leaves.import.csv');
        Route::any('leaves/restore/{id}', [LeaveController::class, 'restore'])->name('leaves.restore');
        Route::resource('leaves', LeaveController::class);

        /* 15- loans – specific routes before resource */
        Route::get('loans/export/excel', [LoanController::class, 'exportExcel'])->name('loans.export.excel');
        Route::post('loans/import/csv', [LoanController::class, 'importCsv'])->name('loans.import.csv');
        Route::any('loans/restore/{id}', [LoanController::class, 'restore'])->name('loans.restore');
        Route::resource('loans', LoanController::class);

        /* 16- payroll-items – specific routes before resource */
        Route::get('payroll-items/export/excel', [PayrollItemController::class, 'exportExcel'])->name('payroll-items.export.excel');
        Route::post('payroll-items/import/csv', [PayrollItemController::class, 'importCsv'])->name('payroll-items.import.csv');
        Route::any('payroll-items/restore/{id}', [PayrollItemController::class, 'restore'])->name('payroll-items.restore');
        Route::resource('payroll-items', PayrollItemController::class);

        /* 17- payrolls – specific routes before resource */
        Route::get('payrolls/export/excel', [PayrollController::class, 'exportExcel'])->name('payrolls.export.excel');
        Route::post('payrolls/import/csv', [PayrollController::class, 'importCsv'])->name('payrolls.import.csv');
        Route::any('payrolls/restore/{id}', [PayrollController::class, 'restore'])->name('payrolls.restore');
        Route::resource('payrolls', PayrollController::class);

        /* 18- salary-structures – specific routes before resource */
        Route::get('salary-structures/export/excel', [SalaryStructureController::class, 'exportExcel'])->name('salary-structures.export.excel');
        Route::post('salary-structures/import/csv', [SalaryStructureController::class, 'importCsv'])->name('salary-structures.import.csv');
        Route::any('salary-structures/restore/{id}', [SalaryStructureController::class, 'restore'])->name('salary-structures.restore');
        Route::resource('salary-structures', SalaryStructureController::class);

        /* 19- shifts – specific routes before resource */
        Route::get('shifts/export/excel', [ShiftController::class, 'exportExcel'])->name('shifts.export.excel');
        Route::post('shifts/import/csv', [ShiftController::class, 'importCsv'])->name('shifts.import.csv');
        Route::any('shifts/restore/{id}', [ShiftController::class, 'restore'])->name('shifts.restore');
        Route::resource('shifts', ShiftController::class);

        /* 20- skills – specific routes before resource */
        Route::get('skills/export/excel', [SkillController::class, 'exportExcel'])->name('skills.export.excel');
        Route::post('skills/import/csv', [SkillController::class, 'importCsv'])->name('skills.import.csv');
        Route::any('skills/restore/{id}', [SkillController::class, 'restore'])->name('skills.restore');
        Route::resource('skills', SkillController::class);

        /************END OF ROUTES************/

        /************START OF EMAIL ROUTES************/
        /*EMAIL ROUTES*/
        if (file_exists(base_path('routes/admin-emails.php'))) {
            require base_path('routes/admin-emails.php');
        }
    });


});



