<?php
require_once __DIR__ . '/includes/auth.php';
chat247_start_session();
$err = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    $relay = chat247_relay_url();
    $ch = curl_init("$relay/api/login");
    curl_setopt_array($ch, [
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
        CURLOPT_POSTFIELDS => json_encode(['email'=>$email,'password'=>$password]),
        CURLOPT_TIMEOUT => 10,
    ]);
    $resp = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    $data = $resp ? json_decode($resp, true) : null;
    if ($code === 200 && !empty($data['bearer'])) {
        chat247_set_bearer($data['bearer'], $data['user']);
        // Allow ?next= bounce-back for admin.247ch.at + sibling subdomains.
        // Only accept https URLs on a 247ch.at host to avoid open-redirect.
        $next = (string)($_POST['next'] ?? $_GET['next'] ?? '/dashboard.php');
        if (preg_match('#^https://[a-z0-9-]+\.?247ch\.at(/.*)?$#i', $next)) {
            header('Location: ' . $next);
        } else {
            header('Location: /dashboard.php');
        }
        exit;
    }
    $err = $data['error'] ?? 'Sign-in failed';
}
?><!doctype html>
<html lang="en"><head>
<base href="https://<?= htmlspecialchars($_SERVER['HTTP_HOST'] ?? '', ENT_QUOTES) ?>/"><meta charset="utf-8"><title>Sign in — 247ch.at</title>
<link rel="icon" href="/favicon.ico?v=<?= @filemtime(__DIR__ . '/favicon.ico') ?: time() ?>">
<style>
 body { font-family:-apple-system,sans-serif; background:#0b0f1a; color:#f3f4f6; display:flex; flex-direction:column; align-items:center; justify-content:center; min-height:100vh; margin:0; padding:24px 0; }
 .card { background:#111827; padding:32px; border-radius:12px; width:360px; }
 .legal-mini { margin-top:24px; font-size:12px; color:#6b7280; text-align:center; }
 .legal-mini a { color:#6b7280; text-decoration:none; margin:0 6px; }
 .legal-mini a:hover { color:#9ca3af; }
 .card h1 { margin:0 0 24px; font-size:22px; text-align:center; }
 .google { display:block; width:100%; padding:12px; background:#fff; color:#111; border:none; border-radius:8px; font-weight:600; text-align:center; text-decoration:none; }
 .or { text-align:center; color:#9ca3af; margin:16px 0; font-size:13px; }
 label { display:block; font-size:13px; color:#9ca3af; margin-top:12px; }
 input { width:100%; padding:10px; background:#0b0f1a; border:1px solid #2a3142; color:#f3f4f6; border-radius:6px; }
 button { width:100%; margin-top:16px; padding:12px; background:#3b82f6; color:#fff; border:none; border-radius:8px; font-weight:600; }
 .err { color:#f87171; font-size:13px; margin-top:12px; text-align:center; }
 .links { text-align:center; margin-top:16px; font-size:13px; }
 .links a { color:#9ca3af; text-decoration:none; }
</style></head><body>
<div class="card">
  <h1>Sign in to 247ch.at</h1>
  <a class="google" href="/oauth/google">Sign in with Google</a>
  <div class="or">or</div>
  <form method="post">
    <?php $nextVal = (string)($_GET['next'] ?? ''); if ($nextVal !== ''): ?>
      <input type="hidden" name="next" value="<?= htmlspecialchars($nextVal) ?>">
    <?php endif; ?>
    <label>Email</label>
    <input type="email" name="email" required autofocus>
    <label>Password</label>
    <input type="password" name="password" required>
    <button type="submit">Sign in</button>
    <?php if ($err): ?><div class="err"><?=htmlspecialchars($err)?></div><?php endif; ?>
  </form>
  <div class="links">
    <a href="/forgot">Forgot password?</a> &middot;
    <a href="/signup">Create account</a>
  </div>
</div>
<div class="legal-mini">
  <a href="/pricing">Pricing</a> &middot;
  <a href="/privacy">Privacy</a> &middot;
  <a href="/terms">Terms</a> &middot;
  <a href="mailto:support@247ch.at">support@247ch.at</a>
</div>
</body></html>
