<?php
/**
 * Contact Configuration Helper Class
 * Manages contact-related configuration settings for dynamic footer
 */

class ContactConfig {
    private $conn;
    
    public function __construct($connection) {
        $this->conn = $connection;
    }
    
    /**
     * Get contact 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 contact 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 contact configuration
     */
    public function getAllContactConfig() {
        $config = [];
        $contact_keys = [
            'payment_phone_number', 
            'payment_name', 
            'whatsapp_contact', 
            'whatsapp_group_link',
            'contact_email',
            'business_address',
            'company_name'
        ];
        
        foreach ($contact_keys as $key) {
            $config[$key] = $this->getConfig($key);
        }
        
        return $config;
    }
    
    /**
     * Get contact phone number
     */
    public function getContactPhone() {
        return $this->getConfig('payment_phone_number', '+233 54 280 1117');
    }
    
    /**
     * Get contact email
     */
    public function getContactEmail() {
        return $this->getConfig('contact_email', 'support@megabytehub.com');
    }
    
    /**
     * Get WhatsApp contact
     */
    public function getWhatsAppContact() {
        return $this->getConfig('whatsapp_contact', '+233 54 280 1117');
    }
    
    /**
     * Get WhatsApp group link
     */
    public function getWhatsAppGroupLink() {
        return $this->getConfig('whatsapp_group_link', '');
    }
    
    /**
     * Get business address
     */
    public function getBusinessAddress() {
        return $this->getConfig('business_address', 'Ghana');
    }
    
    /**
     * Get company name
     */
    public function getCompanyName() {
        return $this->getConfig('company_name', 'D-Bundles Stores');
    }
    
    /**
     * Get payment name
     */
    public function getPaymentName() {
        return $this->getConfig('payment_name', 'D-Bundles Stores');
    }
    
    /**
     * Format phone number for WhatsApp link
     */
    public function getWhatsAppLink() {
        $phone = $this->getWhatsAppContact();
        // Remove any non-numeric characters except +
        $phone = preg_replace('/[^0-9+]/', '', $phone);
        // Remove + if present and add country code if missing
        if (strpos($phone, '+') === 0) {
            $phone = substr($phone, 1);
        }
        return 'https://wa.me/' . $phone;
    }
}
