<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\DepartmentController;
use App\Http\Controllers\DesignationController;
use App\Http\Controllers\EmployeeController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Middleware\CheckAdminRole;

// Public Auth Routes
Route::get('/', function () {
    return redirect()->route('login');
});
Route::get('/login', function () {
    return view('frontend.auth.login');
})->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::post('/get-user-by-phone', [AuthController::class, 'getUserByPhone']);

Route::get('/register', function () {
    return view('frontend.auth.register');
});
Route::post('/register', [AuthController::class, 'register']);

Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// Protected Admin Routes (Requires Login AND role_id = 1)
Route::prefix('admin')->middleware(['auth', CheckAdminRole::class])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('admin.dashboard');
    Route::post('/switch-company', [\App\Http\Controllers\Admin\ContextController::class, 'switchCompany'])->name('admin.switch_company');

    // Departments
    Route::get('departments', [DepartmentController::class, 'index'])->name('departments.index');
    Route::get('departments/create', [DepartmentController::class, 'create'])->name('departments.create');
    Route::post('departments', [DepartmentController::class, 'store'])->name('departments.store');
    Route::get('departments/{department}', [DepartmentController::class, 'show'])->name('departments.show');
    Route::get('departments/{department}/edit', [DepartmentController::class, 'edit'])->name('departments.edit');
    Route::put('departments/{department}', [DepartmentController::class, 'update'])->name('departments.update');
    Route::delete('departments/{department}', [DepartmentController::class, 'destroy'])->name('departments.destroy');

    // Policies
    Route::get('policies', [\App\Http\Controllers\Admin\PolicyController::class, 'index'])->name('policies.index');
    Route::get('policies/create', [\App\Http\Controllers\Admin\PolicyController::class, 'create'])->name('policies.create');
    Route::post('policies', [\App\Http\Controllers\Admin\PolicyController::class, 'store'])->name('policies.store');
    Route::get('policies/{policy}/edit', [\App\Http\Controllers\Admin\PolicyController::class, 'edit'])->name('policies.edit');
    Route::put('policies/{policy}', [\App\Http\Controllers\Admin\PolicyController::class, 'update'])->name('policies.update');
    Route::delete('policies/{policy}', [\App\Http\Controllers\Admin\PolicyController::class, 'destroy'])->name('policies.destroy');

    // Shifts
    Route::get('shifts', [\App\Http\Controllers\Admin\ShiftController::class, 'index'])->name('shifts.index');
    Route::get('shifts/create', [\App\Http\Controllers\Admin\ShiftController::class, 'create'])->name('shifts.create');
    Route::post('shifts', [\App\Http\Controllers\Admin\ShiftController::class, 'store'])->name('shifts.store');
    Route::get('shifts/{shift}/edit', [\App\Http\Controllers\Admin\ShiftController::class, 'edit'])->name('shifts.edit');
    Route::put('shifts/{shift}', [\App\Http\Controllers\Admin\ShiftController::class, 'update'])->name('shifts.update');
    Route::delete('shifts/{shift}', [\App\Http\Controllers\Admin\ShiftController::class, 'destroy'])->name('shifts.destroy');

    // Designations
    Route::get('designations', [\App\Http\Controllers\Admin\DesignationController::class, 'index'])->name('designations.index');
    Route::get('designations/create', [\App\Http\Controllers\Admin\DesignationController::class, 'create'])->name('designations.create');
    Route::post('designations', [\App\Http\Controllers\Admin\DesignationController::class, 'store'])->name('designations.store');
    Route::get('designations/{designation}/edit', [\App\Http\Controllers\Admin\DesignationController::class, 'edit'])->name('designations.edit');
    Route::put('designations/{designation}', [\App\Http\Controllers\Admin\DesignationController::class, 'update'])->name('designations.update');
    Route::delete('designations/{designation}', [\App\Http\Controllers\Admin\DesignationController::class, 'destroy'])->name('designations.destroy');

    // Employees
    Route::get('employees', [\App\Http\Controllers\Admin\EmployeeController::class, 'index'])->name('employees.index');
    Route::get('employees/create', [\App\Http\Controllers\Admin\EmployeeController::class, 'create'])->name('employees.create');
    Route::post('employees', [\App\Http\Controllers\Admin\EmployeeController::class, 'store'])->name('employees.store');
    Route::get('employees/{employee}/edit', [\App\Http\Controllers\Admin\EmployeeController::class, 'edit'])->name('employees.edit');
    Route::get('employees/{employee}/salary-setup', [\App\Http\Controllers\Admin\EmployeeController::class, 'salarySetup'])->name('employees.salarySetup');
    Route::post('employees/{employee}/salary-setup', [\App\Http\Controllers\Admin\EmployeeController::class, 'storeSalarySetup'])->name('employees.storeSalarySetup');
    Route::put('employees/{employee}', [\App\Http\Controllers\Admin\EmployeeController::class, 'update'])->name('employees.update');
    Route::delete('employees/{employee}', [\App\Http\Controllers\Admin\EmployeeController::class, 'destroy'])->name('employees.destroy');

    // Machines
    Route::get('machines', [\App\Http\Controllers\Admin\MachineController::class, 'index'])->name('machines.index');
    // Company
    Route::get('company', [\App\Http\Controllers\Admin\CompanyController::class, 'index'])->name('company.index');
    Route::get('company/create', [\App\Http\Controllers\Admin\CompanyController::class, 'create'])->name('company.create');
    Route::post('company', [\App\Http\Controllers\Admin\CompanyController::class, 'store'])->name('company.store');
    Route::get('company/{company}/edit', [\App\Http\Controllers\Admin\CompanyController::class, 'edit'])->name('company.edit');
    Route::put('company/{company}', [\App\Http\Controllers\Admin\CompanyController::class, 'update'])->name('company.update');
    Route::delete('company/{company}', [\App\Http\Controllers\Admin\CompanyController::class, 'destroy'])->name('company.destroy');

    // Branch
    Route::get('branch', [\App\Http\Controllers\Admin\BranchController::class, 'index'])->name('branch.index');
    Route::get('branch/create', [\App\Http\Controllers\Admin\BranchController::class, 'create'])->name('branch.create');
    Route::post('branch', [\App\Http\Controllers\Admin\BranchController::class, 'store'])->name('branch.store');
    Route::get('branch/{branch}/edit', [\App\Http\Controllers\Admin\BranchController::class, 'edit'])->name('branch.edit');
    Route::put('branch/{branch}', [\App\Http\Controllers\Admin\BranchController::class, 'update'])->name('branch.update');
    Route::delete('branch/{branch}', [\App\Http\Controllers\Admin\BranchController::class, 'destroy'])->name('branch.destroy');
    Route::get('machines/create', [\App\Http\Controllers\Admin\MachineController::class, 'create'])->name('machines.create');
    Route::post('machines', [\App\Http\Controllers\Admin\MachineController::class, 'store'])->name('machines.store');
    Route::get('machines/{machine}', [\App\Http\Controllers\Admin\MachineController::class, 'show'])->name('machines.show');
    Route::get('machines/{machine}/edit', [\App\Http\Controllers\Admin\MachineController::class, 'edit'])->name('machines.edit');
    Route::put('machines/{machine}', [\App\Http\Controllers\Admin\MachineController::class, 'update'])->name('machines.update');
    Route::delete('machines/{machine}', [\App\Http\Controllers\Admin\MachineController::class, 'destroy'])->name('machines.destroy');

    // Hardware Setup
    Route::get('hardware-setup', [\App\Http\Controllers\Admin\HardwareSetupController::class, 'selection'])->name('hardware_setup.selection');
    Route::get('hardware-setup/manager', [\App\Http\Controllers\Admin\HardwareSetupController::class, 'manager'])->name('hardware_setup.manager');

    // Leave Types
    Route::get('leave-types', [\App\Http\Controllers\Admin\LeaveTypeController::class, 'index'])->name('leave-types.index');
    Route::get('leave-types/create', [\App\Http\Controllers\Admin\LeaveTypeController::class, 'create'])->name('leave-types.create');
    Route::post('leave-types', [\App\Http\Controllers\Admin\LeaveTypeController::class, 'store'])->name('leave-types.store');
    Route::get('leave-types/{leaveType}/edit', [\App\Http\Controllers\Admin\LeaveTypeController::class, 'edit'])->name('leave-types.edit');
    Route::put('leave-types/{leaveType}', [\App\Http\Controllers\Admin\LeaveTypeController::class, 'update'])->name('leave-types.update');
    Route::delete('leave-types/{leaveType}', [\App\Http\Controllers\Admin\LeaveTypeController::class, 'destroy'])->name('leave-types.destroy');

    // Holidays
    Route::get('holidays', [\App\Http\Controllers\Admin\HolidayController::class, 'index'])->name('holidays.index');
    Route::get('holidays/create', [\App\Http\Controllers\Admin\HolidayController::class, 'create'])->name('holidays.create');
    Route::post('holidays', [\App\Http\Controllers\Admin\HolidayController::class, 'store'])->name('holidays.store');
    Route::get('holidays/{holiday}/edit', [\App\Http\Controllers\Admin\HolidayController::class, 'edit'])->name('holidays.edit');
    Route::put('holidays/{holiday}', [\App\Http\Controllers\Admin\HolidayController::class, 'update'])->name('holidays.update');
    Route::delete('holidays/{holiday}', [\App\Http\Controllers\Admin\HolidayController::class, 'destroy'])->name('holidays.destroy');

    // Manual Punch
    Route::get('manual-punch', [\App\Http\Controllers\Admin\ManualPunchController::class, 'index'])->name('manual_punch.index');
    Route::post('manual-punch', [\App\Http\Controllers\Admin\ManualPunchController::class, 'store'])->name('manual_punch.store');
    Route::post('manual-punch/view', [\App\Http\Controllers\Admin\ManualPunchController::class, 'viewPunches'])->name('manual_punch.view');

});
