<?php
// Start session and check if user is logged in
session_start();
if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) {
    http_response_code(403);
    echo json_encode(['error' => 'Unauthorized']);
    exit;
}

// Include config file
require_once "includes/config.php";

// Get the type parameter
$type = isset($_GET['type']) ? trim($_GET['type']) : '';

if (empty($type)) {
    http_response_code(400);
    echo json_encode(['error' => 'Type parameter is required']);
    exit;
}

try {
    // Prepare the SQL query
    $sql = "SELECT id, name FROM gallery_categories WHERE type = ? ORDER BY name ASC";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $type);
    $stmt->execute();
    $result = $stmt->get_result();
    
    $categories = [];
    while ($row = $result->fetch_assoc()) {
        $categories[] = $row;
    }
    
    // Return the categories as JSON
    header('Content-Type: application/json');
    echo json_encode($categories);
    
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['error' => 'Error fetching categories: ' . $e->getMessage()]);
}

// Close connection
$conn->close();
?>
