<?php
// Site Configuration File
class SiteConfig {
    private static $settings = [];
    private static $loaded = false;
    
    public static function load() {
        if (!self::$loaded) {
            global $conn;
            
            // Load settings from database if table exists
            $table_check = mysqli_query($conn, "SHOW TABLES LIKE 'settings'");
            if (mysqli_num_rows($table_check) > 0) {
                $result = mysqli_query($conn, "SELECT setting_key, setting_value FROM settings");
                if ($result) {
                    while ($row = mysqli_fetch_assoc($result)) {
                        self::$settings[$row['setting_key']] = $row['setting_value'];
                    }
                    self::$loaded = true;
                }
            }
            
            // Set timezone
            date_default_timezone_set(self::get('timezone', 'Asia/Kolkata'));
        }
    }
    
    public static function get($key, $default = null) {
        self::load();
        return isset(self::$settings[$key]) ? self::$settings[$key] : $default;
    }
    
    public static function set($key, $value) {
        global $conn;
        self::load();
        
        // Update in database
        $key = mysqli_real_escape_string($conn, $key);
        $value = mysqli_real_escape_string($conn, $value);
        
        $check = mysqli_query($conn, "SELECT setting_key FROM settings WHERE setting_key = '$key'");
        if (mysqli_num_rows($check) > 0) {
            mysqli_query($conn, "UPDATE settings SET setting_value = '$value' WHERE setting_key = '$key'");
        } else {
            mysqli_query($conn, "INSERT INTO settings (setting_key, setting_value) VALUES ('$key', '$value')");
        }
        
        // Update in memory
        self::$settings[$key] = $value;
    }
    
    public static function getAll() {
        self::load();
        return self::$settings;
    }
}

// Check if functions are already declared before declaring them
if (!function_exists('getSiteName')) {
    function getSiteName() {
        return SiteConfig::get('site_name', 'shopnest');
    }
}

if (!function_exists('getSiteEmail')) {
    function getSiteEmail() {
        return SiteConfig::get('site_email', 'admin@shopnest.com');
    }
}

if (!function_exists('getCurrency')) {
    function getCurrency() {
        return SiteConfig::get('currency', '₹');
    }
}

if (!function_exists('getCurrencyCode')) {
    function getCurrencyCode() {
        return SiteConfig::get('currency_code', 'INR');
    }
}

if (!function_exists('formatPrice')) {
    function formatPrice($amount) {
        return getCurrency() . number_format($amount, 2);
    }
}

if (!function_exists('getTaxPercentage')) {
    function getTaxPercentage() {
        return (float) SiteConfig::get('tax_percentage', 18);
    }
}

if (!function_exists('getDeliveryCharge')) {
    function getDeliveryCharge() {
        return (float) SiteConfig::get('delivery_charge', 99);
    }
}

if (!function_exists('getFreeDeliveryMinimum')) {
    function getFreeDeliveryMinimum() {
        return (float) SiteConfig::get('free_delivery_minimum', 999);
    }
}

if (!function_exists('getMinRentalDays')) {
    function getMinRentalDays() {
        return (int) SiteConfig::get('min_rental_days', 3);
    }
}

if (!function_exists('getMaxRentalDays')) {
    function getMaxRentalDays() {
        return (int) SiteConfig::get('max_rental_days', 30);
    }
}

if (!function_exists('getCleaningFee')) {
    function getCleaningFee() {
        return (float) SiteConfig::get('cleaning_fee_default', 200);
    }
}

if (!function_exists('getLateReturnFee')) {
    function getLateReturnFee() {
        return (float) SiteConfig::get('late_return_fee', 500);
    }
}

if (!function_exists('getSocialLinks')) {
    function getSocialLinks() {
        return [
            'facebook' => SiteConfig::get('facebook_url'),
            'instagram' => SiteConfig::get('instagram_url'),
            'twitter' => SiteConfig::get('twitter_url'),
            'pinterest' => SiteConfig::get('pinterest_url'),
            'youtube' => SiteConfig::get('youtube_url')
        ];
    }
}

// Auto-load configuration
SiteConfig::load();