<?php
/**
 * Tom's Java Jive - Customer Registration
 */

$pageTitle = "Create Account - Tom's Java Jive";
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/auth.php';

// Redirect if already logged in
if (CustomerAuth::isLoggedIn()) {
    redirect('/account/');
}

$error = '';
$email = '';
$name = '';
$phone = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $name = trim($_POST['name'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $password = $_POST['password'] ?? '';
    $confirmPassword = $_POST['confirm_password'] ?? '';
    
    if (empty($email) || empty($password)) {
        $error = 'Email and password are required.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Please enter a valid email address.';
    } elseif (strlen($password) < 8) {
        $error = 'Password must be at least 8 characters.';
    } elseif ($password !== $confirmPassword) {
        $error = 'Passwords do not match.';
    } else {
        $result = CustomerAuth::register($email, $password, $name, $phone);
        
        if (isset($result['error'])) {
            $error = $result['error'];
        } else {
            // Subscribe to newsletter if checked
            if (isset($_POST['newsletter'])) {
                $existing = db()->fetch("SELECT id FROM email_subscribers WHERE email = :email", ['email' => strtolower($email)]);
                if (!$existing) {
                    db()->insert('email_subscribers', [
                        'email' => strtolower($email),
                        'name' => $name,
                        'source' => 'registration'
                    ]);
                }
            }
            
            setFlash('success', 'Welcome! Your account has been created.');
            redirect('/account/');
        }
    }
}

$metaTitle = "Create Account | Tom's Java Jive";
$metaDescription = 'Create an account to earn rewards, track orders, and get exclusive deals.';
$metaKeywords = 'coffee loyalty rewards, coffee subscription account';
$canonicalUrl = 'https://tomsjavajive.com/register.php';
require_once __DIR__ . '/includes/header.php';
?>

<section class="section" style="padding-top: 2rem;">
    <div class="container">
        <div class="card" style="max-width: 500px; margin: 0 auto;">
            <div class="card-body">
                <h1 style="text-align: center; margin-bottom: 1.5rem;">Create Your Account</h1>
                
                <?php if ($error): ?>
                    <div class="alert alert-error">
                        <i class="fas fa-exclamation-circle"></i>
                        <?= htmlspecialchars($error) ?>
                    </div>
                <?php endif; ?>
                
                <form method="POST" action="">
                    <div class="form-group">
                        <label class="form-label" for="name">Full Name</label>
                        <input type="text" id="name" name="name" class="form-input" 
                               value="<?= htmlspecialchars($name) ?>" placeholder="John Doe">
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label" for="email">Email Address *</label>
                        <input type="email" id="email" name="email" class="form-input" 
                               value="<?= htmlspecialchars($email) ?>" required>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label" for="phone">Phone Number</label>
                        <input type="tel" id="phone" name="phone" class="form-input" 
                               value="<?= htmlspecialchars($phone) ?>" placeholder="(555) 123-4567">
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label" for="password">Password *</label>
                        <input type="password" id="password" name="password" class="form-input" required minlength="8">
                        <small class="text-muted">Minimum 8 characters</small>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label" for="confirm_password">Confirm Password *</label>
                        <input type="password" id="confirm_password" name="confirm_password" class="form-input" required>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-checkbox">
                            <input type="checkbox" name="newsletter" checked>
                            Subscribe to our newsletter for exclusive offers
                        </label>
                    </div>
                    
                    <button type="submit" class="btn btn-primary btn-lg btn-block">
                        Create Account
                    </button>
                </form>
                
                <hr style="margin: 1.5rem 0;">
                
                <p style="text-align: center;">
                    Already have an account? <a href="/login.php">Sign in</a>
                </p>
            </div>
        </div>
    </div>
</section>

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