<?php

require_once __DIR__ . '/../app/config/config.php';
require_once __DIR__ . '/../app/config/database.php';
require_once __DIR__ . '/../app/helpers/functions.php';
require_once __DIR__ . '/../app/helpers/auth.php';

$pageTitle = 'Login | LOVE-U Essentials';
$pageDescription = 'Login to your LOVE-U Essentials account.';

if (is_customer_logged_in()) {
    redirect(BASE_URL . '/public/account.php');
}

$error = '';

if (is_post()) {
    if (!verify_csrf()) {
        $error = 'Invalid request. Please refresh and try again.';
    } else {
        $email = trim($_POST['email'] ?? '');
        $password = $_POST['password'] ?? '';

        if ($email === '' || $password === '') {
            $error = 'Please enter email and password.';
        } else {
            $stmt = $pdo->prepare("
                SELECT *
                FROM customers
                WHERE email = ?
                LIMIT 1
            ");
            $stmt->execute([$email]);
            $customerUser = $stmt->fetch();

            if (!$customerUser || empty($customerUser['password_hash']) || !password_verify($password, $customerUser['password_hash'])) {
                $error = 'Invalid email or password.';
            } elseif ($customerUser['status'] !== 'active') {
                $error = 'This account is not active.';
            } else {
                login_customer($customerUser);
                redirect(BASE_URL . '/public/account.php');
            }
        }
    }
}

require_once __DIR__ . '/includes/header.php';
?>

<section class="lue-auth-page">
    <div class="lue-auth-orb lue-auth-orb-one"></div>
    <div class="lue-auth-orb lue-auth-orb-two"></div>

    <div class="container lue-auth-grid lue-auth-login-grid">
        <div class="lue-auth-visual reveal-up">
            <div class="lue-auth-image">
                <img src="<?= e(BASE_URL); ?>/uploads/products/loveu-product-range.jpg" alt="LOVE-U Essentials self-care products">
            </div>

            <div class="lue-auth-floating-card">
                <span>Welcome back</span>
                <strong>Your self-care space is ready.</strong>
            </div>
        </div>

        <div class="lue-auth-card reveal-up reveal-delay-1">
            <div class="lue-auth-head">
                <div class="eyebrow">Customer Account</div>
                <h1>Login</h1>
                <p>Access your orders, wishlist, loyalty points, and referrals.</p>
            </div>

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

            <form method="POST" class="lue-auth-form">
                <?= csrf_field(); ?>

                <div class="lue-auth-field">
                    <label>Email Address *</label>
                    <input type="email" name="email" placeholder="you@example.com" required>
                </div>

                <div class="lue-auth-field">
    <label>Password *</label>

    <div class="lue-password-wrap">
        <input
            type="password"
            name="password"
            id="loginPassword"
            placeholder="Enter your password"
            required
        >

        <button
            type="button"
            class="lue-password-toggle"
            data-target="loginPassword"
            aria-label="Show password"
        >
            <svg viewBox="0 0 24 24" aria-hidden="true">
                <path d="M2.5 12s3.5-6.5 9.5-6.5S21.5 12 21.5 12s-3.5 6.5-9.5 6.5S2.5 12 2.5 12Z"></path>
                <circle cx="12" cy="12" r="3"></circle>
            </svg>
        </button>
    </div>
</div>

                <button type="submit" class="btn lue-auth-submit">Login</button>
            </form>

            <div class="lue-auth-switch">
                <span>New to LOVE-U Essentials?</span>
                <a href="<?= e(BASE_URL); ?>/public/register.php">Create an account</a>
            </div>

            <div class="lue-auth-benefits">
                <span>Order history</span>
                <span>Wishlist</span>
                <span>Loyalty points</span>
            </div>
        </div>
    </div>
</section>
<?php require_once __DIR__ . '/includes/footer.php'; ?>