<?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, Authorization');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') exit(0);

require_once '../config/database.php';

$defaults = [
    'default_consultation_price' => 500,
    'office_phone' => '+91-9876543210',
    'office_address' => 'Your Office Address',
    'business_hours' => '9:00 AM - 6:00 PM',
    'cancellation_policy' => 'Cancellation allowed 24 hours before appointment',
    'refund_policy' => 'Full refund if cancelled 24 hours before appointment',
    'cash_payment_enabled' => 'on',
    'phonepe_payment_enabled' => 'on'
];

try {
    $pdo = getDBConnection();
    $stmt = $pdo->query("
        SELECT setting_key, setting_value
        FROM settings
        WHERE setting_key IN (
            'other_days_price',
            'office_phone',
            'office_address',
            'business_hours',
            'cancellation_policy',
            'refund_policy',
            'cash_payment_enabled',
            'phonepe_payment_enabled'
        )
    ");

    $settings = [];
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $settings[$row['setting_key']] = $row['setting_value'];
    }

    echo json_encode([
        'success' => true,
        'default_consultation_price' => (float)($settings['other_days_price'] ?? $defaults['default_consultation_price']),
        'office_phone' => $settings['office_phone'] ?? $defaults['office_phone'],
        'office_address' => $settings['office_address'] ?? $defaults['office_address'],
        'business_hours' => $settings['business_hours'] ?? $defaults['business_hours'],
        'cancellation_policy' => $settings['cancellation_policy'] ?? $defaults['cancellation_policy'],
        'refund_policy' => $settings['refund_policy'] ?? $defaults['refund_policy'],
        'cash_payment_enabled' => $settings['cash_payment_enabled'] ?? $defaults['cash_payment_enabled'],
        'phonepe_payment_enabled' => $settings['phonepe_payment_enabled'] ?? $defaults['phonepe_payment_enabled']
    ]);
} catch (Exception $e) {
    error_log("Get Settings API Error: " . $e->getMessage());
    echo json_encode([
        'success' => true,
        'default_consultation_price' => $defaults['default_consultation_price'],
        'office_phone' => $defaults['office_phone'],
        'office_address' => $defaults['office_address'],
        'business_hours' => $defaults['business_hours'],
        'cancellation_policy' => $defaults['cancellation_policy'],
        'refund_policy' => $defaults['refund_policy'],
        'cash_payment_enabled' => $defaults['cash_payment_enabled'],
        'phonepe_payment_enabled' => $defaults['phonepe_payment_enabled']
    ]);
}
?>
