<?php

declare(strict_types=1);

/**
 * 付費評論購買區塊。
 * 需變數：$rv, $rvId, $mid, $masseurStoreId, $masseurCsrf, $loginNext, $masseurMemberId, $masseurVerified, $pdo, $storeMasseurNcoinEsc
 */

$buyerUsername = '';
if ($masseurMemberId > 0 && isset($masseurViewer) && is_array($masseurViewer)) {
    $buyerUsername = (string) ($masseurViewer['username'] ?? '');
}
$buyCheck = fate_masseur_review_paid_buy_check($pdo, $rv, $masseurMemberId, $masseurVerified, $buyerUsername);
$paidInlineFlash = fate_masseur_review_paid_flash_for_review($rvId);
$paidPrice = (int) ($rv['paid_price'] ?? 0);
$viewerBalance = ($masseurMemberId > 0 && $masseurVerified) ? fate_member_read_ncoin($pdo, $masseurMemberId, $buyerUsername) : null;

?>
<div class="masseur-review-paid-cta" id="masseur-review-paid-<?= (int) $rvId ?>">
    <?php if ($paidInlineFlash !== null && ($paidInlineFlash['msg'] ?? '') !== ''): ?>
        <div class="masseur-review-paid-alert masseur-review-paid-alert--<?= ($paidInlineFlash['type'] ?? '') === 'ok' ? 'ok' : 'bad' ?>" role="status">
            <?= htmlspecialchars((string) $paidInlineFlash['msg'], ENT_QUOTES, 'UTF-8') ?>
        </div>
    <?php endif; ?>

    <?php if ($buyCheck['code'] === 'not_logged_in'): ?>
        <p class="masseur-review-paid-cta__text"><?= htmlspecialchars((string) $buyCheck['message'], ENT_QUOTES, 'UTF-8') ?></p>
        <p class="masseur-review-paid-cta__meta">售價 <strong><?= number_format($paidPrice) ?></strong> <?= htmlspecialchars($storeMasseurNcoinEsc, ENT_QUOTES, 'UTF-8') ?></p>
        <a class="masseur-btn masseur-btn--sm" href="login.php?next=<?= $loginNext ?>">登入購買</a>
    <?php elseif ($buyCheck['code'] === 'not_verified'): ?>
        <div class="masseur-review-paid-alert masseur-review-paid-alert--warn" role="status">
            <?= htmlspecialchars((string) $buyCheck['message'], ENT_QUOTES, 'UTF-8') ?>
        </div>
        <p class="masseur-review-paid-cta__meta">售價 <strong><?= number_format($paidPrice) ?></strong> <?= htmlspecialchars($storeMasseurNcoinEsc, ENT_QUOTES, 'UTF-8') ?></p>
        <a class="masseur-btn masseur-btn--sm" href="member_profile.php">前往 Email 驗證</a>
    <?php elseif (!$buyCheck['can_buy']): ?>
        <div class="masseur-review-paid-alert masseur-review-paid-alert--bad" role="alert">
            <?= htmlspecialchars((string) $buyCheck['message'], ENT_QUOTES, 'UTF-8') ?>
        </div>
        <?php if ($buyCheck['code'] === 'insufficient_balance' && $viewerBalance !== null): ?>
            <p class="masseur-review-paid-cta__meta">
                您的 <?= htmlspecialchars($storeMasseurNcoinEsc, ENT_QUOTES, 'UTF-8') ?>：<strong><?= number_format((int) $viewerBalance) ?></strong>
                · 尚需 <strong class="masseur-review-paid-cta__shortfall"><?= number_format((int) $buyCheck['shortfall']) ?></strong>
            </p>
            <a class="masseur-btn masseur-btn--sm" href="member_profile.php#ncoin">查看 <?= htmlspecialchars($storeMasseurNcoinEsc, ENT_QUOTES, 'UTF-8') ?>紀錄</a>
        <?php endif; ?>
    <?php else: ?>
        <p class="masseur-review-paid-cta__text">購買後可閱讀完整內容</p>
        <p class="masseur-review-paid-cta__meta">
            售價 <strong><?= number_format($paidPrice) ?></strong> <?= htmlspecialchars($storeMasseurNcoinEsc, ENT_QUOTES, 'UTF-8') ?>
            <?php if ($viewerBalance !== null): ?>
                · 您的 <?= htmlspecialchars($storeMasseurNcoinEsc, ENT_QUOTES, 'UTF-8') ?> <strong><?= number_format((int) $viewerBalance) ?></strong>
                <?php if ((int) $viewerBalance >= $paidPrice): ?>
                    <span class="masseur-review-paid-cta__ok">（足夠）</span>
                <?php endif; ?>
            <?php endif; ?>
        </p>
        <form method="post" action="api/masseur_action.php" class="masseur-review-paid-buy-form">
            <input type="hidden" name="csrf" value="<?= htmlspecialchars($masseurCsrf, ENT_QUOTES, 'UTF-8') ?>">
            <input type="hidden" name="action" value="buy_paid_review">
            <input type="hidden" name="store_id" value="<?= (int) $masseurStoreId ?>">
            <input type="hidden" name="masseur_id" value="<?= (int) $mid ?>">
            <input type="hidden" name="review_id" value="<?= (int) $rvId ?>">
            <button type="submit" class="masseur-btn masseur-btn--sm" onclick="return confirm('確定花費 <?= (int) $paidPrice ?> <?= htmlspecialchars($storeMasseurNcoinEsc, ENT_QUOTES, 'UTF-8') ?>購買此評論？');">購買全文</button>
        </form>
    <?php endif; ?>
</div>
