<?php

use App\Http\Controllers\AppraisalController;
use App\Http\Controllers\AppraisalPeriodController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\DepartmentController;
use App\Http\Controllers\DevelopmentPlanController;
use App\Http\Controllers\FeedbackController;
use App\Http\Controllers\KpiLibraryController;
use App\Http\Controllers\KraController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\StaffController;
use Illuminate\Support\Facades\Route;

// ── Guest Routes ───────────────────────────────────
Route::middleware('guest')->group(function () {
    Route::get('/', fn() => redirect()->route('login'));
    Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [LoginController::class, 'login']);
});

// ── Authenticated Routes ───────────────────────────
Route::middleware('auth')->group(function () {
    Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

    // Dashboard
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Appraisals (accessible to all authenticated users)
    Route::get('appraisals/{appraisal}/print', [AppraisalController::class, 'print'])->name('appraisals.print');
    Route::resource('appraisals', AppraisalController::class)->except(['destroy']);
    Route::delete('appraisals/{appraisal}/goals/{goal}', [AppraisalController::class, 'deleteGoal'])->name('appraisals.goals.delete');

    // Feedback
    Route::resource('feedback', FeedbackController::class)->only(['index', 'create', 'store']);

    // Development Plans
    Route::resource('development-plans', DevelopmentPlanController::class);

    // ── Admin & HOD Routes ─────────────────────────
    Route::middleware('role:admin,director,department_head')->group(function () {
        // Staff Management
        Route::resource('staff', StaffController::class);

        // Departments
        Route::resource('departments', DepartmentController::class);

        // Appraisal Periods
        Route::resource('appraisal-periods', AppraisalPeriodController::class)->except(['show']);

        // KRA Management  
        Route::resource('kras', KraController::class)->except(['show']);

        // KPI Library
        Route::resource('kpi-library', KpiLibraryController::class)->except(['show']);

        // Reports
        Route::get('/reports', [ReportController::class, 'index'])->name('reports.index');
        Route::get('/reports/department', [ReportController::class, 'departmentReport'])->name('reports.department');
        Route::get('/reports/individual', [ReportController::class, 'individualReport'])->name('reports.individual');
    });
});
