<?php

use App\Http\Controllers\AnalyticsController;
use App\Http\Controllers\AnnouncementController;
use App\Http\Controllers\ApplicationController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\CalendarController;
use App\Http\Controllers\CompanyController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\LeaveAllocationController;
use App\Http\Controllers\PendingChangeController;
use App\Http\Controllers\SelfServiceController;
use App\Http\Controllers\StaffController;
use App\Http\Controllers\StaffDetailController;
use App\Http\Controllers\TraineeController;
use App\Http\Controllers\TurnoverController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

Route::prefix('hr-portal')->group(function () {

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

    Route::get('/self-service/{token}', [SelfServiceController::class, 'show'])->name('hr.self-service.show');
    Route::post('/self-service/{token}/contact', [SelfServiceController::class, 'submitContactUpdate'])->name('hr.self-service.contact');
    Route::post('/self-service/{token}/spouse', [SelfServiceController::class, 'submitSpouse'])->name('hr.self-service.spouse');
    Route::post('/self-service/{token}/child', [SelfServiceController::class, 'submitChild'])->name('hr.self-service.child');

    Route::middleware('auth')->group(function () {
        Route::post('/logout', [AuthController::class, 'destroy'])->name('hr.logout');
        Route::get('/', fn () => redirect()->route('hr.dashboard'));
        Route::get('/dashboard', [DashboardController::class, 'index'])->name('hr.dashboard');

        Route::get('/companies', [CompanyController::class, 'index'])->name('hr.companies.index');
        Route::post('/companies', [CompanyController::class, 'store'])->name('hr.companies.store');

        Route::get('/staff', [StaffController::class, 'index'])->name('hr.staff.index');
        Route::get('/staff/{staff}', [StaffController::class, 'show'])->name('hr.staff.show');
        Route::post('/staff/{staff}/generate-token', [StaffController::class, 'generateSelfServiceToken'])->name('hr.staff.generate-token');

        Route::get('/applications', [ApplicationController::class, 'index'])->name('hr.applications.index');
        Route::post('/applications', [ApplicationController::class, 'store'])->name('hr.applications.store');
        Route::patch('/applications/{application}', [ApplicationController::class, 'update'])->name('hr.applications.update');

        Route::get('/pending-changes', [PendingChangeController::class, 'index'])->name('hr.pending-changes.index');
        Route::post('/pending-changes/{pendingChange}/approve', [PendingChangeController::class, 'approve'])->name('hr.pending-changes.approve');
        Route::post('/pending-changes/{pendingChange}/reject', [PendingChangeController::class, 'reject'])->name('hr.pending-changes.reject');

        Route::get('/users', [UserController::class, 'index'])->name('hr.users.index');
        Route::post('/users', [UserController::class, 'store'])->name('hr.users.store');
        Route::delete('/users/{user}', [UserController::class, 'destroy'])->name('hr.users.destroy');

        // Leave Allocation
        Route::get('/leave-allocation', [LeaveAllocationController::class, 'index'])->name('hr.leave-allocation.index');
        Route::post('/leave-allocation', [LeaveAllocationController::class, 'store'])->name('hr.leave-allocation.store');
        Route::post('/leave-allocation/bulk', [LeaveAllocationController::class, 'bulk'])->name('hr.leave-allocation.bulk');

        // Calendar
        Route::get('/calendar', [CalendarController::class, 'index'])->name('hr.calendar.index');
        Route::post('/calendar', [CalendarController::class, 'store'])->name('hr.calendar.store');
        Route::patch('/calendar/{calendarEvent}', [CalendarController::class, 'update'])->name('hr.calendar.update');
        Route::delete('/calendar/{calendarEvent}', [CalendarController::class, 'destroy'])->name('hr.calendar.destroy');

        // Announcements
        Route::get('/announcements', [AnnouncementController::class, 'index'])->name('hr.announcements.index');
        Route::post('/announcements', [AnnouncementController::class, 'store'])->name('hr.announcements.store');
        Route::patch('/announcements/{announcement}', [AnnouncementController::class, 'update'])->name('hr.announcements.update');
        Route::delete('/announcements/{announcement}', [AnnouncementController::class, 'destroy'])->name('hr.announcements.destroy');

        // Trainees, Analytics, Turnover
        Route::get('/trainees', [TraineeController::class, 'index'])->name('hr.trainees.index');
        Route::get('/analytics', [AnalyticsController::class, 'index'])->name('hr.analytics.index');
        Route::get('/turnover', [TurnoverController::class, 'index'])->name('hr.turnover.index');

        // Staff sub-resources
        Route::post('/staff/{staff}/employment-history', [StaffDetailController::class, 'storeEmploymentHistory'])->name('hr.staff.employment-history.store');
        Route::delete('/staff/{staff}/employment-history/{record}', [StaffDetailController::class, 'destroyEmploymentHistory'])->name('hr.staff.employment-history.destroy');
        Route::post('/staff/{staff}/training', [StaffDetailController::class, 'storeTraining'])->name('hr.staff.training.store');
        Route::delete('/staff/{staff}/training/{record}', [StaffDetailController::class, 'destroyTraining'])->name('hr.staff.training.destroy');
        Route::post('/staff/{staff}/medical-history', [StaffDetailController::class, 'storeMedicalHistory'])->name('hr.staff.medical-history.store');
        Route::delete('/staff/{staff}/medical-history/{record}', [StaffDetailController::class, 'destroyMedicalHistory'])->name('hr.staff.medical-history.destroy');
        Route::post('/staff/{staff}/medical-declaration', [StaffDetailController::class, 'storeMedicalDeclaration'])->name('hr.staff.medical-declaration.store');
        Route::post('/staff/{staff}/addresses', [StaffDetailController::class, 'storeAddress'])->name('hr.staff.addresses.store');
        Route::delete('/staff/{staff}/addresses/{address}', [StaffDetailController::class, 'destroyAddress'])->name('hr.staff.addresses.destroy');
        Route::post('/staff/{staff}/phones', [StaffDetailController::class, 'storePhone'])->name('hr.staff.phones.store');
        Route::delete('/staff/{staff}/phones/{phone}', [StaffDetailController::class, 'destroyPhone'])->name('hr.staff.phones.destroy');
        Route::post('/staff/{staff}/separation', [StaffDetailController::class, 'storeSeparation'])->name('hr.staff.separation.store');
    });

});
