<?php
require_once 'config.php';

$token = clean($_GET['token'] ?? '');
$errors = [];
$success = false;

if (empty($token)) {
    header('Location: ' . SITE_URL . '/forgot-password.php');
    exit;
}

// Validate token
$stmt = $conn->prepare("SELECT id, full_name FROM patients WHERE reset_token=? AND reset_expires > NOW()");
$stmt->bind_param('s', $token);
$stmt->execute();
$patient = $stmt->get_result()->fetch_assoc();

if (!$patient) {
    $invalid = true;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !isset($invalid)) {
    $new     = $_POST['new_password'] ?? '';
    $confirm = $_POST['confirm_password'] ?? '';

    if (strlen($new) < 6) {
        $errors[] = 'Password must be at least 6 characters.';
    } elseif ($new !== $confirm) {
        $errors[] = 'Passwords do not match.';
    } else {
        $hash = password_hash($new, PASSWORD_BCRYPT);
        $upd  = $conn->prepare("UPDATE patients SET password=?, reset_token=NULL, reset_expires=NULL WHERE id=?");
        $upd->bind_param('si', $hash, $patient['id']);
        $upd->execute();
        $success = true;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Reset Password - HealthLink</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
  <style>
    body { background: linear-gradient(135deg,#e6f5f3,#f0f9ff); min-height: 100vh; font-family: 'Segoe UI',sans-serif; display: flex; align-items: center; justify-content: center; }
    .auth-card { background: white; border-radius: 20px; padding: 40px; max-width: 440px; width: 100%; box-shadow: 0 8px 40px rgba(11,123,110,0.12); }
    .brand { text-align: center; margin-bottom: 28px; }
    .brand-icon { width: 60px; height: 60px; border-radius: 16px; background: linear-gradient(135deg,#0B7B6E,#13A898); display: flex; align-items: center; justify-content: center; margin: 0 auto 12px; font-size: 1.8rem; color: white; }
  </style>
</head>
<body>
<div class="auth-card">
  <div class="brand">
    <div class="brand-icon">🔐</div>
    <h4 style="font-weight:800;color:#0D2340;">Reset Password</h4>
    <?php if (!isset($invalid) && !$success): ?>
      <p style="color:#718096;font-size:0.88rem;">Hello, <?= htmlspecialchars($patient['full_name']) ?></p>
    <?php endif; ?>
  </div>

  <?php if (isset($invalid)): ?>
    <div class="alert alert-danger text-center">
      <i class="fas fa-exclamation-triangle fa-2x mb-2 d-block"></i>
      <strong>Link Expired or Invalid</strong><br>
      This reset link has expired or already been used.
    </div>
    <a href="<?= SITE_URL ?>/forgot-password.php" class="btn btn-success w-100">Request New Link</a>
  <?php elseif ($success): ?>
    <div class="alert alert-success text-center">
      <i class="fas fa-check-circle fa-2x mb-2 d-block"></i>
      <strong>Password Reset Successfully!</strong><br>
      You can now login with your new password.
    </div>
    <a href="<?= SITE_URL ?>/patient-login.php" class="btn btn-success w-100">
      <i class="fas fa-sign-in-alt me-1"></i> Go to Login
    </a>
  <?php else: ?>
    <?php if (!empty($errors)): ?>
      <div class="alert alert-danger"><?= htmlspecialchars($errors[0]) ?></div>
    <?php endif; ?>
    <form method="POST">
      <div class="mb-3">
        <label class="form-label fw-600">New Password</label>
        <input type="password" name="new_password" class="form-control form-control-lg" placeholder="Min 6 characters" required autofocus>
      </div>
      <div class="mb-4">
        <label class="form-label fw-600">Confirm New Password</label>
        <input type="password" name="confirm_password" class="form-control form-control-lg" placeholder="Repeat password" required>
      </div>
      <button type="submit" class="btn btn-success w-100 btn-lg">
        <i class="fas fa-save me-1"></i> Save New Password
      </button>
    </form>
  <?php endif; ?>
</div>
</body>
</html>
