<?php
require_once 'config.php';
require_once 'includes/auth.php';

$pageTitle = 'Login';
$auth = new Auth();

$error = '';
$redirect = isset($_GET['redirect']) ? $_GET['redirect'] : 'index.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = sanitize($_POST['email']);
    $password = $_POST['password'];
    
    $result = $auth->login($email, $password);
    
    if ($result['success']) {
        header('Location: ' . $redirect);
        exit;
    } else {
        $error = $result['message'];
    }
}

require_once 'includes/header.php';
?>

<!-- Login Page -->
<section class="products-section" style="padding-top: calc(var(--header-height) + var(--spacing-xxl)); min-height: 80vh;">
    <div class="container">
        <div class="auth-form">
            <h2>Sign In</h2>
            
            <?php if ($error): ?>
                <div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
            <?php endif; ?>
            
            <form method="POST">
                <div class="form-group">
                    <label for="email">Email Address</label>
                    <input type="email" id="email" name="email" required>
                </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-primary btn-block">Sign In</button>
            </form>
            
            <p style="text-align: center; margin-top: var(--spacing-lg); color: var(--medium-gray);">
                Don't have an account? <a href="<?php echo SITE_URL; ?>/register.php">Create one</a>
            </p>
            
 
    </div>
</section>

<?php require_once 'includes/footer.php'; ?>
