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

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

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = sanitize($_POST['email']);
    $password = $_POST['password'];
    $confirmPassword = $_POST['confirm_password'];
    $fullName = sanitize($_POST['full_name']);
    $phone = sanitize($_POST['phone'] ?? '');
    
    // Validation
    if ($password !== $confirmPassword) {
        $error = 'Passwords do not match.';
    } elseif (strlen($password) < 6) {
        $error = 'Password must be at least 6 characters.';
    } else {
        $result = $auth->register($email, $password, $fullName, $phone);
        
        if ($result['success']) {
            // Auto-login after registration
            $loginResult = $auth->login($email, $password);
            if ($loginResult['success']) {
                header('Location: ' . $redirect);
                exit;
            }
        } else {
            $error = $result['message'];
        }
    }
}

require_once 'includes/header.php';
?>

<!-- Register 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>Create Account</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="full_name">Full Name *</label>
                    <input type="text" id="full_name" name="full_name" required>
                </div>
                
                <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="phone">Phone Number</label>
                    <input type="tel" id="phone" name="phone">
                </div>
                
                <div class="form-group">
                    <label for="password">Password *</label>
                    <input type="password" id="password" name="password" required minlength="6">
                </div>
                
                <div class="form-group">
                    <label for="confirm_password">Confirm Password *</label>
                    <input type="password" id="confirm_password" name="confirm_password" required>
                </div>
                
                <button type="submit" class="btn btn-primary btn-block">Create Account</button>
            </form>
            
            <p style="text-align: center; margin-top: var(--spacing-lg); color: var(--medium-gray);">
                Already have an account? <a href="<?php echo SITE_URL; ?>/login.php">Sign in</a>
            </p>
        </div>
    </div>
</section>

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