<?php

use Core\Middleware\AuthMiddleware;
use Core\Middleware\GuestMiddleware;
use Core\Middleware\RoleMiddleware;

/*
|--------------------------------------------------------------------------
| PUBLIC ROUTES (No Login Required)
|--------------------------------------------------------------------------
*/
// Home & Static Pages
$router->get('/', 'HomeController@index');
$router->get('/about', 'HomeController@aboutPage');
$router->get('/contact', 'HomeController@contactPage');
$router->post('/contact', 'HomeController@submitContactForm');

// Emergency Incident Reporting Workflow (NO LOGIN REQUIRED)
$router->get('/emergency/report', 'IncidentController@reportPage');
$router->post('/emergency/report', 'IncidentController@submitReport'); // Handles GPS, Image, Category

// Live Tracking & Incident View for the Victim (Accessible via a unique secure link/ID)
$router->get('/emergency/track/{id}', 'IncidentController@show'); // View live status and map

// Real-Time Chat API (AJAX) - Accessible by victim via the secure session ID
$router->get('/api/emergency/{id}/messages', 'ChatController@getMessages');
$router->post('/api/emergency/{id}/messages', 'ChatController@sendMessage');


/*
|--------------------------------------------------------------------------
| GUEST AUTH ROUTES (Not Logged In)
|--------------------------------------------------------------------------
*/
$router->group(["middleware" => [new GuestMiddleware()]], function($router){
    $router->get('/login', 'AuthController@loginPage');
    $router->post('/login', 'AuthController@login');
    $router->get('/register', 'AuthController@registerPage');
    $router->post('/register', 'AuthController@register');
    
    // Password Recovery
    $router->get('/forgot-password', 'AuthController@forgotPasswordPage');
    $router->post('/forgot-password', 'AuthController@sendResetLink');
    $router->get('/reset-password', 'AuthController@resetPasswordPage');
    $router->post('/reset-password', 'AuthController@resetPassword');
});

/*
|--------------------------------------------------------------------------
| LOGGED-IN USER ROUTES (Optional Account Features)
|--------------------------------------------------------------------------
*/
$router->group(["middleware" => [new AuthMiddleware()]], function($router){
    // Global Logout
    $router->get('/logout', 'AuthController@logout');

    // User Dashboard (For viewing past reports if they chose to create an account)
    $router->get('/dashboard', 'UserController@dashboard');
    $router->post('/profile/update', 'UserController@profileUpdate');
});

/*
|--------------------------------------------------------------------------
| RESPONDER ROUTES (Protected by Auth + Responder Role)
|--------------------------------------------------------------------------
*/
$router->group([
    'prefix' => '/responder',
    'middleware' => [new AuthMiddleware(), new RoleMiddleware(['responder'])]
], function($router){

    // Responder Dashboard (Map view of assigned incidents)
    $router->get('/', 'Responder\\DashboardController@index');

    $router->get('/dashboard', 'Responder\\DashboardController@index');

    // Incident Actions
    $router->get('/incident/{id}', 'Responder\\IncidentController@show');
    $router->post('/incident/{id}/accept', 'Responder\\IncidentController@accept'); // Accept the dispatch
    $router->post('/incident/{id}/status', 'Responder\\IncidentController@updateStatus'); // En route, Resolved

    // Live Location Updates from Responder to Backend (AJAX/API)
    $router->post('/api/location/update', 'Responder\\LocationController@updateCoordinates');
});

/*
|--------------------------------------------------------------------------
| ADMIN ROUTES (Protected by Auth + Admin Role)
|--------------------------------------------------------------------------
*/
$router->group([
    'prefix' => '/admin',
    'middleware' => [new AuthMiddleware(), new RoleMiddleware(['admin'])]
], function($router){

    // Admin Main Dashboard (Analytics & Heatmap)
    $router->get('/', 'Admin\\DashboardController@index');

    // 1. Incident Verification & Dispatch (Human-in-the-loop)
    $router->get('/incidents/pending', 'Admin\\DispatchController@pending');
    $router->get('/incidents/{id}', 'Admin\\DispatchController@review');
    $router->post('/incidents/{id}/verify', 'Admin\\DispatchController@verify'); // Admin verifies real emergency
    $router->post('/incidents/{id}/dispatch', 'Admin\\DispatchController@dispatchResponder'); // Admin assigns nearest responder

    // 2. Emergency Categories Management (CRUD)
    $router->get('/categories', 'Admin\\CategoryController@index');
    $router->post('/categories', 'Admin\\CategoryController@store');
    $router->post('/categories/{id}/update', 'Admin\\CategoryController@update');
    $router->post('/categories/{id}/delete', 'Admin\\CategoryController@destroy');

    // 3. Responder Directory Management (Adding Police, Fire, Medics)
    $router->get('/responders', 'Admin\\ResponderController@index');
    $router->get('/responders/create', 'Admin\\ResponderController@create');
    $router->post('/responders', 'Admin\\ResponderController@store'); // Set base GPS location & Category
    $router->get('/responders/{id}/edit', 'Admin\\ResponderController@edit');
    $router->post('/responders/{id}/update', 'Admin\\ResponderController@update');

    // 4. System Users (Victims & Admins)
    $router->get('/users', 'Admin\\UserController@index');
    $router->post('/users/{id}/status', 'Admin\\UserController@updateStatus');

    // 5. Reporting & Analytics (Audit Logs)
    $router->get('/reports', 'Admin\\ReportController@index');
    $router->get('/reports/generate', 'Admin\\ReportController@generateCSV'); // Export functionality
    $router->get('/audit-logs', 'Admin\\AuditController@index'); // View all system actions
});

// Media serving route for incident image uploads
$router->get('/media/{path:.+}', 'MediaController@serve');