<?php

use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\AccessController;
use App\Http\Controllers\MonitoringController;
use App\Http\Controllers\TrackingController;
use App\Http\Controllers\IncidentController;
use App\Http\Controllers\AuthorizationController;
use App\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes - Ojo de Águila
|--------------------------------------------------------------------------
*/

// Redirección raíz a Login o Dashboard
Route::get('/', function () {
    return redirect()->route('login');
});

// Autenticación (Rutas para Invitados)
Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [LoginController::class, 'login']);
});

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

// Rutas Protegidas (Requieren autenticación y acceso al recinto activo)
Route::middleware(['auth', 'account'])->group(function () {
    
    // Dashboard General
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Control de Acceso (CORE)
    Route::get('/access', [AccessController::class, 'index'])->name('access.index');
    Route::get('/access/create', [AccessController::class, 'create'])->name('access.create');
    Route::post('/access', [AccessController::class, 'store'])->name('access.store');
    Route::post('/access/{id}/exit', [AccessController::class, 'exit'])->name('access.exit');

    // Monitoreo en Vivo (Tiempo Real)
    Route::get('/monitoring', [MonitoringController::class, 'index'])->name('monitoring');
    
    // Geolocalización e Historial GPS
    Route::get('/tracking', [TrackingController::class, 'index'])->name('tracking');

    // Módulo de Incidencias de Seguridad
    Route::get('/incidents', [IncidentController::class, 'index'])->name('incidents.index');
    Route::post('/incidents', [IncidentController::class, 'store'])->name('incidents.store');

    // Módulo de Autorizaciones de Residentes (Solo Administradores de Recinto y Supervisores)
    Route::middleware('role:client_admin,supervisor')->group(function () {
        Route::get('/authorizations', [AuthorizationController::class, 'index'])->name('authorizations.index');
        Route::post('/authorizations/{id}/{status}', [AuthorizationController::class, 'updateStatus'])->name('authorizations.update');
    });

    // Administración SaaS (Solo SuperAdmin Global)
    Route::middleware('role:super_admin')->group(function () {
        Route::get('/admin/accounts', [AdminController::class, 'index'])->name('admin.accounts');
        Route::post('/admin/accounts', [AdminController::class, 'store'])->name('admin.accounts.store');
        Route::post('/admin/accounts/{id}/toggle', [AdminController::class, 'toggleActive'])->name('admin.accounts.toggle');
    });

    // --- Endpoints de API internas por AJAX ---
    Route::get('/api/identity/verify', [AccessController::class, 'verifyIdentity']);
    Route::post('/api/tracking/position', [TrackingController::class, 'storePosition']);
    Route::get('/api/tracking/active-guards', [TrackingController::class, 'getActiveGuards']);
    Route::get('/api/monitoring/data', [MonitoringController::class, 'getRealtimeData']);
});
