<?php
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(200); exit; }

require_once __DIR__ . '/config.php';

$db   = getDB();
$hoy  = date('Y-m-d');

// Solo avisos activos cuya vigencia incluye hoy
$stmt = $db->prepare(
    "SELECT id, titulo, mensaje, fechaInicio, fechaFin
     FROM mensaje
     WHERE status = 1
       AND fechaInicio <= ?
       AND fechaFin    >= ?
     ORDER BY id DESC"
);
$stmt->execute([$hoy, $hoy]);
$avisos = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo json_encode(['success' => true, 'data' => $avisos], JSON_UNESCAPED_UNICODE);
