<?php

use App\Http\Controllers\EmployeeController;
use App\Http\Controllers\AttendanceController;
use App\Http\Controllers\SalaryController;
use App\Http\Controllers\AttendanceRequestController;
use App\Http\Controllers\LeaveRequestController;
use App\Http\Controllers\PayslipController;
use App\Http\Controllers\DeductionTypeController;
use App\Http\Controllers\BusinessSettingController;
use App\Http\Controllers\AttendanceReportController;
use App\Http\Controllers\HolidayController;
use App\Http\Controllers\HolidayCalendarController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ProfileApprovalController;
use App\Http\Controllers\ExpenseController;
use App\Http\Controllers\BiometricDeviceController;

Auth::routes(['register' => false]); 

Route::middleware(['auth'])->group(function () {
    Route::get('/', [\App\Http\Controllers\DashboardController::class, 'index'])->name('dashboard')->middleware('permission:dashboard.view');
    Route::view('/features', 'features')->name('features');

    // Employee Dashboard
    Route::get('employee-dashboard', [\App\Http\Controllers\EmployeeDashboardController::class, 'index'])->name('employee.dashboard');
    Route::get('employee-dashboard/stats', [\App\Http\Controllers\EmployeeDashboardController::class, 'getStats'])->name('employee.dashboard.stats');
    Route::get('employee-dashboard/data', [\App\Http\Controllers\EmployeeDashboardController::class, 'getAttendanceData'])->name('employee.dashboard.data');

    // Attendance routes for everyone
    Route::get('attendances/status', [AttendanceController::class, 'getStatus'])->name('attendances.status');
    Route::get('attendances/data', [AttendanceController::class, 'getData'])->name('attendances.data');
    Route::post('attendances/punch', [AttendanceController::class, 'punch'])->name('attendances.punch');
    Route::get('my-attendance', [AttendanceController::class, 'myAttendance'])->name('attendances.mine');
    Route::get('team-attendance', [AttendanceController::class, 'teamAttendance'])->name('attendances.team');

    // Attendance Reports
    Route::prefix('reports/attendance')->name('reports.attendance.')->group(function () {
        Route::get('daily', [AttendanceReportController::class, 'daily'])->name('daily')->middleware('permission:reports.daily');
        Route::get('monthly', [AttendanceReportController::class, 'monthly'])->name('monthly')->middleware('permission:reports.monthly');
        Route::get('yearly', [AttendanceReportController::class, 'yearly'])->name('yearly');
        Route::get('issues', [AttendanceReportController::class, 'issues'])->name('issues')->middleware('permission:reports.daily');
        Route::get('issues/data', [AttendanceReportController::class, 'getIssuesData'])->name('issues.data')->middleware('permission:reports.daily');
        Route::get('issues/export', [AttendanceReportController::class, 'exportIssues'])->name('issues.export')->middleware('permission:reports.daily');
    });

    // Attendance Requests
    Route::prefix('attendance-requests')->name('attendance-requests.')->group(function () {
        Route::get('/data', [AttendanceRequestController::class, 'getData'])->name('data')->middleware('permission:attendance.view');
        Route::post('/{id}/approve', [AttendanceRequestController::class, 'approve'])->name('approve')->middleware('permission:attendance.manage_requests');
        Route::post('/{id}/reject', [AttendanceRequestController::class, 'reject'])->name('reject')->middleware('permission:attendance.manage_requests');
        Route::resource('/', AttendanceRequestController::class)->parameters(['' => 'attendance_request'])->middleware('permission:attendance.view');
    });

    // Leave Requests
    Route::prefix('leave-requests')->name('leave-requests.')->group(function () {
        Route::get('/data', [LeaveRequestController::class, 'getData'])->name('data')->middleware('permission:leaves.view');
        Route::post('/{id}/approve', [LeaveRequestController::class, 'approve'])->name('approve')->middleware('permission:leaves.manage');
        Route::post('/{id}/reject', [LeaveRequestController::class, 'reject'])->name('reject')->middleware('permission:leaves.manage');
        Route::resource('/', LeaveRequestController::class)->parameters(['' => 'leave_request'])->middleware('permission:leaves.view');
    });

    Route::get('holiday-calendar', [HolidayCalendarController::class, 'index'])->name('holidays.calendar')->middleware('permission:holidays.view');
    Route::get('holiday-calendar/events', [HolidayCalendarController::class, 'getEvents'])->name('holidays.events')->middleware('permission:holidays.view');

    Route::get('payslips/{id}', [PayslipController::class, 'show'])->name('payslips.show')->middleware('permission:payroll.view');
    Route::get('payslips/{id}/download', [PayslipController::class, 'download'])->name('payslips.download')->middleware('permission:payroll.export');

    // Expense Routes
    Route::prefix('expenses')->name('expenses.')->group(function () {
        Route::get('/data', [ExpenseController::class, 'getData'])->name('data')->middleware('permission:expenses.view');
        Route::get('/stats', [ExpenseController::class, 'getStats'])->name('stats')->middleware('permission:expenses.view');
        Route::resource('/', ExpenseController::class)->parameters(['' => 'expense'])->middleware('permission:expenses.view');
    });

    // Profile Routes
    Route::get('my-profile', [ProfileController::class, 'index'])->name('profile.index');
    Route::post('my-profile/update', [ProfileController::class, 'updateInfo'])->name('profile.update');
    Route::post('my-profile/password', [ProfileController::class, 'changePassword'])->name('profile.password');
    Route::post('my-profile/upload-document', [ProfileController::class, 'uploadDocument'])->name('profile.upload-document');

    // Approval Routes (Accessible by Admin and Managers)
    Route::prefix('approvals')->name('approvals.')->group(function () {
        Route::get('profile', [ProfileApprovalController::class, 'index'])->name('profile.index')->middleware('permission:employees.edit');
        Route::get('profile/data', [ProfileApprovalController::class, 'getProfileData'])->name('profile.data')->middleware('permission:employees.edit');
        Route::post('profile/{id}/approve', [ProfileApprovalController::class, 'approveProfile'])->name('profile.approve')->middleware('permission:employees.edit');
        Route::post('profile/{id}/reject', [ProfileApprovalController::class, 'rejectProfile'])->name('profile.reject')->middleware('permission:employees.edit');

        Route::get('documents', [ProfileApprovalController::class, 'documents'])->name('documents.index')->middleware('permission:employees.edit');
        Route::get('documents/data', [ProfileApprovalController::class, 'getDocumentData'])->name('documents.data')->middleware('permission:employees.edit');
        Route::post('documents/{id}/approve', [ProfileApprovalController::class, 'approveDocument'])->name('documents.approve')->middleware('permission:employees.edit');
        Route::post('documents/{id}/reject', [ProfileApprovalController::class, 'rejectDocument'])->name('documents.reject')->middleware('permission:employees.edit');
    });

    Route::get('employees/search', [EmployeeController::class, 'search'])->name('employees.search');
    Route::resource('attendances', AttendanceController::class)->middleware('permission:attendance.view');
    Route::post('attendances/{id}/approve', [AttendanceController::class, 'approve'])->name('attendances.approve')->middleware('permission:attendance.manage_requests');

    // Admin Only
    Route::middleware(['role:Admin'])->group(function () {
        Route::prefix('employees')->name('employees.')->group(function () {
            Route::get('/data', [EmployeeController::class, 'getData'])->name('data')->middleware('permission:employees.view');
            Route::get('/org-chart', [EmployeeController::class, 'getOrgChartData'])->name('org-chart')->middleware('permission:employees.org_chart');
            Route::get('/{id}/id-card', [EmployeeController::class, 'idCard'])->name('id-card')->middleware('permission:employees.id_card');
            Route::get('/{id}/id-card/download', [EmployeeController::class, 'downloadIdCard'])->name('id-card.download')->middleware('permission:employees.id_card');
            Route::resource('/', EmployeeController::class)->parameters(['' => 'employee'])->middleware('permission:employees.view');
            
            // Write actions
            Route::get('/create', [EmployeeController::class, 'create'])->name('create')->middleware('permission:employees.create');
            Route::post('/', [EmployeeController::class, 'store'])->name('store')->middleware('permission:employees.create');
            Route::get('/{employee}/edit', [EmployeeController::class, 'edit'])->name('edit')->middleware('permission:employees.edit');
            Route::put('/{employee}', [EmployeeController::class, 'update'])->name('update')->middleware('permission:employees.edit');
            Route::delete('/{employee}', [EmployeeController::class, 'destroy'])->name('destroy')->middleware('permission:employees.delete');
        });


        Route::prefix('salaries')->name('salaries.')->group(function () {
            Route::get('/processor', [\App\Http\Controllers\MonthlyPayrollController::class, 'editor'])->name('bulk.processor')->middleware('permission:payroll.process');
            Route::post('/processor/save', [\App\Http\Controllers\MonthlyPayrollController::class, 'saveBatch'])->name('bulk.save')->middleware('permission:payroll.process');
            Route::get('/data', [SalaryController::class, 'getData'])->name('data')->middleware('permission:payroll.view');
            Route::get('/report', [SalaryController::class, 'report'])->name('report')->middleware('permission:payroll.report');
            Route::get('/report/data', [SalaryController::class, 'reportData'])->name('report.data')->middleware('permission:payroll.report');
            Route::get('/export/excel', [SalaryController::class, 'exportExcel'])->name('export.excel')->middleware('permission:payroll.export');
            Route::get('/export/csv', [SalaryController::class, 'exportCSV'])->name('export.csv')->middleware('permission:payroll.export');
            Route::resource('/', SalaryController::class)->parameters(['' => 'salary'])->middleware('permission:payroll.view');
        });

        // Biometric Devices
        Route::prefix('biometric-devices')->name('biometric-devices.')->group(function () {
            Route::get('/data', [BiometricDeviceController::class, 'getData'])->name('data')->middleware('permission:biometric.view');
            Route::resource('/', BiometricDeviceController::class)->parameters(['' => 'biometric_device'])->middleware('permission:biometric.manage');
        });

        Route::prefix('assets')->name('assets.')->group(function () {
            Route::get('/data', [App\Http\Controllers\AssetController::class, 'getData'])->name('data')->middleware('permission:assets.view');
            Route::post('/{id}/assign', [App\Http\Controllers\AssetController::class, 'assign'])->name('assign')->middleware('permission:assets.manage');
            Route::post('/{id}/unassign', [App\Http\Controllers\AssetController::class, 'unassign'])->name('unassign')->middleware('permission:assets.manage');
            Route::resource('/', App\Http\Controllers\AssetController::class)->parameters(['' => 'asset'])->middleware('permission:assets.view');
        });

        Route::resource('deduction-types', DeductionTypeController::class)->middleware('permission:settings.statutory');
        Route::resource('addition-types', \App\Http\Controllers\AdditionTypeController::class)->middleware('permission:settings.statutory');
        
        // Settings Routes
        Route::prefix('settings')->name('settings.')->group(function () {
            Route::get('/', function() { return redirect()->route('settings.general'); }); 
            Route::get('/general', [BusinessSettingController::class, 'general'])->name('general')->middleware('permission:settings.general');
            Route::get('/theme', [BusinessSettingController::class, 'theme'])->name('theme')->middleware('permission:settings.theme');
            Route::get('/statutory', [BusinessSettingController::class, 'statutory'])->name('statutory')->middleware('permission:settings.statutory');
            Route::post('/update', [BusinessSettingController::class, 'update'])->name('update')->middleware('permission:settings.general');
        });

        Route::get('holidays/data', [HolidayController::class, 'getData'])->name('holidays.data')->middleware('permission:holidays.view');
        Route::resource('holidays', HolidayController::class)->middleware('permission:holidays.view');

        // Role Management
        Route::get('roles/data', [\App\Http\Controllers\RoleController::class, 'getData'])->name('roles.data')->middleware('permission:roles.manage');
        Route::resource('roles', \App\Http\Controllers\RoleController::class)->middleware('permission:roles.manage');

        // Advance Expense Actions
        Route::post('expenses/{id}/approve', [ExpenseController::class, 'approve'])->name('expenses.approve')->middleware('permission:expenses.manage');
        Route::post('expenses/{id}/reject', [ExpenseController::class, 'reject'])->name('expenses.reject')->middleware('permission:expenses.manage');
        Route::post('expenses/{id}/settle', [ExpenseController::class, 'settle'])->name('expenses.settle')->middleware('permission:expenses.manage');
    });
});
