<?php
// ─── SunScope Extra — Restore Access ─────────────────────────────────────────
// Drop this file on your VPS alongside index.html.
// Paste your Stripe SECRET key below (never the publishable key).
// Swap sk_test_... for sk_live_... when you go live.

define('STRIPE_SECRET_KEY', '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');
define('SUNSCOPE_URL', 'https://sunscope.net');

$error   = '';
$success = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim(strtolower($_POST['email'] ?? ''));

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Please enter a valid email address.';
    } else {
        // Search Stripe for a customer with this email
        $ch = curl_init('https://api.stripe.com/v1/customers?email=' . urlencode($email) . '&limit=5');
        curl_setopt_array($ch, [
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_USERPWD        => STRIPE_SECRET_KEY . ':',
        ]);
        $response = json_decode(curl_exec($ch), true);
        curl_close($ch);

        $isActive = false;

        if (!empty($response['data'])) {
            foreach ($response['data'] as $customer) {
                // Check subscriptions for this customer
                $ch2 = curl_init('https://api.stripe.com/v1/subscriptions?customer=' . $customer['id'] . '&status=active&limit=5');
                curl_setopt_array($ch2, [
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_USERPWD        => STRIPE_SECRET_KEY . ':',
                ]);
                $subs = json_decode(curl_exec($ch2), true);
                curl_close($ch2);

                if (!empty($subs['data'])) {
                    $isActive = true;
                    break;
                }
            }
        }

        if ($isActive) {
            // Active subscriber — redirect with pro=1
            header('Location: ' . SUNSCOPE_URL . '/?pro=1');
            exit;
        } else {
            $error = 'No active SunScope Extra subscription found for that email.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Restore Access · SunScope Extra</title>
  <link rel="icon" type="image/svg+xml" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>☀️</text></svg>" />
  <link href="https://fonts.bunny.net/css2?family=Fraunces:ital,wght@0,900;1,300&family=Manrope:wght@400;600&family=JetBrains+Mono:wght@400;600&display=swap" rel="stylesheet" />
  <style>
    *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
    body {
      font-family: Manrope, sans-serif;
      background: #f5edd6;
      color: #1e1208;
      min-height: 100vh;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding: 2rem;
    }
    .card {
      background: #fffcf2;
      border: 1.5px solid #c9b08a;
      border-radius: 6px;
      padding: 2.5rem 2.5rem 2rem;
      max-width: 420px;
      width: 100%;
      text-align: center;
    }
    .logo {
      font-family: Fraunces, serif;
      font-size: 2rem;
      line-height: 1;
      margin-bottom: 0.4rem;
      letter-spacing: -0.02em;
    }
    .logo-sun   { color: #c8922a; font-weight: 900; font-style: normal; }
    .logo-scope { color: #1e1208; font-weight: 300; font-style: italic; }
    .tagline {
      font-family: Fraunces, serif;
      font-style: italic;
      font-size: 0.95rem;
      color: #7a5c2a;
      margin-bottom: 2rem;
    }
    h1 {
      font-family: Fraunces, serif;
      font-weight: 900;
      font-size: 1.15rem;
      margin-bottom: 0.5rem;
    }
    p {
      font-size: 0.9rem;
      color: #4a3420;
      line-height: 1.6;
      margin-bottom: 1.5rem;
    }
    input[type="email"] {
      width: 100%;
      padding: 10px 14px;
      border: 1.5px solid #c9b08a;
      border-radius: 4px;
      font-family: Manrope, sans-serif;
      font-size: 0.95rem;
      background: #fff;
      color: #1e1208;
      margin-bottom: 0.75rem;
      outline: none;
    }
    input[type="email"]:focus { border-color: #c8922a; }
    button {
      width: 100%;
      padding: 11px;
      background: #c8922a;
      color: #fff;
      border: none;
      border-radius: 4px;
      font-family: 'Manrope', sans-serif;
      font-weight: 700;
      font-size: 0.75rem;
      text-transform: uppercase;
      letter-spacing: 0.07em;
      cursor: pointer;
    }
    button:hover { background: #a06e18; }
    .error {
      margin-top: 1rem;
      padding: 10px 14px;
      background: #fdf0e0;
      border: 1px solid #c9b08a;
      border-left: 3px solid #c8922a;
      border-radius: 3px;
      font-size: 0.85rem;
      color: #4a3420;
      text-align: left;
    }
    .back {
      margin-top: 1.5rem;
      font-family: 'Manrope', sans-serif;
      font-weight: 700;
      font-size: 0.7rem;
      text-transform: uppercase;
      letter-spacing: 0.06em;
    }
    .back a { color: #b09870; text-decoration: none; }
    .back a:hover { color: #c8922a; }
  </style>
</head>
<body>
  <div class="card">
    <div class="logo">
      <span class="logo-sun">SUN</span><span class="logo-scope">Scope</span>
    </div>
    <div class="tagline">See the sun the way your skin does.</div>

    <h1>Restore your access</h1>
    <p>Enter the email address you used to subscribe and we'll unlock SunScope Extra on this browser.</p>

    <form method="POST" action="">
      <input
        type="email"
        name="email"
        placeholder="your@email.com"
        value="<?= htmlspecialchars($_POST['email'] ?? '') ?>"
        required
        autofocus
      />
      <button type="submit">Restore access →</button>
    </form>

    <?php if ($error): ?>
      <div class="error"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <div class="back"><a href="/">← Back to SunScope</a></div>
  </div>
</body>
</html>
