<?php
/**
 * Gift Genius Environment Configuration
 * Automatically detects testing vs production environment
 */

// Prevent direct access
if (!defined('GIFT_GENIUS_CONFIG')) {
    define('GIFT_GENIUS_CONFIG', true);
}

class AppConfig {
    private static $config = null;
    
    /**
     * Get current environment configuration
     */
    public static function get() {
        if (self::$config === null) {
            self::$config = self::detectEnvironment();
        }
        return self::$config;
    }
    
    /**
     * Detect environment based on domain/host
     */
    private static function detectEnvironment() {
        $host = $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? 'localhost';
        $isHttps = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on';
        $protocol = $isHttps ? 'https' : 'http';
        
        // Determine environment
        if (strpos($host, 'localhost') !== false || strpos($host, '127.0.0.1') !== false) {
            $environment = 'testing';
        } elseif (strpos($host, 'giftfindrs.co.uk') !== false) {
            $environment = 'production';
        } else {
            // Default to testing for unknown hosts
            $environment = 'testing';
        }
        
        // Base configuration
        $config = [
            'environment' => $environment,
            'host' => $host,
            'protocol' => $protocol,
            'base_url' => $protocol . '://' . $host,
            'debug' => $environment === 'testing',
        ];
        
        // Environment-specific settings
        if ($environment === 'testing') {
            $config = array_merge($config, self::getTestingConfig());
        } else {
            $config = array_merge($config, self::getProductionConfig());
        }
        
        return $config;
    }
    
    /**
     * Testing environment configuration (localhost)
     */
    private static function getTestingConfig() {
        return [
            'database' => [
                'host' => 'localhost',
                'name' => 'gift_genius_dev',
                'user' => 'root',
                'pass' => 'mysql',
                'charset' => 'utf8mb4'
            ],
            'security' => [
                'force_https' => false,
                'strict_transport_security' => false,
                'secure_cookies' => false
            ],
            'logging' => [
                'level' => 'debug',
                'file' => 'logs/debug.log'
            ],
            'cache' => [
                'enabled' => false,
                'ttl' => 300
            ],
            'amazon' => [
                'affiliate_tag' => 'test-tag-21',
                'base_url' => 'https://amazon.co.uk'
            ]
        ];
    }
    
    /**
     * Production environment configuration (giftfindrs.co.uk)
     */
    private static function getProductionConfig() {
        return [
            'database' => [
                'host' => 'localhost',
                'name' => 'gift_genius',
                'user' => 'root',
                'pass' => 'mysql', // TODO: Change for production
                'charset' => 'utf8mb4'
            ],
            'security' => [
                'force_https' => true,
                'strict_transport_security' => true,
                'secure_cookies' => true
            ],
            'logging' => [
                'level' => 'error',
                'file' => 'logs/error.log'
            ],
            'cache' => [
                'enabled' => true,
                'ttl' => 3600
            ],
            'amazon' => [
                'affiliate_tag' => 'giftfindrs-21', // Update with your real affiliate tag
                'base_url' => 'https://amazon.co.uk'
            ]
        ];
    }
    
    /**
     * Get specific config value with dot notation
     */
    public static function getValue($key, $default = null) {
        $config = self::get();
        $keys = explode('.', $key);
        $value = $config;
        
        foreach ($keys as $k) {
            if (isset($value[$k])) {
                $value = $value[$k];
            } else {
                return $default;
            }
        }
        
        return $value;
    }
    
    /**
     * Check if we're in testing environment
     */
    public static function isTesting() {
        return self::getValue('environment') === 'testing';
    }
    
    /**
     * Check if we're in production environment
     */
    public static function isProduction() {
        return self::getValue('environment') === 'production';
    }
    
    /**
     * Get database configuration
     */
    public static function getDatabase() {
        return self::getValue('database');
    }
    
    /**
     * Get base URL for current environment
     */
    public static function getBaseUrl() {
        return self::getValue('base_url');
    }
    
    /**
     * Get API base URL
     */
    public static function getApiBaseUrl() {
        return self::getBaseUrl() . '/api';
    }
}

// Export configuration for JavaScript
if (isset($_GET['js_config']) && $_GET['js_config'] === '1') {
    header('Content-Type: application/javascript');
    $jsConfig = [
        'environment' => AppConfig::getValue('environment'),
        'baseUrl' => AppConfig::getBaseUrl(),
        'apiBaseUrl' => AppConfig::getApiBaseUrl(),
        'debug' => AppConfig::getValue('debug'),
        'amazonAffiliateTag' => AppConfig::getValue('amazon.affiliate_tag')
    ];
    echo 'window.APP_CONFIG = ' . json_encode($jsConfig) . ';';
    exit;
}
?> 