<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/../vendor/autoload.php';

use App\Services\AuthService;
use App\Middleware\RoleMiddleware;
use App\Models\Assignment;

// Verificar Auth
RoleMiddleware::auth();

$auth = new AuthService();
$user = $auth->getCurrentUser();

// Obtener resumen (Semana actual)
$assignmentModel = new Assignment();
$currentAssignment = $assignmentModel->getCurrentWeek();

// Obtener próximas tareas si es ayudante
$myAssignments = [];
if ($auth->isAyudante() || $auth->isCoordinador()) {
    $myAssignments = $assignmentModel->getByUser($user['id']);
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard - Contenedor Ibiza</title>
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <?php include 'partials/navbar.php'; ?>

    <div class="container">
        <h1>Bienvenido, <?= e($user['full_name']) ?></h1>
        <p class="mb-4">Panel de Control - Rol: <span class="badge badge-success"><?= strtoupper($user['role']) ?></span></p>
        
        <div class="card">
            <div class="card-header">
                <h2 class="card-title">📅 Esta Semana en el Contenedor</h2>
            </div>
            
            <?php if ($currentAssignment): ?>
                <div style="font-size: 1.2rem; text-align: center; padding: 1rem;">
                    <p><strong>Semana <?= $currentAssignment['week_number'] ?></strong> (<?= formatDate($currentAssignment['start_date']) ?> - <?= formatDate($currentAssignment['end_date']) ?>)</p>
                    <p class="mt-2">Ayudante a cargo:</p>
                    <h3 style="font-size: 2rem; color: var(--primary); margin: 1rem 0;">
                        <?= e($currentAssignment['full_name']) ?>
                    </h3>
                </div>
            <?php else: ?>
                <p class="text-center">No hay asignación programada para esta semana.</p>
            <?php endif; ?>
        </div>

        <?php if (!empty($myAssignments)): ?>
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">👤 Mis Próximos Turnos</h2>
                </div>
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>Semana</th>
                                <th>Fechas</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php 
                            // Filtrar solo futuras
                            $today = date('Y-m-d');
                            $count = 0;
                            foreach ($myAssignments as $asg): 
                                if ($asg['end_date'] < $today) continue;
                                $count++;
                                if ($count > 5) break; 
                            ?>
                                <tr>
                                    <td>Semana <?= $asg['week_number'] ?></td>
                                    <td><?= formatDate($asg['start_date']) ?> al <?= formatDate($asg['end_date']) ?></td>
                                </tr>
                            <?php endforeach; ?>
                            
                            <?php if ($count === 0): ?>
                                <tr><td colspan="2">No tienes turnos pendientes próximamente.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        <?php endif; ?>
        
        <div class="card">
            <div class="card-header">
                <h2 class="card-title">🚀 Accesos Rápidos</h2>
            </div>
            <div style="display: flex; gap: 1rem; flex-wrap: wrap;">
                <a href="rotaciones.php" class="btn btn-primary">Ver Rotación Completa</a>
                <a href="horarios.php" class="btn btn-secondary">Ver Horarios</a>
                <a href="export-pdf.php" target="_blank" class="btn btn-success">Descargar PDF</a>
                
                <?php if ($auth->isCoordinador() || $auth->isAdmin()): ?>
                    <a href="coordinador/reorganizar.php" class="btn btn-primary">Reorganizar Turnos</a>
                <?php endif; ?>
                
                <?php if ($auth->isAdmin()): ?>
                    <a href="admin/usuarios.php" class="btn btn-primary">Gestionar Usuarios</a>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <script src="assets/js/main.js"></script>
</body>
</html>
