<?php
/** @var Router $router */

use App\Core\Router;
use App\Core\Auth;
use App\Core\PortalAuth;
use App\Controllers\AuthController;
use App\Controllers\PortalAuthController;
use App\Controllers\DashboardController;
use App\Controllers\TicketController;
use App\Controllers\KbController;
use App\Controllers\AdminController;
use App\Controllers\SlaController;
use App\Controllers\ReportController;
use App\Controllers\PortalController;

// ---- Public (agent side) ----------------------------------------------
$router->get('/login', [AuthController::class, 'loginForm']);
$router->post('/login', [AuthController::class, 'login']);
$router->get('/logout', [AuthController::class, 'logout']);

// ---- Internal agent console (auth required) ----------------------------
$router->group([Auth::requireLogin()], function (Router $router) {
    $router->get('/', [DashboardController::class, 'index']);
    $router->get('/dashboard', [DashboardController::class, 'index']);
    $router->get('/notifications/read-all', [DashboardController::class, 'markAllRead']);

    $router->get('/tickets', [TicketController::class, 'index']);
    $router->get('/tickets/my', [TicketController::class, 'my']);
    $router->get('/tickets/unassigned', [TicketController::class, 'unassigned']);
    $router->get('/tickets/escalated', [TicketController::class, 'escalated']);
    $router->get('/tickets/new', [TicketController::class, 'createForm']);
    $router->post('/tickets', [TicketController::class, 'store']);
    $router->get('/tickets/{id}', [TicketController::class, 'show']);
    $router->post('/tickets/{id}/reply', [TicketController::class, 'reply']);
    $router->post('/tickets/{id}/note', [TicketController::class, 'note']);
    $router->post('/tickets/{id}/status', [TicketController::class, 'changeStatus']);
    $router->post('/tickets/{id}/kanban-move', [TicketController::class, 'kanbanMove']);
    $router->post('/tickets/{id}/assign', [TicketController::class, 'assign']);
    $router->post('/tickets/{id}/claim', [TicketController::class, 'claim']);
    $router->post('/tickets/{id}/escalate', [TicketController::class, 'escalate']);
    $router->post('/tickets/{id}/classify', [TicketController::class, 'classify']);
    $router->post('/tickets/{id}/attachment', [TicketController::class, 'uploadAttachment']);
    $router->get('/tickets/{id}/attachment/{attId}', [TicketController::class, 'downloadAttachment']);
    $router->post('/tickets/{id}/time', [TicketController::class, 'logTime']);
    $router->post('/tickets/{id}/link', [TicketController::class, 'linkRecord']);
    $router->post('/tickets/{id}/watch', [TicketController::class, 'watch']);

    $router->get('/kb', [KbController::class, 'index']);
    $router->get('/kb/search', [KbController::class, 'search']);
    $router->get('/kb/new', [KbController::class, 'createForm']);
    $router->post('/kb', [KbController::class, 'store']);
    $router->get('/kb/{id}', [KbController::class, 'show']);
    $router->get('/kb/{id}/edit', [KbController::class, 'editForm']);
    $router->post('/kb/{id}', [KbController::class, 'update']);
    $router->post('/kb/{id}/publish', [KbController::class, 'publish']);
    $router->post('/kb/{id}/retire', [KbController::class, 'retire']);

    $router->get('/reports', [ReportController::class, 'index']);
    $router->get('/reports/sla-compliance', [ReportController::class, 'slaCompliance']);
    $router->get('/reports/ageing', [ReportController::class, 'ageing']);
    $router->get('/reports/breach-register', [ReportController::class, 'breachRegister']);
    $router->get('/reports/agent-productivity', [ReportController::class, 'agentProductivity']);
    $router->get('/reports/csat', [ReportController::class, 'csat']);
    $router->get('/reports/entitlement', [ReportController::class, 'entitlement']);

    $router->get('/admin', [AdminController::class, 'index']);
    $router->get('/admin/categories', [AdminController::class, 'categories']);
    $router->post('/admin/categories', [AdminController::class, 'storeCategory']);
    $router->post('/admin/categories/{id}/toggle', [AdminController::class, 'toggleCategory']);
    $router->post('/admin/categories/{id}', [AdminController::class, 'updateCategory']);
    $router->post('/admin/categories/{id}/delete', [AdminController::class, 'deleteCategory']);
    $router->get('/admin/priorities', [AdminController::class, 'priorities']);
    $router->post('/admin/priorities/{id}', [AdminController::class, 'updatePriority']);
    $router->get('/admin/queues', [AdminController::class, 'queues']);
    $router->post('/admin/queues', [AdminController::class, 'storeQueue']);
    $router->get('/admin/teams', [AdminController::class, 'teams']);
    $router->post('/admin/teams', [AdminController::class, 'storeTeam']);
    $router->post('/admin/teams/{id}/members', [AdminController::class, 'addTeamMember']);
    $router->get('/admin/calendar', [AdminController::class, 'calendar']);
    $router->post('/admin/calendar/holidays', [AdminController::class, 'storeHoliday']);
    $router->get('/admin/templates', [AdminController::class, 'templates']);
    $router->post('/admin/templates/{id}', [AdminController::class, 'updateTemplate']);
    $router->post('/admin/canned', [AdminController::class, 'storeCanned']);
    $router->post('/admin/canned/{id}', [AdminController::class, 'updateCanned']);
    $router->post('/admin/canned/{id}/delete', [AdminController::class, 'deleteCanned']);
    $router->get('/admin/users', [AdminController::class, 'users']);
    $router->post('/admin/users', [AdminController::class, 'storeUser']);
    $router->post('/admin/users/{id}/toggle', [AdminController::class, 'toggleUser']);
    $router->get('/admin/permissions', [AdminController::class, 'permissions']);
    $router->post('/admin/permissions', [AdminController::class, 'updatePermissions']);
    $router->get('/admin/customers', [AdminController::class, 'customers']);
    $router->post('/admin/customers', [AdminController::class, 'storeCustomer']);
    $router->get('/admin/customers/{id}', [AdminController::class, 'customerShow']);
    $router->post('/admin/customers/{id}', [AdminController::class, 'updateCustomer']);
    $router->post('/admin/customers/{id}/toggle', [AdminController::class, 'toggleCustomer']);
    $router->post('/admin/customers/{id}/contacts', [AdminController::class, 'storeContact']);
    $router->post('/admin/contacts/{id}', [AdminController::class, 'updateContact']);
    $router->post('/admin/customers/{id}/contracts', [AdminController::class, 'storeContract']);
    $router->post('/admin/contracts/{id}/modules', [AdminController::class, 'updateContractModules']);
    $router->get('/admin/products', [AdminController::class, 'products']);
    $router->post('/admin/products', [AdminController::class, 'storeProduct']);
    $router->post('/admin/products/{id}', [AdminController::class, 'updateProduct']);
    $router->post('/admin/products/{id}/toggle', [AdminController::class, 'toggleProduct']);
    $router->post('/admin/products/{id}/delete', [AdminController::class, 'deleteProduct']);
    $router->get('/admin/audit-log', [AdminController::class, 'auditLog']);
    $router->get('/admin/quarantine', [AdminController::class, 'quarantine']);

    $router->get('/admin/sla', [SlaController::class, 'index']);
    $router->get('/admin/sla/new', [SlaController::class, 'createForm']);
    $router->post('/admin/sla', [SlaController::class, 'store']);
    $router->get('/admin/sla/{id}', [SlaController::class, 'edit']);
    $router->post('/admin/sla/{id}', [SlaController::class, 'update']);
    $router->post('/admin/sla/{id}/escalation', [SlaController::class, 'storeEscalationLevel']);
    $router->post('/admin/sla/{id}/simulate', [SlaController::class, 'simulate']);
});

// ---- Customer portal -----------------------------------------------------
$router->get('/portal/login', [PortalAuthController::class, 'loginForm']);
$router->post('/portal/login', [PortalAuthController::class, 'login']);
$router->get('/portal/logout', [PortalAuthController::class, 'logout']);
$router->get('/portal/kb-search', [PortalController::class, 'kbSearch']);

$router->group([PortalAuth::requireLogin()], function (Router $router) {
    $router->get('/portal', [PortalController::class, 'dashboard']);
    $router->get('/portal/tickets', [PortalController::class, 'tickets']);
    $router->get('/portal/tickets/new', [PortalController::class, 'createForm']);
    $router->post('/portal/tickets', [PortalController::class, 'store']);
    $router->post('/portal/kb-deflect', [PortalController::class, 'deflect']);
    $router->get('/portal/tickets/{id}', [PortalController::class, 'show']);
    $router->get('/portal/tickets/{id}/attachment/{attId}', [PortalController::class, 'downloadAttachment']);
    $router->post('/portal/tickets/{id}/reply', [PortalController::class, 'reply']);
    $router->post('/portal/tickets/{id}/accept', [PortalController::class, 'accept']);
    $router->post('/portal/tickets/{id}/reopen', [PortalController::class, 'reopen']);
    $router->post('/portal/tickets/{id}/csat', [PortalController::class, 'csat']);
    // Article view stays reachable from the ticket-creation deflection suggestions,
    // even though the standalone Knowledge Base browse page has been removed.
    $router->get('/portal/kb/{slug}', [PortalController::class, 'kbShow']);
});
