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

try {
    $pdo = getDBConnection();
    
    // Check if settings table exists and has data
    $stmt = $pdo->query("SHOW TABLES LIKE 'settings'");
    $tableExists = $stmt->rowCount() > 0;
    
    if (!$tableExists) {
        // Create settings table
        $pdo->exec("
            CREATE TABLE settings (
                id INT AUTO_INCREMENT PRIMARY KEY,
                default_consultation_price DECIMAL(10,2) DEFAULT 500.00,
                office_phone VARCHAR(20) DEFAULT '',
                office_address TEXT DEFAULT '',
                business_hours VARCHAR(100) DEFAULT '9:00 AM - 6:00 PM',
                cancellation_policy TEXT DEFAULT '',
                refund_policy TEXT DEFAULT '',
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
            )
        ");
        
        // Insert default settings
        $pdo->exec("
            INSERT INTO settings (
                default_consultation_price,
                office_phone,
                office_address,
                business_hours,
                cancellation_policy,
                refund_policy
            ) VALUES (
                500.00,
                '+91-9876543210',
                'Your Office Address',
                '9:00 AM - 6:00 PM',
                'Cancellation allowed 24 hours before appointment',
                'Full refund if cancelled 24 hours before appointment'
            )
        ");
    }
    
    // Get settings from database
    $stmt = $pdo->prepare("
        SELECT 
            default_consultation_price,
            office_phone,
            office_address,
            business_hours,
            cancellation_policy,
            refund_policy
        FROM settings 
        WHERE id = 1
    ");
    
    $stmt->execute();
    $settings = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($settings) {
        echo json_encode([
            'success' => true,
            'default_consultation_price' => $settings['default_consultation_price'] ?? 500,
            'office_phone' => $settings['office_phone'] ?? '',
            'office_address' => $settings['office_address'] ?? '',
            'business_hours' => $settings['business_hours'] ?? '9:00 AM - 6:00 PM',
            'cancellation_policy' => $settings['cancellation_policy'] ?? '',
            'refund_policy' => $settings['refund_policy'] ?? ''
        ]);
    } else {
        // Return default settings if no record found
        echo json_encode([
            'success' => true,
            'default_consultation_price' => 500,
            'office_phone' => '',
            'office_address' => '',
            'business_hours' => '9:00 AM - 6:00 PM',
            'cancellation_policy' => '',
            'refund_policy' => ''
        ]);
    }
    
} catch (Exception $e) {
    error_log("Settings API Error: " . $e->getMessage());
    echo json_encode([
        'success' => false,
        'error' => 'Failed to fetch settings',
        'default_consultation_price' => 500
    ]);
}
?>