<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
header("Content-Type: application/json");

$servername = "localhost";
$username = "root";
$password = "altostratus";
$dbname = "smfi_db";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    echo json_encode(["success" => false, "message" => "DB connection failed: " . $conn->connect_error]);
    exit;
}

$sql = "SELECT * FROM nowcast_reports ORDER BY created_at DESC";
$result = $conn->query($sql);

$reports = [];

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        // Decode JSON fields
        $row["districts"] = json_decode($row["districts"], true);
        $row["weather_advice"] = json_decode($row["weather_advice"], true);
        $row["weather_impact"] = json_decode($row["weather_impact"], true);
        $reports[] = $row;
    }
}

$conn->close();

echo json_encode(["success" => true, "data" => $reports]);
?>

