<?php
/**
 * Payment Gateway Factory
 * File: app/Payment/PaymentGatewayFactory.php
 * 
 * This factory creates the correct payment gateway instance
 * based on your database settings
 */

class PaymentGatewayFactory {
    private static $instance = null;
    private $db;
    private $defaultGateway;
    private $paystackEnabled;
    private $flutterwaveEnabled;
    
    private function __construct() {
        $this->db = Database::getInstance();
        $this->loadSettings();
    }
    
    public static function getInstance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    /**
     * Load payment gateway settings from database
     */
    private function loadSettings() {
        $settings = $this->db->fetchAll(
            "SELECT setting_key, setting_value FROM settings 
             WHERE setting_key IN ('default_payment_gateway', 'enable_paystack', 'enable_flutterwave')"
        );
        
        foreach ($settings as $setting) {
            switch ($setting['setting_key']) {
                case 'default_payment_gateway':
                    $this->defaultGateway = $setting['setting_value'];
                    break;
                case 'enable_paystack':
                    $this->paystackEnabled = ($setting['setting_value'] === 'true' || $setting['setting_value'] === '1');
                    break;
                case 'enable_flutterwave':
                    $this->flutterwaveEnabled = ($setting['setting_value'] === 'true' || $setting['setting_value'] === '1');
                    break;
            }
        }
        
        // Default to Paystack if not set
        if (empty($this->defaultGateway)) {
            $this->defaultGateway = 'paystack';
        }
    }
    
    /**
     * Get the default payment gateway instance
     */
    public function getGateway($gateway = null) {
        $gateway = $gateway ?? $this->defaultGateway;
        
        if ($gateway === 'paystack' && $this->paystackEnabled !== false) {
            return new Paystack();
        } elseif ($gateway === 'flutterwave' && $this->flutterwaveEnabled !== false) {
            return new Flutterwave();
        }
        
        // Fallback to Paystack
        return new Paystack();
    }
    
    /**
     * Get default gateway name
     */
    public function getDefaultGatewayName() {
        return $this->defaultGateway ?? 'paystack';
    }
    
    /**
     * Check if Paystack is enabled
     */
    public function isPaystackEnabled() {
        return $this->paystackEnabled !== false;
    }
    
    /**
     * Check if Flutterwave is enabled
     */
    public function isFlutterwaveEnabled() {
        return $this->flutterwaveEnabled !== false;
    }
    
    /**
     * Get gateway configuration
     */
    public function getGatewayConfig($gateway = null) {
        $gateway = $gateway ?? $this->defaultGateway;
        
        if ($gateway === 'paystack') {
            return [
                'name' => 'Paystack',
                'public_key' => PAYSTACK_PUBLIC_KEY,
                'enabled' => $this->isPaystackEnabled()
            ];
        } elseif ($gateway === 'flutterwave') {
            return [
                'name' => 'Flutterwave',
                'public_key' => FLW_PUBLIC_KEY,
                'encryption_key' => FLW_ENCRYPTION_KEY ?? '',
                'enabled' => $this->isFlutterwaveEnabled()
            ];
        }
        
        return null;
    }
}