<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\EmployeeController;
use App\Http\Controllers\LeaveController;
use App\Http\Controllers\PerformanceController;
use App\Http\Controllers\DisciplineController;
use App\Http\Controllers\AttendanceController;
use App\Http\Controllers\PayrollController;
use App\Http\Controllers\TrainingController;

use App\Http\Controllers\ThemeController;

// Public splash — must be registered BEFORE Auth::routes() so Auth scaffolding
// cannot override the '/' path. Guests see the marketing page; authenticated
// users are redirected to the dashboard.
Route::get('/', function () {
    return auth()->check()
        ? redirect()->route('dashboard')
        : view('welcome');
})->name('welcome');

// Auth routes (login / logout only — registration disabled)
Auth::routes(['register' => false]);

// Authenticated application
Route::middleware('auth')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Theme preference
    Route::post('/theme',   [ThemeController::class, 'update'])->name('theme.update');

    // Employee Profile (self)
    Route::get('/profile',        [EmployeeController::class, 'editProfile'])->name('profile');
    Route::put('/profile',        [EmployeeController::class, 'updateProfile'])->name('profile.update');

    // Employees (HR admin)
    Route::resource('employees', EmployeeController::class);
    Route::post('employees/{employee}/documents', [EmployeeController::class, 'uploadDocument'])->name('employees.upload_document');

    // Leaves
    Route::resource('leaves', LeaveController::class)->except(['edit','update','destroy']);
    Route::post('leaves/{leave}/approve-dept',      [LeaveController::class, 'approveDept'])->name('leaves.approve_dept');
    Route::post('leaves/{leave}/approve-hr',        [LeaveController::class, 'approveHR'])->name('leaves.approve_hr');
    Route::post('leaves/{leave}/approve-president', [LeaveController::class, 'approvePresident'])->name('leaves.approve_president');
    Route::post('leaves/{leave}/reject',            [LeaveController::class, 'reject'])->name('leaves.reject');
    Route::post('leaves/{leave}/cancel',            [LeaveController::class, 'cancel'])->name('leaves.cancel');

    // Performance
    Route::resource('performance', PerformanceController::class)->except(['edit','update','destroy']);
    Route::post('performance/{performance}/acknowledge', [PerformanceController::class, 'acknowledge'])->name('performance.acknowledge');
    Route::post('performance/{performance}/hr-review',   [PerformanceController::class, 'hrReview'])->name('performance.hr_review');

    // Discipline
    Route::resource('discipline', DisciplineController::class)->except(['destroy']);
    Route::post('discipline/{discipline}/action', [DisciplineController::class, 'takeAction'])->name('discipline.action');
    Route::post('discipline/{discipline}/close',  [DisciplineController::class, 'close'])->name('discipline.close');

    // Attendance
    Route::get('attendance',             [AttendanceController::class, 'index'])->name('attendance.index');
    Route::get('attendance/summary',     [AttendanceController::class, 'summary'])->name('attendance.summary');
    Route::post('attendance/bulk',       [AttendanceController::class, 'bulkStore'])->name('attendance.bulk');
    Route::post('attendance/clock',      [AttendanceController::class, 'markToday'])->name('attendance.clock');

    // Payroll
    Route::get('payroll',                [PayrollController::class, 'index'])->name('payroll.index');
    Route::get('payroll/create',         [PayrollController::class, 'create'])->name('payroll.create');
    Route::post('payroll/generate',      [PayrollController::class, 'generate'])->name('payroll.generate');
    Route::get('payroll/{payroll}',      [PayrollController::class, 'show'])->name('payroll.show');
    Route::post('payroll/{payroll}/approve', [PayrollController::class, 'approve'])->name('payroll.approve');
    Route::get('payroll/payslip/{record}',   [PayrollController::class, 'payslip'])->name('payroll.payslip');

    // Training
    Route::resource('training', TrainingController::class)->except(['edit','update','destroy']);
    Route::post('training/{training}/enroll',       [TrainingController::class, 'enroll'])->name('training.enroll');
    Route::post('training/{training}/self-enroll',  [TrainingController::class, 'selfEnroll'])->name('training.self_enroll');
    Route::post('training/{training}/complete/{employee}', [TrainingController::class, 'complete'])->name('training.complete');
