<?php

use App\Http\Controllers\Admin\HrAdminController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\PublicCareerController;
use App\Http\Controllers\User\PortalController;
use Illuminate\Support\Facades\Route;

Route::get('/', fn () => auth()->check()
    ? redirect()->route(auth()->user()->isAdmin() ? 'admin.dashboard' : 'user.dashboard')
    : redirect()->route('login'));

Route::get('/careers', [PublicCareerController::class, 'index'])->name('careers.index');
Route::get('/careers/{job}', [PublicCareerController::class, 'show'])->name('careers.show');
Route::post('/careers/{job}/apply', [PublicCareerController::class, 'apply'])->name('careers.apply');

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

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

Route::middleware(['auth', 'admin'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [HrAdminController::class, 'dashboard'])->name('dashboard');
    Route::get('/employees', [HrAdminController::class, 'employees'])->name('employees');
    Route::post('/employees', [HrAdminController::class, 'storeEmployee'])->name('employees.store');
    Route::get('/employees/{employee}', [HrAdminController::class, 'showEmployee'])->name('employees.show');
    Route::put('/employees/{employee}', [HrAdminController::class, 'updateEmployee'])->name('employees.update');

    Route::get('/departments', [HrAdminController::class, 'departments'])->name('departments');
    Route::post('/departments', [HrAdminController::class, 'storeDepartment'])->name('departments.store');
    Route::put('/departments/{department}', [HrAdminController::class, 'updateDepartment'])->name('departments.update');

    Route::get('/designations', [HrAdminController::class, 'designations'])->name('designations');
    Route::post('/designations', [HrAdminController::class, 'storeDesignation'])->name('designations.store');
    Route::put('/designations/{designation}', [HrAdminController::class, 'updateDesignation'])->name('designations.update');

    Route::get('/attendance', [HrAdminController::class, 'attendance'])->name('attendance');
    Route::post('/attendance', [HrAdminController::class, 'storeAttendance'])->name('attendance.store');

    Route::get('/leaves', [HrAdminController::class, 'leaves'])->name('leaves');
    Route::post('/leave-types', [HrAdminController::class, 'storeLeaveType'])->name('leave-types.store');
    Route::put('/leaves/{leave}', [HrAdminController::class, 'reviewLeave'])->name('leaves.review');

    Route::get('/payrolls', [HrAdminController::class, 'payrolls'])->name('payrolls');
    Route::post('/payrolls', [HrAdminController::class, 'storePayroll'])->name('payrolls.store');
    Route::put('/payrolls/{payroll}', [HrAdminController::class, 'updatePayroll'])->name('payrolls.update');

    Route::get('/documents', [HrAdminController::class, 'documents'])->name('documents');
    Route::post('/documents', [HrAdminController::class, 'storeDocument'])->name('documents.store');

    Route::get('/performance', [HrAdminController::class, 'performance'])->name('performance');
    Route::post('/performance', [HrAdminController::class, 'storePerformance'])->name('performance.store');

    Route::get('/announcements', [HrAdminController::class, 'announcements'])->name('announcements');
    Route::post('/announcements', [HrAdminController::class, 'storeAnnouncement'])->name('announcements.store');

    Route::get('/jobs', [HrAdminController::class, 'jobs'])->name('jobs');
    Route::post('/jobs', [HrAdminController::class, 'storeJob'])->name('jobs.store');
    Route::get('/applications', [HrAdminController::class, 'applications'])->name('applications');
    Route::put('/applications/{application}', [HrAdminController::class, 'reviewApplication'])->name('applications.review');

    Route::get('/trainings', [HrAdminController::class, 'trainings'])->name('trainings');
    Route::post('/trainings', [HrAdminController::class, 'storeTraining'])->name('trainings.store');
    Route::post('/trainings/{training}/enroll', [HrAdminController::class, 'enrollTraining'])->name('trainings.enroll');

    Route::get('/tickets', [HrAdminController::class, 'tickets'])->name('tickets');
    Route::put('/tickets/{ticket}', [HrAdminController::class, 'updateTicket'])->name('tickets.update');

    Route::get('/discipline', [HrAdminController::class, 'discipline'])->name('discipline');
    Route::post('/discipline', [HrAdminController::class, 'storeDiscipline'])->name('discipline.store');
});

Route::middleware('auth')->prefix('portal')->name('user.')->group(function () {
    Route::get('/dashboard', [PortalController::class, 'dashboard'])->name('dashboard');
    Route::get('/profile', [PortalController::class, 'profile'])->name('profile');
    Route::put('/profile', [PortalController::class, 'updateProfile'])->name('profile.update');
    Route::get('/attendance', [PortalController::class, 'attendance'])->name('attendance');
    Route::post('/attendance/clock-in', [PortalController::class, 'clockIn'])->name('attendance.clock-in');
    Route::post('/attendance/clock-out', [PortalController::class, 'clockOut'])->name('attendance.clock-out');
    Route::get('/leaves', [PortalController::class, 'leaves'])->name('leaves');
    Route::post('/leaves', [PortalController::class, 'storeLeave'])->name('leaves.store');
    Route::get('/payslips', [PortalController::class, 'payslips'])->name('payslips');
    Route::get('/documents', [PortalController::class, 'documents'])->name('documents');
    Route::get('/announcements', [PortalController::class, 'announcements'])->name('announcements');
    Route::get('/performance', [PortalController::class, 'performance'])->name('performance');
    Route::get('/trainings', [PortalController::class, 'trainings'])->name('trainings');
    Route::post('/trainings/{training}/enroll', [PortalController::class, 'enrollTraining'])->name('trainings.enroll');
    Route::get('/tickets', [PortalController::class, 'tickets'])->name('tickets');
    Route::post('/tickets', [PortalController::class, 'storeTicket'])->name('tickets.store');
});
