<?php

use App\Http\Controllers\AttendanceController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\DocumentController;
use App\Http\Controllers\ImportController;
use App\Http\Controllers\PayrollController;
use App\Http\Controllers\RenewalController;
use App\Http\Controllers\SetupController;
use App\Http\Controllers\SiteController;
use App\Http\Controllers\StaffController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

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

// Auth
Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginController::class, 'show'])->name('login');
    Route::post('/login', [LoginController::class, 'login']);
});
Route::post('/logout', [LoginController::class, 'logout'])->middleware('auth')->name('logout');

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

    // Staff — everyone can view (own-only enforced for staff); manage restricted in controller.
    Route::get('/staff', [StaffController::class, 'index'])->name('staff.index');
    Route::get('/staff/create', [StaffController::class, 'create'])->name('staff.create');
    Route::post('/staff', [StaffController::class, 'store'])->name('staff.store');
    Route::get('/staff/{staff}', [StaffController::class, 'show'])->name('staff.show');
    Route::get('/staff/{staff}/photo', [StaffController::class, 'photo'])->name('staff.photo');
    Route::post('/staff/{staff}/reassign-site', [StaffController::class, 'reassignSite'])->name('staff.reassign');
    Route::get('/staff/{staff}/edit', [StaffController::class, 'edit'])->name('staff.edit');
    Route::put('/staff/{staff}', [StaffController::class, 'update'])->name('staff.update');
    Route::delete('/staff/{staff}', [StaffController::class, 'destroy'])->name('staff.destroy');

    // Documents (upload/download/delete)
    Route::post('/staff/{staff}/documents', [DocumentController::class, 'store'])->name('documents.store');
    Route::get('/documents/{document}/download', [DocumentController::class, 'download'])->name('documents.download');
    Route::delete('/documents/{document}', [DocumentController::class, 'destroy'])->name('documents.destroy');

    // Renewals (record against a staff)
    Route::post('/staff/{staff}/renewals', [RenewalController::class, 'store'])->name('renewals.store');
    Route::get('/renewals/{renewal}/download', [RenewalController::class, 'download'])->name('renewals.download');

    // Payslip self-service (staff) + shared single PDF
    Route::get('/my-payslips', [PayrollController::class, 'mine'])->name('payslips.mine');
    Route::get('/payslips/{payslip}/pdf', [PayrollController::class, 'pdf'])->name('payslips.pdf');

    // Attendance — admin, finance and supervisor (report + manual entry).
    Route::middleware('role:admin|finance|supervisor')->group(function () {
        Route::get('/attendance', [AttendanceController::class, 'index'])->name('attendance.index');
        Route::get('/attendance/summary', [AttendanceController::class, 'summary'])->name('attendance.summary');
        Route::post('/attendance', [AttendanceController::class, 'store'])->name('attendance.store');
    });

    // Supervisor/admin area
    Route::middleware('role:admin|supervisor')->group(function () {
        Route::resource('sites', SiteController::class)->except(['show']);
        Route::get('/sites/{site}', [SiteController::class, 'show'])->name('sites.show');
        Route::get('/renewals', [RenewalController::class, 'index'])->name('renewals.index');
        Route::get('/setup', [SetupController::class, 'index'])->name('setup.index');
        Route::post('/setup/{staff}/complete', [SetupController::class, 'complete'])->name('setup.complete');
    });

    // Finance/admin payroll
    Route::middleware('role:admin|finance')->group(function () {
        Route::get('/payroll', [PayrollController::class, 'index'])->name('payroll.index');
        Route::get('/payroll/create', [PayrollController::class, 'create'])->name('payroll.create');
        Route::post('/payroll', [PayrollController::class, 'store'])->name('payroll.store');
        Route::get('/payroll/batch-pdf', [PayrollController::class, 'batchPdf'])->name('payroll.batch');
        Route::get('/payroll/{payslip}', [PayrollController::class, 'show'])->name('payroll.show');
        Route::get('/payroll/{payslip}/edit', [PayrollController::class, 'edit'])->name('payroll.edit');
        Route::put('/payroll/{payslip}', [PayrollController::class, 'update'])->name('payroll.update');
        Route::post('/payroll/{payslip}/finalize', [PayrollController::class, 'finalize'])->name('payroll.finalize');
        Route::delete('/payroll/{payslip}', [PayrollController::class, 'destroy'])->name('payroll.destroy');
    });

    // Admin user management + masterlist import
    Route::middleware('role:admin')->group(function () {
        Route::resource('users', UserController::class)->except(['show']);
        Route::get('/import', [ImportController::class, 'show'])->name('import.show');
        Route::post('/import', [ImportController::class, 'store'])->name('import.store');
    });
});
