<?php
$pageTitle = 'Premium · Velora';
require __DIR__ . '/../layouts/header.php';
$price = isset($price) ? (float) $price : monthly_price_kes();
$hasAccess = !empty($hasAccess);
$isPaid = !empty($isPaid);
$trialLeft = (int) ($trialLeft ?? 0);
$paySettings = $paySettings ?? payment_settings();
$userRow = $userRow ?? [];
$defaultPhone = $userRow['phone'] ?? ($u['phone'] ?? '');
?>
<section class="page-head">
  <h1>Monthly Access</h1>
  <p class="muted">KES <?= number_format($price) ?> / month · Full discovery, matches & messaging</p>
</section>

<?php if ($isPaid): ?>
  <div class="notice success" style="margin-bottom:16px">
    <strong>Verified member</strong> <?= paid_badge_html((int)$u['id']) ?>
    — your blue badge is active.
    <?php if (!empty($userRow['paid_until'])): ?>
      Access until <?= e(date('M j, Y', strtotime($userRow['paid_until']))) ?>.
    <?php endif; ?>
  </div>
<?php elseif ($trialLeft > 0): ?>
  <div class="notice" style="margin-bottom:16px">
    <strong>Free trial:</strong> about <?= (int)$trialLeft ?> hour(s) left.
    After that, Monthly Access (KES <?= number_format($price) ?>) is required.
  </div>
<?php else: ?>
  <div class="notice error" style="margin-bottom:16px">
    <strong>Trial ended.</strong> Pay KES <?= number_format($price) ?> to unlock the community and get your blue verified badge.
  </div>
<?php endif; ?>


<?php
  $mpesaReady = !empty($paySettings['mpesa_enabled']) && !empty($paySettings['mpesa_consumer_key']) && !empty($paySettings['mpesa_shortcode']);
  $stripeReady = !empty($paySettings['card_enabled']) && !empty($paySettings['card_secret_key']);
?>
<p class="muted" style="font-size:13px;margin-bottom:12px">
  M-Pesa: <?= $mpesaReady ? '<span class="status-paid">configured</span>' : '<span class="status-failed">not configured</span>' ?>
  · Cards (Stripe): <?= $stripeReady ? '<span class="status-paid">configured</span>' : '<span class="status-failed">not configured</span>' ?>
</p>
<div class="grid two section">
  <section class="panel pay-panel">
    <h2>Pay with M-Pesa <small class="muted">(Safaricom Daraja STK)</small></h2>
    <p class="muted">Kenya mobile money. Enter your Safaricom number — you get an STK PIN prompt. Automatic retries on network errors.</p>
    <form method="post" action="<?= url('premium/mpesa') ?>" class="stack"><?= csrf() ?>
      <label>
        Phone number
        <input type="tel" name="phone" required placeholder="07XX XXX XXX" value="<?= e($defaultPhone) ?>" autocomplete="tel">
      </label>
      <p class="muted" style="font-size:13px">Amount: <strong>KES <?= number_format($price) ?></strong></p>
      <button class="btn" type="submit">Send M-Pesa prompt</button>
    </form>
  </section>
  <section class="panel pay-panel">
    <h2>Pay with card <small class="muted">(Stripe Checkout)</small></h2>
    <p class="muted">International Visa / Mastercard / Amex via Stripe. Card details stay on Stripe — not stored on Velora.</p>
    <form method="post" action="<?= url('premium/card') ?>" class="stack"><?= csrf() ?>
      <p class="muted" style="font-size:13px">Amount: <strong>KES <?= number_format($price) ?></strong></p>
      <button class="btn ghost" type="submit">Pay with card</button>
    </form>
  </section>
</div>


<section class="panel section compare-section" style="margin-top:16px">
  <h2>Trial vs paid (clear rules)</h2>
  <p class="muted" style="margin-top:0;font-size:13px">Messaging stays paid-only so chats stay genuine. Trial is for discovery and matching.</p>
  <div class="table-wrap">
    <table class="compare-table" aria-label="Trial versus paid features">
      <thead>
        <tr>
          <th scope="col">Feature</th>
          <th scope="col">72h free trial</th>
          <th scope="col">Trial ended (unpaid)</th>
          <th scope="col">Paid Monthly Access</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th scope="row">Profile / photos</th>
          <td><span class="yes">Yes</span></td>
          <td><span class="yes">Yes</span></td>
          <td><span class="yes">Yes</span></td>
        </tr>
        <tr>
          <th scope="row">Discover, swipe, like, pass</th>
          <td><span class="yes">Yes</span></td>
          <td><span class="no">Blocked → Premium</span></td>
          <td><span class="yes">Yes</span></td>
        </tr>
        <tr>
          <th scope="row">Rewind</th>
          <td><span class="yes">Yes</span></td>
          <td><span class="no">No</span></td>
          <td><span class="yes">Yes</span></td>
        </tr>
        <tr>
          <th scope="row">Inbox / chat / calls</th>
          <td><span class="no">No</span></td>
          <td><span class="no">No</span></td>
          <td><span class="yes">Yes</span></td>
        </tr>
        <tr>
          <th scope="row">Blue verified badge</th>
          <td><span class="no">No</span></td>
          <td><span class="no">No</span></td>
          <td><span class="yes">Yes (30 days)</span></td>
        </tr>
      </tbody>
    </table>
  </div>
</section>

<div class="grid two section" style="margin-top:16px">
  <section class="panel">
    <h2>What you get</h2>
    <ul>
      <li>Full access to all available members</li>
      <li>Unlimited likes & matches</li>
      <li><strong>Inbox &amp; messaging</strong> with matches (paid members only)</li>
      <li>Icebreaker suggestions to start chats</li>
      <li><span class="paid-badge">✓</span> Blue verified badge</li>
      <li>Priority in discovery</li>
    </ul>
  </section>
  <section class="panel">
    <h2>Payment history</h2>
    <?php if (!can_foreach($tx)): ?>
      <p class="muted">No payments yet.</p>
    <?php else: ?>
      <div class="list compact-list">
        <?php foreach ($tx as $t): if (!is_array($t)) continue; ?>
          <div class="match compact-row">
            <div class="match-info">
              <b><?= e($t['plan_name'] ?? 'Monthly Access') ?></b>
              <p>
                <?= e($t['currency'] ?? 'KES') ?> <?= number_format((float)($t['amount'] ?? 0), 2) ?>
                · <?= e($t['provider'] ?? '') ?> · <?= e($t['status'] ?? '') ?>
                <?php if (!empty($t['reference'])): ?> · <?= e($t['reference']) ?><?php endif; ?>
              </p>
              <?php if (($t['provider'] ?? '') === 'mpesa' && in_array($t['status'] ?? '', ['pending', 'failed'], true)): ?>
                <form method="post" action="<?= url('premium/mpesa/retry') ?>" style="margin-top:6px"><?= csrf() ?>
                  <input type="hidden" name="tx_id" value="<?= (int)($t['id'] ?? 0) ?>">
                  <button class="btn ghost small" type="submit">Retry M-Pesa prompt</button>
                </form>
              <?php endif; ?>
            </div>
            <small class="muted"><?= !empty($t['created_at']) ? e(date('M j, H:i', strtotime($t['created_at']))) : '' ?></small>
          </div>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>
  </section>
</div>

<p class="notice muted" style="margin-top:16px;font-size:13px">
  M-Pesa: Safaricom Daraja STK Push (callback verified against your pending CheckoutRequestID). Cards: Stripe Checkout — no card numbers are stored on this server.
</p>
<?php require __DIR__ . '/../layouts/footer.php'; ?>
