<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\DepartmentController;
use App\Http\Controllers\DesignationController;
use App\Http\Controllers\EmployeeController;
use App\Http\Controllers\LeaveTypeController;
use App\Http\Controllers\LeaveRequestController;
use App\Http\Controllers\AttendanceController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\SalaryStructureController;
use App\Http\Controllers\PayslipController;
use App\Http\Controllers\BioTimeDeviceController;
use App\Http\Controllers\BioTimePushController;
use App\Http\Controllers\AreaController;
use App\Http\Controllers\ShiftController;
use App\Http\Controllers\ScheduleController;
use App\Http\Controllers\SettingController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\BackupController;
use App\Http\Controllers\ProcessTemplateController;
use App\Http\Controllers\EmployeeProcessController;
use App\Http\Controllers\ManualAttendanceController;
use App\Http\Controllers\ReportsController;
use App\Http\Controllers\LoanController;
use App\Http\Controllers\EmployeeDeviceSyncController;
use App\Http\Controllers\EmployeeImportExportController;
use App\Http\Controllers\InstallController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

// BioTime Push Protocol (ADMS) Endpoints
Route::any('/iclock/cdata', [BioTimePushController::class, 'cdata']);
Route::get('/iclock/getrequest', [BioTimePushController::class, 'getRequest']);
Route::post('/iclock/devicecmd', [BioTimePushController::class, 'deviceCmd']);

Route::get('language/{locale}', function ($locale) {
    if (in_array($locale, ['en', 'ar'])) {
        session(['locale' => $locale]);
    }
    return redirect()->back();
})->name('language.switch');

Route::get('/', function () {
    return view('welcome');
});

Route::get('/install', [InstallController::class, 'index'])->name('install.index');
Route::get('/install/requirements', [InstallController::class, 'requirementsPage'])->name('install.requirements');
Route::post('/install', [InstallController::class, 'store'])->name('install.store');
Route::get('/install/update', [InstallController::class, 'updatePage'])->name('install.update');
Route::post('/install/update', [InstallController::class, 'runUpdate'])->name('install.update.run');

Route::get('/dashboard', DashboardController::class)
    ->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');

    Route::resource('departments', DepartmentController::class);
    Route::resource('designations', DesignationController::class);
    // Employee Import/Export
    Route::get('/employees/import-export', [EmployeeImportExportController::class, 'index'])->name('employees.import-export');
    Route::get('/employees/template-csv', [EmployeeImportExportController::class, 'templateCsv'])->name('employees.template.csv');
    Route::get('/employees/export-csv', [EmployeeImportExportController::class, 'exportCsv'])->name('employees.export.csv');
    Route::post('/employees/import-csv', [EmployeeImportExportController::class, 'importCsv'])->name('employees.import.csv');

    Route::resource('employees', EmployeeController::class);
    Route::resource('leave-types', LeaveTypeController::class);
    Route::resource('leave-requests', LeaveRequestController::class);

    Route::get('/attendance', [AttendanceController::class, 'index'])->name('attendance.index');
    Route::get('/attendance/logs', [AttendanceController::class, 'logs'])->name('attendance.logs');
    Route::get('/attendance/sync-logs', [AttendanceController::class, 'syncLogs'])->name('attendance.sync-logs');
    Route::post('/attendance/sync', [AttendanceController::class, 'sync'])->name('attendance.sync');
    Route::get('/attendance/manual', [ManualAttendanceController::class, 'index'])->name('manual-attendance.index');
    Route::get('/attendance/manual/create', [ManualAttendanceController::class, 'create'])->name('manual-attendance.create');
    Route::post('/attendance/manual', [ManualAttendanceController::class, 'store'])->name('manual-attendance.store');
    
    // Reports
    Route::get('/reports/time-card', [ReportsController::class, 'timeCard'])->name('reports.time-card');
    Route::get('/reports/employees', [ReportsController::class, 'employeeDetails'])->name('reports.employees');
    Route::get('/reports/weekly', [ReportsController::class, 'weekly'])->name('reports.weekly');
    Route::get('/reports/monthly', [ReportsController::class, 'monthly'])->name('reports.monthly');
    Route::get('/reports/salary', [ReportsController::class, 'salary'])->name('reports.salary');
    Route::get('/reports/salary/print', [ReportsController::class, 'printSalarySlip'])->name('reports.salary.print');

    Route::resource('shifts', ShiftController::class);
    Route::resource('schedules', ScheduleController::class);

    Route::resource('biotime-devices', BioTimeDeviceController::class);
    Route::post('/biotime-devices/{biotimeDevice}/toggle-lock', [BioTimeDeviceController::class, 'toggleLock'])->name('biotime-devices.toggle-lock');
    Route::post('/biotime-devices/{biotimeDevice}/reboot', [BioTimeDeviceController::class, 'reboot'])->name('biotime-devices.reboot');
    Route::post('/biotime-devices/{biotimeDevice}/clear-pending', [BioTimeDeviceController::class, 'clearPendingCommands'])->name('biotime-devices.clear-pending');
    Route::get('/biotime-devices/live-transactions', [BioTimeDeviceController::class, 'liveTransactions'])->name('biotime-devices.live-transactions');
    Route::post('/biotime-devices/{biotimeDevice}/sync-employees', [EmployeeDeviceSyncController::class, 'syncAllToDevice'])->name('biotime-devices.sync-employees');
    Route::post('/biotime-devices/{biotimeDevice}/read-info', [BioTimeDeviceController::class, 'readInfo'])->name('biotime-devices.read-info');
    Route::post('/biotime-devices/{biotimeDevice}/remove-admin', [BioTimeDeviceController::class, 'removeAdminAccess'])->name('biotime-devices.remove-admin');
    Route::post('/biotime-devices/{biotimeDevice}/sync-users-from-device', [BioTimeDeviceController::class, 'syncUsersFromDevice'])->name('biotime-devices.sync-users-from-device');
    Route::resource('areas', AreaController::class);
    Route::resource('salary-structures', SalaryStructureController::class);
    Route::resource('payslips', PayslipController::class);

    // Company Settings
    Route::get('/settings', [SettingController::class, 'index'])->name('settings.index');
    Route::patch('/settings', [SettingController::class, 'update'])->name('settings.update');

    // User & Role Management
    Route::resource('users', UserController::class);
    Route::resource('roles', RoleController::class);

    // Backup
    Route::get('/backup', [BackupController::class, 'index'])->name('backup.index');
    Route::post('/backup/download', [BackupController::class, 'download'])->name('backup.download');

    // Onboarding / Offboarding
    Route::resource('process-templates', ProcessTemplateController::class);
    Route::resource('processes', EmployeeProcessController::class);
    Route::post('/processes/tasks/{task}/complete', [EmployeeProcessController::class, 'completeTask'])->name('processes.task.complete');
    
    // Loans
    Route::resource('loans', LoanController::class);
    Route::post('/loans/{loan}/approve', [LoanController::class, 'approve'])->name('loans.approve');
    Route::post('/loans/{loan}/reject', [LoanController::class, 'reject'])->name('loans.reject');
});

require __DIR__.'/auth.php';
