<?php
session_start();
require_once('mysqli_connect.php');

$errors = [];
$success = "";
$email = "";

function h($s) {
    return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8');
}

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

    // Validate email
    if ($email === '') {
        $errors[] = "Email is required.";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = "Please enter a valid email address.";
    }

    // Validate new password
    if ($pass1 === '') {
        $errors[] = "New password is required.";
    } elseif (strlen($pass1) < 6) {
        $errors[] = "New password must be at least 6 characters.";
    }

    // Validate confirm password
    if ($pass2 === '') {
        $errors[] = "Please confirm your new password.";
    } elseif ($pass1 !== $pass2) {
        $errors[] = "New password and confirm password do not match.";
    }

    if (empty($errors)) {
        $emailEsc = mysqli_real_escape_string($dbc, $email);
        $newPassEsc = mysqli_real_escape_string($dbc, $pass1);

        // Check if email exists
        $q = "SELECT user_id FROM users WHERE email = '$emailEsc' LIMIT 1";
        $r = mysqli_query($dbc, $q);

        if ($r && mysqli_num_rows($r) === 1) {
            $row = mysqli_fetch_assoc($r);
            $user_id = (int)$row['user_id'];
            
        // Update password 
        $newHash = password_hash($pass1, PASSWORD_DEFAULT);
            $q = "UPDATE users
                  SET password_hash = '". mysqli_real_escape_string($dbc, $newHash) ."'
                  WHERE user_id = $user_id
                  LIMIT 1";

            $r = mysqli_query($dbc, $q);

            if ($r && mysqli_affected_rows($dbc) === 1) {
                $success = "Your password has been reset successfully. You can now log in.";
                $email = "";
            } else {
                $errors[] = "Your password could not be reset. Please try again.";
            }
        } else {
            $errors[] = "That email address was not found in our system.";
        }
    }
}
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Forgot Password • Sideline</title>
  <link rel="icon" type="image/x-icon" href="images/sideline_logo.ico">
  <style>
    :root{
      --bg:#f6f7fb; --card:#fff; --text:#0f172a; --muted:#64748b;
      --line:#e5e7eb; --dark:#111827; --ok:#16a34a;
    }
    *{box-sizing:border-box}
    body{
      margin:0;
      font-family:system-ui,Segoe UI,Arial,sans-serif;
      background:linear-gradient(to top, #2B784A, #111827, #251F47);
      min-height: 100vh;
      color:var(--text);
    }
    .brand{font-weight:800; letter-spacing:.3px}
    .nav{display:flex; gap:10px; flex-wrap:wrap; align-items:center}
    .nav a{
      background:transparent;
      color:#fff;
      border:1px solid rgba(255,255,255,.18);
      padding:8px 10px;
      border-radius:12px;
      opacity:.95;
    }
    .nav a.active{border-color:#fff; opacity:1}
    .container{max-width:900px; margin:30px auto; padding:0 16px}
    .card{
      background: rgba(255, 255, 255, 0.45);
      border:1px solid var(--line);
      border-radius:16px;
      padding:20px;
      box-shadow:0 10px 30px rgba(2,6,23,.05);
    }
    h2,h3{margin:0 0 10px}
    p{margin:8px 0; color:var(--muted)}
    label{
      display:block;
      font-size:.9rem;
      color:#FFFFFF;
      margin:10px 0 6px;
    }
    input{
      width:100%;
      padding:10px 12px;
      border:1px solid var(--line);
      border-radius:12px;
      background:#fff;
    }
    .btn{
      background:#251F47;
      color:#fff;
      border:none;
      border-radius:12px;
      padding:10px 12px;
      cursor:pointer;
      display:inline-block;
    }
    .btn.secondary{
      background:#fff;
      color:var(--dark);
      border:1px solid var(--line);
    }
    .split{
      display:flex;
      gap:10px;
      flex-wrap:wrap;
      margin-top:14px;
    }
    .error{
      background:#fef2f2;
      border:1px solid #fecaca;
      color:#991b1b;
      padding:10px 12px;
      border-radius:12px;
      margin:10px 0;
    }
    .success{
      background:#ecfdf5;
      border:1px solid #bbf7d0;
      color:#166534;
      padding:10px 12px;
      border-radius:12px;
      margin:10px 0;
    }
    .notice{
      padding:10px 12px;
      border-radius:12px;
      border:1px dashed var(--line);
      background:#fafafa;
      color:#334155;
      margin-top:12px;
    }
  </style>
</head>
<body>

<main class="container">
  <div class="card">
    <h2 style="color:#FFFFFF">Reset Password</h2>
    <p style="color:#FFFFFF">Enter your email address and choose a new password.</p>

    <?php if (!empty($errors)): ?>
      <div class="error">
        <strong>Please fix:</strong>
        <ul>
          <?php foreach ($errors as $e): ?>
            <li><?= h($e) ?></li>
          <?php endforeach; ?>
        </ul>
      </div>
    <?php endif; ?>

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

    <form method="post" action="forgot_password.php" novalidate>
      <label>Email Address</label>
      <input
        type="email"
        name="email"
        value="<?= h($email) ?>"
        placeholder="name@email.com"
        required
      >

      <label>New Password</label>
      <input
        type="password"
        name="pass1"
        placeholder="Enter new password"
        required
      >

      <label>Confirm New Password</label>
      <input
        type="password"
        name="pass2"
        placeholder="Confirm new password"
        required
      >

      <div class="split">
        <button class="btn" type="submit">Reset Password</button>
        <a class="btn secondary" href="index.php">Back to Login</a>
      </div>
    </form>
  </div>
</main>

</body>
</html>