<?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');

// Handle preflight request
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    http_response_code(200);
    exit();
}

// Database configuration
$host = 'localhost';
$dbname = 'astro_db';
$username = 'root';
$password = '';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'message' => 'Database connection failed']);
    exit();
}

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    try {
        // Get tatkal settings
        $stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'tatkal_%'");
        $stmt->execute();
        $settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
        
        // Get day settings (check both value and setting_value columns)
        $normalDaysQuery = $pdo->prepare("SELECT COALESCE(value, setting_value) FROM settings WHERE setting_key = 'normal_days'");
        $normalDaysQuery->execute();
        $normalDays = json_decode($normalDaysQuery->fetchColumn() ?: '[]', true);
        
        $tatkalDaysQuery = $pdo->prepare("SELECT COALESCE(value, setting_value) FROM settings WHERE setting_key = 'tatkal_days'");
        $tatkalDaysQuery->execute();
        $tatkalDays = json_decode($tatkalDaysQuery->fetchColumn() ?: '[]', true);
        
        // Default days if not set
        if (empty($normalDays)) {
            $normalDays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
        }
        
        if (empty($tatkalDays)) {
            $tatkalDays = ['monday', 'tuesday', 'wednesday', 'friday'];
        }
        
        // Default values
        $response = [
            'success' => true,
            'tatkal_enabled' => $settings['tatkal_enabled'] ?? '1',
            'tatkal_price' => $settings['tatkal_price'] ?? '1500',
            'tatkal_slots' => $settings['tatkal_slots'] ?? '10',
            'tatkal_advance_days' => $settings['tatkal_advance_days'] ?? '7',
            'tatkal_monday' => in_array('monday', $tatkalDays) ? '1' : '0',
            'tatkal_tuesday' => in_array('tuesday', $tatkalDays) ? '1' : '0',
            'tatkal_wednesday' => in_array('wednesday', $tatkalDays) ? '1' : '0',
            'tatkal_thursday' => in_array('thursday', $tatkalDays) ? '1' : '0',
            'tatkal_friday' => in_array('friday', $tatkalDays) ? '1' : '0',
            'tatkal_saturday' => in_array('saturday', $tatkalDays) ? '1' : '0',
            'tatkal_sunday' => in_array('sunday', $tatkalDays) ? '1' : '0',
            'normal_days' => $normalDays,
            'tatkal_days' => $tatkalDays
        ];
        
        echo json_encode($response);
    } catch (Exception $e) {
        echo json_encode(['success' => false, 'message' => 'Error fetching settings: ' . $e->getMessage()]);
    }
} else {
    echo json_encode(['success' => false, 'message' => 'Invalid request method']);
}
?>