<?php
// User dual currency helper
// Usage: include this file after requireLogin() and getDB()
// Provides: $userCur (preferred currency), $bdtRate, $dualPrice($bdt) function

function getUserPreferredCurrency($db, $userId) {
    try {
        $stmt = $db->prepare("SELECT c.* FROM users u JOIN currencies c ON u.preferred_currency=c.code WHERE u.id=? AND c.is_active=1");
        $stmt->execute([$userId]);
        return $stmt->fetch() ?: null;
    } catch(Exception $e) { return null; }
}

function dualCurrency($bdtAmount, $userCur) {
    if (!$userCur || $userCur['code'] === 'BDT') return '';
    $converted = round(floatval($bdtAmount) * floatval($userCur['rate']), 2);
    return ' <span style="color:#888;font-size:0.8em;font-weight:500">≈ ' . htmlspecialchars($userCur['symbol']) . number_format($converted, 2) . ' ' . $userCur['code'] . '</span>';
}

function dualBadge($bdtAmount, $userCur) {
    if (!$userCur || $userCur['code'] === 'BDT') return '';
    $converted = round(floatval($bdtAmount) * floatval($userCur['rate']), 2);
    return '<div style="font-size:11px;color:#888;margin-top:2px">' . htmlspecialchars($userCur['symbol']) . number_format($converted, 2) . ' ' . $userCur['code'] . '</div>';
}
