<?php
require_once __DIR__ . '/../lib/auth.php';

if (currentUserId()) {
    header('Location: /dashboard');
    exit;
}

$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $result = login($_POST['email'] ?? '', $_POST['password'] ?? '');
    if ($result['ok']) {
        header('Location: /dashboard');
        exit;
    }
    $error = $result['error'];
}
?>
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login — Speechky</title>
    <link rel="stylesheet" href="/assets/style.css">
</head>
<body>

<div class="container-wide">
    <nav class="nav">
        <a href="/" class="nav-logo">Speechky</a>
        <div class="nav-links">
            <a href="/register">Sign Up</a>
        </div>
    </nav>
</div>

<div class="container">
    <div class="card">
        <h2>Log in</h2>

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

        <form method="POST">
            <div class="form-group">
                <label for="email">Email</label>
                <input type="email" id="email" name="email" required
                       value="<?= htmlspecialchars($_POST['email'] ?? '') ?>"
                       placeholder="you@example.com">
            </div>
            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" id="password" name="password" required
                       placeholder="Your password">
            </div>
            <button type="submit" class="btn btn-primary form-submit">Log in</button>
        </form>

        <div class="form-footer">
            Don't have an account? <a href="/register">Sign up</a>
        </div>
    </div>
</div>

</body>
</html>
