<?php
/**
 * Payment Configuration Helper Class
 * Manages payment-related configuration settings
 */

class PaymentConfig {
    private $conn;
    
    public function __construct($connection) {
        $this->conn = $connection;
    }
    
    /**
     * Get payment configuration value by key
     */
    public function getConfig($key, $default = '') {
        $stmt = $this->conn->prepare("SELECT config_value FROM payment_config WHERE config_key = ?");
        if (!$stmt) {
            error_log("Failed to prepare payment config query: " . $this->conn->error);
            return $default;
        }
        
        $stmt->bind_param("s", $key);
        $stmt->execute();
        $result = $stmt->get_result();
        $row = $result->fetch_assoc();
        $stmt->close();
        
        return $row ? $row['config_value'] : $default;
    }
    
    /**
     * Get all payment configuration
     */
    public function getAllConfig() {
        $config = [];
        $stmt = $this->conn->prepare("SELECT config_key, config_value FROM payment_config");
        if (!$stmt) {
            error_log("Failed to prepare payment config query: " . $this->conn->error);
            return $config;
        }
        
        $stmt->execute();
        $result = $stmt->get_result();
        
        while ($row = $result->fetch_assoc()) {
            $config[$row['config_key']] = $row['config_value'];
        }
        
        $stmt->close();
        return $config;
    }
    
    /**
     * Get payment phone number
     */
    public function getPaymentPhone() {
        return $this->getConfig('payment_phone_number', '');
    }
    
    /**
     * Get payment name
     */
    public function getPaymentName() {
        return $this->getConfig('payment_name', '');
    }
    
    /**
     * Get WhatsApp contact
     */
    public function getWhatsAppContact() {
        return $this->getConfig('whatsapp_contact', '');
    }
    
    /**
     * Get WhatsApp group link
     */
    public function getWhatsAppGroupLink() {
        return $this->getConfig('whatsapp_group_link', '');
    }
    
    /**
     * Get formatted payment instructions
     */
    public function getPaymentInstructions() {
        $phone = $this->getPaymentPhone();
        $name = $this->getPaymentName();
        return "Pay to <span class=\"font-bold bg-accent-500 text-white px-2 py-1 rounded\">{$phone} ({$name})</span>";
    }
    
    /**
     * Get WhatsApp agent registration link
     */
    public function getWhatsAppAgentLink() {
        $contact = $this->getWhatsAppContact();
        $message = urlencode("Hello%2C%20I%20am%20interested%20in%20registering%20as%20an%20agent.");
        return "https://wa.me/{$contact}?text={$message}";
    }
}
?>


