<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\EmployeeController;
use App\Http\Controllers\AccessCatalogController;
use App\Http\Controllers\EmployeeAccessController;
use App\Http\Controllers\ExtensionRequestController;
use App\Http\Controllers\RevocationController;
use App\Http\Controllers\ReminderLogController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\TelegramWebhookController;
use App\Http\Controllers\SearchController;

/*
|--------------------------------------------------------------------------
| Public Routes
|--------------------------------------------------------------------------
*/

Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login']);
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

// Telegram Webhook (public)
Route::post('/telegram/webhook', [TelegramWebhookController::class, 'handle']);

/*
|--------------------------------------------------------------------------
| Protected Routes (Auth Required)
|--------------------------------------------------------------------------
*/

Route::middleware(['auth'])->group(function () {
    
    /*
    |--------------------------------------------------------------------------
    | Dashboard & Search
    |--------------------------------------------------------------------------
    */
    Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/search', [SearchController::class, 'index'])->name('search');
    Route::get('/dashboard/stats', [DashboardController::class, 'getStatsApi'])->name('dashboard.stats');

    /*
    |--------------------------------------------------------------------------
    | Employee Management (Admin, HR, Manager)
    |--------------------------------------------------------------------------
    */
    Route::middleware(['role:super_admin,admin_hr,manager'])->group(function () {
        Route::resource('employees', EmployeeController::class);
        Route::get('/api/employees', [EmployeeController::class, 'getEmployeesApi'])->name('employees.api');
        
        // Employee Access Management
        Route::post('/employees/{employee}/access', [EmployeeAccessController::class, 'store'])->name('employee-access.store');
        Route::get('/employee-access/{accessDetail}', [EmployeeAccessController::class, 'show'])->name('employee-access.show');
        Route::put('/employee-access/{accessDetail}', [EmployeeAccessController::class, 'update'])->name('employee-access.update');
        Route::delete('/employee-access/{accessDetail}', [EmployeeAccessController::class, 'destroy'])->name('employee-access.destroy');
    });

    /*
    |--------------------------------------------------------------------------
    | Access Catalogs (IT Security, Super Admin)
    |--------------------------------------------------------------------------
    */
    Route::middleware(['role:super_admin,it_security'])->group(function () {
        Route::resource('access-catalogs', AccessCatalogController::class);
        Route::get('/api/access-catalogs', [AccessCatalogController::class, 'getCatalogsApi'])->name('access-catalogs.api');
    });

    /*
    |--------------------------------------------------------------------------
    | Extension Requests (Admin, HR, Manager)
    |--------------------------------------------------------------------------
    */
    Route::middleware(['role:super_admin,admin_hr,manager'])->group(function () {
        Route::get('/extensions', [ExtensionRequestController::class, 'index'])->name('extensions.index');
        Route::get('/extensions/create/{accessDetail}', [ExtensionRequestController::class, 'create'])->name('extensions.create');
        Route::post('/extensions/{accessDetail}', [ExtensionRequestController::class, 'store'])->name('extensions.store');
        Route::get('/extensions/{extension}', [ExtensionRequestController::class, 'show'])->name('extensions.show');
        Route::get('/api/extensions/pending-count', [ExtensionRequestController::class, 'getPendingCountApi'])->name('extensions.pending-count');
    });

    // Extension Approval (Admin, HR, Manager with specific permission)
    Route::middleware(['role:super_admin,admin_hr,manager'])->group(function () {
        Route::post('/extensions/{extension}/approve', [ExtensionRequestController::class, 'approve'])->name('extensions.approve');
        Route::post('/extensions/{extension}/reject', [ExtensionRequestController::class, 'reject'])->name('extensions.reject');
    });

    /*
    |--------------------------------------------------------------------------
    | Revocation Management (IT Security, Super Admin, Admin HR)
    |--------------------------------------------------------------------------
    */
    Route::middleware(['role:super_admin,it_security,admin_hr'])->group(function () {
        Route::get('/revocations', [RevocationController::class, 'index'])->name('revocations.index');
        Route::post('/revocations/{accessDetail}/revoke', [RevocationController::class, 'revoke'])->name('revocations.revoke');
        Route::post('/revocations/bulk', [RevocationController::class, 'bulkRevoke'])->name('revocations.bulk');
        Route::post('/revocations/{accessDetail}/immediate', [RevocationController::class, 'scheduleImmediate'])->name('revocations.immediate');
        Route::get('/api/revocations/calendar', [RevocationController::class, 'getCalendarApi'])->name('revocations.calendar');
    });

    /*
    |--------------------------------------------------------------------------
    | Reminder Logs (Admin, HR, IT Security)
    |--------------------------------------------------------------------------
    */
    Route::middleware(['role:super_admin,admin_hr,it_security'])->group(function () {
        Route::get('/reminders', [ReminderLogController::class, 'index'])->name('reminders.index');
        Route::post('/reminders/{reminderLog}/resend', [ReminderLogController::class, 'resend'])->name('reminders.resend');
        Route::post('/reminders/test', [ReminderLogController::class, 'testSend'])->name('reminders.test');
        Route::get('/api/reminders/recent', [ReminderLogController::class, 'getRecentApi'])->name('reminders.recent');
    });

    /*
    |--------------------------------------------------------------------------
    | Reports (Admin, HR, Manager)
    |--------------------------------------------------------------------------
    */
    Route::middleware(['role:super_admin,admin_hr,manager'])->group(function () {
        Route::get('/reports/retirement', [ReportController::class, 'retirementReport'])->name('reports.retirement');
        Route::get('/reports/revocation', [ReportController::class, 'revocationReport'])->name('reports.revocation');
        Route::get('/reports/extension', [ReportController::class, 'extensionReport'])->name('reports.extension');
        Route::get('/reports/export-excel/{type}', [ReportController::class, 'exportExcel'])->name('reports.export-excel');
        Route::get('/reports/export-pdf/{type}', [ReportController::class, 'exportPdf'])->name('reports.export-pdf');
        Route::get('/api/reports/chart-data', [ReportController::class, 'getChartData'])->name('reports.chart-data');
    });

    /*
    |--------------------------------------------------------------------------
    | System Admin Only (Super Admin)
    |--------------------------------------------------------------------------
    */
    Route::middleware(['role:super_admin'])->group(function () {
        Route::get('/telegram/set-webhook', [TelegramWebhookController::class, 'setWebhook'])->name('telegram.set-webhook');
        Route::get('/telegram/remove-webhook', [TelegramWebhookController::class, 'removeWebhook'])->name('telegram.remove-webhook');
        Route::get('/telegram/bot-info', [TelegramWebhookController::class, 'getBotInfo'])->name('telegram.bot-info');
    });
});