<?php
header('Content-Type: application/json');
include '../db.php';
include '../includes/tax_helpers.php';

try {
    // Get current tax calculation method
    $tax_method = getTaxCalculationMethod($conn);
    
    // Get all payment method tax rates
    $payment_tax_rates = getAllPaymentMethodTaxRates($conn);
    
    // Check if payment method tax rates should be shown
    $show_tax_rates = shouldShowPaymentMethodTaxRates($conn);
    
    // Check if tax preview is enabled
    $enable_preview = isTaxPreviewEnabled($conn);
    
    $response = [
        'success' => true,
        'tax_method' => $tax_method,
        'tax_method_display' => getTaxMethodDisplayName($tax_method),
        'payment_tax_rates' => $payment_tax_rates,
        'show_tax_rates' => $show_tax_rates,
        'enable_preview' => $enable_preview
    ];
    
} catch (Exception $e) {
    $response = [
        'success' => false,
        'error' => 'Failed to load tax settings: ' . $e->getMessage(),
        'tax_method' => 'item_wise', // Default fallback
        'payment_tax_rates' => [],
        'show_tax_rates' => false,
        'enable_preview' => false
    ];
}

echo json_encode($response);
?>
