<?php

use App\Http\Controllers\Admin\AuditLogController;
use App\Http\Controllers\Admin\BugController;
use App\Http\Controllers\Admin\CeoDashboardController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\EmployeeController;
use App\Http\Controllers\Admin\GamificationController;
use App\Http\Controllers\Admin\KnowledgeBaseController;
use App\Http\Controllers\Admin\LiveTrackingController;
use App\Http\Controllers\Admin\NotificationEngineController;
use App\Http\Controllers\Admin\NotificationFeedController;
use App\Http\Controllers\Admin\ProjectController;
use App\Http\Controllers\Admin\ProjectModuleController;
use App\Http\Controllers\Admin\QaDashboardController;
use App\Http\Controllers\Admin\QueueController;
use App\Http\Controllers\Admin\ReportingController;
use App\Http\Controllers\Admin\RoleController;
use App\Http\Controllers\Admin\SettingController;
use App\Http\Controllers\Admin\SmartWorkflowController;
use App\Http\Controllers\Admin\TaskBoardController;
use App\Http\Controllers\Admin\TaskController;
use App\Http\Controllers\Admin\TimeTrackingController;
use App\Http\Controllers\Auth\AuthenticatedSessionController;
use Illuminate\Support\Facades\Route;

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

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

Route::middleware('auth')->group(function (): void {
    Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])->name('logout');
    Route::get('/notifications/unread', [NotificationFeedController::class, 'unread'])->name('notifications.unread');
    Route::patch('/notifications/{notification}/read', [NotificationFeedController::class, 'markRead'])->name('notifications.read');

    // --- GOVERNANCE & SETTINGS (Super Admin Only) ---
    Route::middleware('role:super_admin')->group(function (): void {
        Route::get('/settings', [SettingController::class, 'index'])->name('settings.index');
        Route::post('/settings', [SettingController::class, 'update'])->name('settings.update');
        Route::get('/security/roles', [RoleController::class, 'index'])->name('security.roles.index');
        Route::post('/security/roles', [RoleController::class, 'store'])->name('security.roles.store');
        Route::get('/security/audit-logs', [AuditLogController::class, 'index'])->name('security.audit-logs');
        Route::get('/admin/ceo', [CeoDashboardController::class, 'index'])->name('admin.ceo');
        Route::get('/admin/gamification/settings', [GamificationController::class, 'settings'])->name('gamification.settings');
        Route::post('/admin/gamification/settings', [GamificationController::class, 'saveSettings'])->name('gamification.settings.save');
        Route::post('/admin/gamification/badges', [GamificationController::class, 'saveBadge'])->name('gamification.badges.save');
        Route::post('/admin/gamification/calculate', [GamificationController::class, 'calculate'])->name('gamification.calculate');
    });

    // --- MANAGEMENT & HR (Super Admin, HR, PM) ---
    Route::middleware('role:super_admin,hr,project_manager')->group(function (): void {
        Route::get('/admin/dashboard', [DashboardController::class, 'admin'])->name('admin.dashboard');
        Route::resource('/employees', EmployeeController::class)->except(['show']);
        Route::resource('/projects', ProjectController::class)->except(['show']);
        Route::get('/admin/reports', [ReportingController::class, 'dashboard'])->name('reports.dashboard');
        Route::post('/admin/reports/generate', [ReportingController::class, 'generate'])->name('reports.generate');
        Route::get('/admin/reports/{snapshot}/export/csv', [ReportingController::class, 'exportCsv'])->name('reports.export.csv');
        Route::get('/admin/reports/{snapshot}/export/pdf', [ReportingController::class, 'exportPdf'])->name('reports.export.pdf');
        Route::get('/admin/reports/{snapshot}/whatsapp', [ReportingController::class, 'whatsapp'])->name('reports.whatsapp');
        Route::get('/admin/workflow', [SmartWorkflowController::class, 'dashboard'])->name('workflow.dashboard');
        Route::post('/admin/workflow/rules', [SmartWorkflowController::class, 'saveRule'])->name('workflow.rules.store');
        Route::post('/admin/workflow/auto-assign', [SmartWorkflowController::class, 'runAutoAssign'])->name('workflow.auto-assign');
        Route::post('/admin/workflow/predictions', [SmartWorkflowController::class, 'runPredictions'])->name('workflow.predictions');
        Route::post('/admin/workflow/smart-queue', [SmartWorkflowController::class, 'runSmartQueue'])->name('workflow.smart-queue');
        Route::post('/admin/workflow/recommendations/{recommendation}/apply', [SmartWorkflowController::class, 'applyRecommendation'])->name('workflow.recommendations.apply');
        Route::get('/admin/time-tracking', [TimeTrackingController::class, 'adminDashboard'])->name('admin.time-tracking.dashboard');
    });

    // --- OPERATIONS (All Staff) ---
    Route::middleware('role:super_admin,hr,project_manager,developer,tester,designer')->group(function (): void {
        Route::get('/live-tracking', [LiveTrackingController::class, 'index'])->name('live-tracking');

        Route::post('/projects/{project}/modules', [ProjectModuleController::class, 'store'])->name('projects.modules.store');
        Route::put('/project-modules/{module}', [ProjectModuleController::class, 'update'])->name('project-modules.update');
        Route::delete('/project-modules/{module}', [ProjectModuleController::class, 'destroy'])->name('project-modules.destroy');

        // Task Intelligence Group
        Route::prefix('tasks')->group(function () {
            Route::get('/kanban', [TaskController::class, 'kanban'])->name('tasks.kanban');
            Route::get('/timeline', [TaskController::class, 'timeline'])->name('tasks.timeline');
            Route::post('/{task}/subtasks', [TaskController::class, 'storeSubtask'])->name('tasks.subtasks.store');
            Route::post('/{task}/comments/api', [TaskController::class, 'apiAddComment'])->name('tasks.comments.api');
            Route::patch('/{task}/status', [TaskController::class, 'updateStatus'])->name('tasks.status.update');
        });
        Route::resource('/tasks', TaskController::class);

        // Timer Engine
        Route::prefix('time-tracking')->group(function () {
            Route::post('/start', [TimeTrackingController::class, 'start'])->name('timer.start');
            Route::post('/{timeLog}/pause', [TimeTrackingController::class, 'pause'])->name('timer.pause');
            Route::post('/{timeLog}/resume', [TimeTrackingController::class, 'resume'])->name('timer.resume');
            Route::post('/{timeLog}/stop', [TimeTrackingController::class, 'stop'])->name('timer.stop');
        });

        Route::resource('/bugs', BugController::class);
        Route::post('/bugs/{bug}/approve', [BugController::class, 'approve'])->name('bugs.approve');
        Route::post('/bugs/{bug}/reject', [BugController::class, 'reject'])->name('bugs.reject');
        Route::post('/bugs/{bug}/reopen', [BugController::class, 'reopen'])->name('bugs.reopen');
        Route::post('/bugs/{bug}/escalate', [BugController::class, 'escalate'])->name('bugs.escalate');
        Route::post('/bugs/{bug}/convert-to-task', [BugController::class, 'convertToTask'])->name('bugs.convert-to-task');
        Route::get('/admin/qa', [QaDashboardController::class, 'index'])->name('qa.dashboard');
        Route::post('/admin/qa/reports', [QaDashboardController::class, 'storeReport'])->name('qa.reports.store');

        Route::get('/admin/gamification/leaderboard', [GamificationController::class, 'leaderboard'])->name('gamification.leaderboard');
        Route::get('/admin/gamification/reports', [GamificationController::class, 'reports'])->name('gamification.reports');

        Route::resource('/knowledge', KnowledgeBaseController::class)->except(['show']);
        Route::post('/knowledge/auto-generate', [KnowledgeBaseController::class, 'autoGenerate'])->name('knowledge.auto-generate');
        Route::post('/knowledge/{knowledge}/restore/{auditId}', [KnowledgeBaseController::class, 'restore'])->name('knowledge.restore');
        Route::get('/knowledge/architecture/logs', [KnowledgeBaseController::class, 'architecture'])->name('knowledge.architecture');
        Route::post('/knowledge/architecture/logs', [KnowledgeBaseController::class, 'storeArchitecture'])->name('knowledge.architecture.store');

        Route::get('/admin/notifications', [NotificationEngineController::class, 'dashboard'])->name('notification-engine.dashboard');
        Route::post('/admin/notifications/templates', [NotificationEngineController::class, 'saveTemplate'])->name('notification-engine.templates.store');
        Route::post('/admin/notifications/rules', [NotificationEngineController::class, 'saveRule'])->name('notification-engine.rules.store');
        Route::post('/admin/notifications/test', [NotificationEngineController::class, 'sendTest'])->name('notification-engine.test.send');

        Route::get('/queue', [QueueController::class, 'index'])->name('queue.index');
        Route::post('/queue/retry/{id}', [QueueController::class, 'retry'])->name('queue.retry');
        Route::post('/queue/clear', [QueueController::class, 'clear'])->name('queue.clear');
    });

    Route::middleware('role:employee,super_admin,hr,project_manager,developer,tester,designer')->group(function (): void {
        Route::get('/employee/dashboard', [DashboardController::class, 'employee'])->name('employee.dashboard');
        Route::get('/employee/time-tracking', [TimeTrackingController::class, 'employeeDashboard'])->name('employee.time-tracking.dashboard');

        Route::post('/time-tracking/start', [TimeTrackingController::class, 'start'])->name('time-tracking.start');
        Route::patch('/time-tracking/{timeLog}/pause', [TimeTrackingController::class, 'pause'])->name('time-tracking.pause');
        Route::patch('/time-tracking/{timeLog}/resume', [TimeTrackingController::class, 'resume'])->name('time-tracking.resume');
        Route::patch('/time-tracking/{timeLog}/stop', [TimeTrackingController::class, 'stop'])->name('time-tracking.stop');
        Route::post('/time-tracking/idle', [TimeTrackingController::class, 'idle'])->name('time-tracking.idle');
        Route::post('/time-tracking/{timeLog}/note', [TimeTrackingController::class, 'manualNote'])->name('time-tracking.note');
    });
});
