<?php
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../auth/login.php");
    exit();
}

require_once __DIR__ . '/../../controllers/SaleController.php';
$saleController = new SaleController();
$salesByMonth = $saleController->getSalesByMonth();

$labels = [];
$data = [];
foreach ($salesByMonth as $sale) {
    $labels[] = $sale['month'];
    $data[] = $sale['total_sales'];
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Análisis de Temporada</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
    <div class="container">
        <h1 class="mt-3">Análisis de Temporada</h1>
        <canvas id="salesChart"></canvas>
        <a href="dashboard.php" class="btn btn-primary mt-3">Volver al Dashboard</a>
    </div>
    <script>
        const ctx = document.getElementById('salesChart').getContext('2d');
        const salesChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: <?php echo json_encode($labels); ?>,
                datasets: [{
                    label: 'Ventas por Mes',
                    data: <?php echo json_encode($data); ?>,
                    backgroundColor: 'rgba(152, 255, 152, 0.2)',
                    borderColor: 'rgba(152, 255, 152, 1)',
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    y: {
                        beginAtZero: true
                    }
                }
            }
        });
    </script>
</body>
</html>