<?php
/**
 * Login page
 */

require_once __DIR__ . '/../includes/auth.php';

// Redirect if already logged in
if (!empty($_SESSION['user_id'])) {
    nazareth_redirect(nazareth_post_login_route());
}

$error = null;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';

    if (empty($username) || empty($password)) {
        $error = 'Please enter username and password.';
    } else {
        $result = attemptLogin($pdo, $username, $password);
        if ($result['success']) {
            $role = $result['role'] ?? ($_SESSION['role'] ?? '');
            $mustChange = !empty($result['must_change_password']);

            // Data collectors always go to the mobile collection app (same as /mobile/login).
            if ($role === 'data_collector') {
                nazareth_redirect(nazareth_post_login_route($role, $mustChange));
            }

            if ($mustChange) {
                nazareth_redirect('change-password');
            }

            $redirect = trim((string) ($_GET['redirect'] ?? ''));
            if ($redirect !== '' && strpos($redirect, '//') === false && strpos($redirect, "\n") === false) {
                header('Location: ' . $redirect);
                exit;
            }

            header('Location: ' . ($result['default_redirect'] ?? nazareth_url('dashboard')));
            exit;
        }
        $error = $result['error'];
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - <?= htmlspecialchars(SITE_NAME) ?></title>
    <link rel="icon" href="<?= nazareth_asset('img/logo.png') ?>" type="image/png">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="<?= nazareth_asset('css/style.css') ?>">
</head>
<body class="login-page">
    <div class="login-card">
        <div class="login-brand">
            <img src="<?= nazareth_asset('img/logo.png') ?>" alt="" class="login-logo">
            <h1 class="login-title">PCG-Nazareth Cong.</h1>
        </div>

        <?php if ($error): ?>
            <div data-toast="error" data-toast-msg="<?= htmlspecialchars($error) ?>" style="display:none"></div>
        <?php endif; ?>

        <form method="post" action="<?= nazareth_url('login') ?>" class="login-form">
            <div class="form-group">
                <label for="username">Username</label>
                <input type="text" id="username" name="username" required autofocus
                    value="<?= htmlspecialchars($_POST['username'] ?? '') ?>">
            </div>
            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" id="password" name="password" required>
            </div>
            <button type="submit" class="btn btn-block btn-primary">Sign in</button>
        </form>
        <p class="login-extra-link">
            <a href="<?= nazareth_url('forgot-password') ?>">Forgot password?</a>
        </p>
    </div>
    <footer class="login-footer">© <?= date('Y') ?> <?= htmlspecialchars(SITE_NAME) ?>. All rights reserved.</footer>
    <div id="toast-container" class="toast-container" aria-live="polite"></div>
    <script src="<?= nazareth_asset('js/main.js') ?>"></script>
</body>
</html>
