<?php
// Fichier inclus par index.php pour l'envoi d'email de gage
// NE PAS modifier - appeler sendBetEmail() directement

function sendBetEmail($pdo, $betId, $senderId, $receiverId) {
    if (!$receiverId) return;
    try {
        $bet = $pdo->prepare("SELECT * FROM bets WHERE id=?");
        $bet->execute([$betId]);
        $betRow = $bet->fetch();

        $snd = $pdo->prepare("SELECT username,email FROM users WHERE id=?");
        $snd->execute([$senderId]);
        $sRow = $snd->fetch();

        $rcv = $pdo->prepare("SELECT username,email FROM users WHERE id=?");
        $rcv->execute([$receiverId]);
        $rRow = $rcv->fetch();

        if (!$betRow || !$sRow || !$rRow || empty($rRow['email'])) return;

        $lanceur = htmlspecialchars($sRow['username']);
        $dest    = htmlspecialchars($rRow['username']);
        $desc    = htmlspecialchars($betRow['description'] ?? '');
        $montant = (int)($betRow['amount'] ?? 0);
        $jetons  = (int)($betRow['wallet_stake'] ?? 0);
        $subject = $lanceur . " vous lance un gage sur JeTeGageQue !";

        $jetonsHtml = $jetons > 0
            ? ' &mdash; <span style="color:#0dcaf0;">' . $jetons . ' jetons</span>'
            : '';

        $htmlBody  = '<!DOCTYPE html><html><head><meta charset="UTF-8"></head>';
        $htmlBody .= '<body style="margin:0;background:#050510;font-family:Arial,sans-serif;">';
        $htmlBody .= '<div style="max-width:520px;margin:0 auto;background:#0d0d1a;border:1px solid #1a1a3a;border-radius:8px;overflow:hidden;">';
        $htmlBody .= '<div style="background:#050520;padding:14px 20px;border-bottom:2px solid #0dcaf0;text-align:center;">';
        $htmlBody .= '<div style="font-size:20px;font-weight:900;color:#0dcaf0;letter-spacing:3px;">JETEGAGEQUE</div>';
        $htmlBody .= '</div>';
        $htmlBody .= '<div style="padding:20px;">';
        $htmlBody .= '<h2 style="color:#ffc107;font-size:16px;">Nouveau gage !</h2>';
        $htmlBody .= '<div style="background:#080810;border-left:3px solid #ffc107;padding:12px;border-radius:0 5px 5px 0;color:#ddd;font-size:14px;line-height:1.6;">';
        $htmlBody .= '<strong>' . $lanceur . '</strong> vous lance un gage :<br><br>';
        $htmlBody .= '&ldquo;' . $desc . '&rdquo;<br><br>';
        $htmlBody .= '<span style="color:#ffc107;">Montant : ' . $montant . ' pts</span>' . $jetonsHtml;
        $htmlBody .= '</div>';
        $htmlBody .= '<div style="margin-top:16px;text-align:center;">';
        $htmlBody .= '<a href="https://jetegageque.com/jetegageque/" style="background:#ffc107;color:#000;padding:10px 24px;border-radius:6px;text-decoration:none;font-weight:700;">Voir le gage</a>';
        $htmlBody .= '</div></div>';
        $htmlBody .= '<div style="padding:12px 20px;background:#060610;font-size:10px;color:#888;">JeTeGageQue &mdash; Ne pas repondre.</div>';
        $htmlBody .= '</div></body></html>';

        $headers  = "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
        $headers .= "From: JeTeGageQue <noreply@jetegageque.com>\r\n";

        @mail($rRow['email'], '=?UTF-8?B?' . base64_encode($subject) . '?=', $htmlBody, $headers);

    } catch (Exception $e) {
        // Silencieux
    }
}

function sendHonorEmail($pdo, $betId, $winnerId, $loserId) {
    try {
        $bet = $pdo->prepare("SELECT * FROM bets WHERE id=?"); $bet->execute([$betId]); $betRow=$bet->fetch();
        $w = $pdo->prepare("SELECT username,email FROM users WHERE id=?"); $w->execute([$winnerId]); $wRow=$w->fetch();
        $l = $pdo->prepare("SELECT username,email FROM users WHERE id=?"); $l->execute([$loserId]); $lRow=$l->fetch();
        if (!$betRow || !$wRow) return;

        foreach ([[$wRow,'gagné','#28a745'],[$lRow,'perdu','#dc3545']] as [$r,$res,$col]) {
            if (empty($r['email'])) continue;
            $subject = "Résultat du gage #$betId — JeTeGageQue";
            $desc = htmlspecialchars($betRow['description'] ?? '');
            $html  = '<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body style="margin:0;background:#050510;font-family:Arial;">';
            $html .= '<div style="max-width:520px;margin:0 auto;background:#0d0d1a;border:1px solid #1a1a3a;border-radius:8px;overflow:hidden;">';
            $html .= '<div style="background:#050520;padding:14px 20px;border-bottom:2px solid '.$col.';text-align:center;">';
            $html .= '<div style="font-size:20px;font-weight:900;color:#0dcaf0;letter-spacing:3px;">JETEGAGEQUE</div></div>';
            $html .= '<div style="padding:20px;">';
            $html .= '<h2 style="color:'.$col.';font-size:16px;">Gage '.strtoupper($res).' !</h2>';
            $html .= '<p style="color:#ddd;font-size:14px;">Le gage &ldquo;'.$desc.'&rdquo; a été honoré.</p>';
            $html .= '<p style="color:'.$col.';font-size:16px;font-weight:700;">Vous avez <strong>'.$res.'</strong> ce gage.</p>';
            $html .= '<div style="text-align:center;margin-top:16px;"><a href="https://jetegageque.com/jetegageque/" style="background:'.$col.';color:#fff;padding:10px 24px;border-radius:6px;text-decoration:none;font-weight:700;">Voir mon profil</a></div>';
            $html .= '</div><div style="padding:12px 20px;background:#060610;font-size:10px;color:#555;">JeTeGageQue — Ne pas répondre.</div></div></body></html>';
            $headers = "MIME-Version: 1.0\r\nContent-Type: text/html; charset=UTF-8\r\nFrom: JeTeGageQue <noreply@jetegageque.com>\r\n";
            @mail($r['email'], '=?UTF-8?B?'.base64_encode($subject).'?=', $html, $headers);
        }
    } catch (Exception $e) {}
}
