<?php
// 1. Headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

// 2. Connect
include_once 'db_connect.php';

// Initialize the master response structure
$response = [
    "news" => [],
    "documents" => [], // This will be an object keyed by category
    "gallery" => [],
    "site_images" => [],
    "tuition_fees" => [],
    "mandatory_fees" => []
];

// --- 1. FETCH NEWS ---
$query = "SELECT id, title, category, author, date, excerpt, content, image_url FROM news ORDER BY date DESC";
$result = $conn->query($query);
if ($result && $result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $response['news'][] = array(
            "id" => $row['id'],
            "title" => $row['title'],
            "category" => $row['category'],
            "author" => $row['author'],
            "date" => date("M d, Y", strtotime($row['date'])),
            "excerpt" => $row['excerpt'],
            "content" => $row['content'],
            "image" => $row['image_url']
        );
    }
}

// --- 2. FETCH DOCUMENTS (Grouped by Category) ---
$query = "SELECT id, title, category, file_path, file_size, upload_date FROM documents ORDER BY category, upload_date DESC";
$result = $conn->query($query);
if ($result && $result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $cat = $row['category'];
        $item = array(
            "id" => $row['id'],
            "title" => $row['title'],
            "category" => $cat,
            "date" => date("M d, Y", strtotime($row['upload_date'])),
            "size" => $row['file_size'],
            "url" => $row['file_path'],
            "fileName" => basename($row['file_path'])
        );

        if (!isset($response['documents'][$cat])) {
            $response['documents'][$cat] = [];
        }
        $response['documents'][$cat][] = $item;
    }
}

// --- 3. FETCH GALLERY ---
$query = "SELECT id, title, category, image_url FROM gallery ORDER BY uploaded_at DESC";
$result = $conn->query($query);
if ($result && $result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $response['gallery'][] = array(
            "id" => $row['id'],
            "title" => $row['title'],
            "category" => $row['category'],
            "src" => $row['image_url']
        );
    }
}

// --- 4. FETCH SITE IMAGES ---
$query = "SELECT section_name, image_url, alt_text, display_order FROM site_images ORDER BY display_order ASC";
$result = $conn->query($query);
if ($result && $result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $section = $row['section_name'];
        if (!isset($response['site_images'][$section])) {
            $response['site_images'][$section] = [];
        }
        $response['site_images'][$section][] = $row['image_url'];
    }
}

// 5. Fetch Tuition Fees
$tuition_res = $conn->query("SELECT * FROM tuition_fees ORDER BY display_order ASC");
while($row = $tuition_res->fetch_assoc()) {
    $response["tuition_fees"][] = $row;
}

// 6. Fetch Mandatory Fees
$mandatory_res = $conn->query("SELECT * FROM mandatory_fees ORDER BY id ASC");
while($row = $mandatory_res->fetch_assoc()) {
    $response["mandatory_fees"][] = $row;
}

// 7. Return Everything
echo json_encode($response);
$conn->close();
?>