<?php
require_once 'config.php';
require_once 'functions.php';

if (isLoggedIn()) redirect('dashboard.php');

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    csrf_verify();

    $email    = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';

    $stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
    $stmt->execute([$email]);
    $user = $stmt->fetch();

    // verifyPassword() supporte SHA-256 legacy ET bcrypt, et migre auto vers bcrypt
    if ($user && verifyPassword($pdo, $user, $password)) {
        session_regenerate_id(true);
        $_SESSION['user_id']   = $user['id'];
        $_SESSION['user_name'] = $user['name'];
        $_SESSION['is_admin']  = (bool)$user['is_admin'];
        redirect('dashboard.php');
    } else {
        $error = "Email ou mot de passe incorrect.";
    }
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Connexion - First Car</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<?php include 'header.php'; ?>
<main class="container">
  <div class="form-wrapper">
    <div style="text-align:center;margin-bottom:2rem;">
      <h1 style="font-family:'Cormorant Garamond',serif;font-size:2.5rem;font-weight:700;">Connexion</h1>
      <p style="color:var(--light2);font-size:.88rem;margin-top:.5rem;font-weight:300;">Bienvenue ! Connectez-vous pour accéder à votre espace.</p>
    </div>

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

    <div class="form">
      <form method="post">
        <?= csrf_field() ?>
        <div class="form-group">
          <label for="email">Adresse email</label>
          <input type="email" id="email" name="email" placeholder="vous@exemple.com" required autocomplete="email"
                 value="<?= isset($_POST['email']) ? sanitize($_POST['email']) : '' ?>">
        </div>
        <div class="form-group">
          <label for="password">Mot de passe</label>
          <input type="password" id="password" name="password" placeholder="••••••••" required autocomplete="current-password">
        </div>
        <div style="text-align:right;margin-bottom:1.2rem;margin-top:-.8rem;">
          <a href="forgot_password.php" style="font-size:.8rem;color:var(--light2);text-decoration:none;transition:color .2s;"
             onmouseover="this.style.color='var(--orange)'" onmouseout="this.style.color='var(--light2)'">
            Mot de passe oublié ?
          </a>
        </div>
        <button type="submit" class="btn btn-primary" style="width:100%;justify-content:center;padding:1rem;">
          Se connecter
        </button>
      </form>
    </div>
    <p class="text-center mt-2" style="color:var(--light2);font-size:.88rem;">
      Pas encore de compte ? <a href="register.php" style="color:var(--orange);font-weight:600;">Inscrivez-vous gratuitement</a>
    </p>
  </div>
</main>
<?php include 'footer.php'; ?>
</body>
</html>
