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

if ($_SERVER["REQUEST_METHOD"] === "OPTIONS") {
    exit(0);
}

require_once "../config/database.php";

try {
    $pdo = getDBConnection();
    
    // Get tatkal settings
    $settings = [];
    $stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE \"tatkal_%\"");
    $stmt->execute();
    
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $settings[$row["setting_key"]] = $row["setting_value"];
    }
    
    // Default values if not set
    $response = [
        "success" => true,
        "tatkal_enabled" => $settings["tatkal_enabled"] ?? "1",
        "tatkal_price" => $settings["tatkal_price"] ?? "1500",
        "tatkal_slots" => $settings["tatkal_slots"] ?? "5",
        "tatkal_advance_days" => $settings["tatkal_advance_days"] ?? "7",
        "tatkal_monday_enabled" => $settings["tatkal_monday_enabled"] ?? "1",
        "tatkal_tuesday_enabled" => $settings["tatkal_tuesday_enabled"] ?? "1",
        "tatkal_wednesday_enabled" => $settings["tatkal_wednesday_enabled"] ?? "1",
        "tatkal_thursday_enabled" => $settings["tatkal_thursday_enabled"] ?? "1",
        "tatkal_friday_enabled" => $settings["tatkal_friday_enabled"] ?? "1",
        "tatkal_saturday_enabled" => $settings["tatkal_saturday_enabled"] ?? "1",
        "tatkal_sunday_enabled" => $settings["tatkal_sunday_enabled"] ?? "1"
    ];
    
    echo json_encode($response);
    
} catch (Exception $e) {
    echo json_encode([
        "success" => false,
        "message" => "Error: " . $e->getMessage()
    ]);
}
?>
