<?php
// ZiniPay Configuration — loaded from settings table
// Configure at: /admin/zinipay_settings.php

$cfg = [
    'api_key'      => '',
    'enabled'      => false,
    'mode'         => 'sandbox',
    'label'        => 'ZiniPay (bKash/Nagad/Card)',
    'api_base'     => 'https://api.zinipay.com/v1',
    'redirect_url' => (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . ($_SERVER['HTTP_HOST'] ?? '') . '/fees/zinipay_callback.php',
    'webhook_url'  => (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . ($_SERVER['HTTP_HOST'] ?? '') . '/fees/zinipay_webhook.php',
];

// Try every possible way to get $pdo
$_pdo = null;
if (isset($pdo))             $_pdo = $pdo;
elseif (isset($GLOBALS['pdo'])) $_pdo = $GLOBALS['pdo'];
else {
    // Last resort: include database config
    $db_path = __DIR__ . '/database.php';
    if (file_exists($db_path)) {
        require_once $db_path;
        if (isset($pdo)) $_pdo = $pdo;
    }
}

if ($_pdo) {
    try {
        $stmt = $_pdo->query("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'zinipay_%'");
        while ($row = $stmt->fetch()) {
            switch ($row['setting_key']) {
                case 'zinipay_api_key': $cfg['api_key'] = $row['setting_value']; break;
                case 'zinipay_enabled': $cfg['enabled'] = $row['setting_value'] === '1'; break;
                case 'zinipay_mode':    $cfg['mode']    = $row['setting_value']; break;
                case 'zinipay_label':   $cfg['label']   = $row['setting_value']; break;
            }
        }
    } catch (Exception $e) { /* use defaults */ }
}

return $cfg;
