<?php
require_once 'includes/functions.php';

if (isLoggedIn()) {
    redirect(USER_URL);
}

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = sanitize($_POST['name'] ?? '');
    $email = sanitize($_POST['email'] ?? '');
    $phone = sanitize($_POST['phone'] ?? '');
    $password = $_POST['password'] ?? '';
    $confirmPassword = $_POST['confirm_password'] ?? '';
    
    if (empty($name) || empty($email) || empty($password)) {
        $error = 'Please fill in all required fields.';
    } elseif ($password !== $confirmPassword) {
        $error = 'Passwords do not match.';
    } elseif (strlen($password) < 6) {
        $error = 'Password must be at least 6 characters.';
    } else {
        $db = getDBConnection();
        $stmt = $db->prepare("SELECT id FROM users WHERE email = ?");
        $stmt->execute([$email]);
        
        if ($stmt->fetch()) {
            $error = 'Email address already registered.';
        } else {
            $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
            $stmt = $db->prepare("INSERT INTO users (name, email, phone, password, role, status) VALUES (?, ?, ?, ?, 'user', 'active')");
            
            if ($stmt->execute([$name, $email, $phone, $hashedPassword])) {
                $userId = $db->lastInsertId();
                logActivity($userId, 'user_registered', $name . ' registered');
                $success = 'Registration successful! You can now login.';
            } else {
                $error = 'Registration failed. Please try again.';
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register - Traveler</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&family=Playfair+Display:wght@400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <link rel="stylesheet" href="<?= BASE_URL ?>assets/css/style.css">
</head>
<body>
    <div class="auth-page">
        <div class="auth-card" style="max-width:500px;">
            <div class="auth-header">
                <div class="brand-text">Travel<span>er</span></div>
                <p>Create your account and start exploring!</p>
            </div>
            
            <?php if ($error): ?>
                <div class="alert alert-danger">
                    <i class="fas fa-exclamation-circle"></i> <?= $error ?>
                </div>
            <?php endif; ?>
            
            <?php if ($success): ?>
                <div class="alert alert-success">
                    <i class="fas fa-check-circle"></i> <?= $success ?>
                    <a href="<?= BASE_URL ?>login.php" style="color:var(--success);font-weight:600;margin-left:5px;">Login Now</a>
                </div>
            <?php endif; ?>
            
            <form method="POST" data-validate>
                <div class="form-group">
                    <label><i class="fas fa-user"></i> Full Name <span style="color:var(--danger);">*</span></label>
                    <input type="text" name="name" class="form-control" placeholder="Enter your full name" required value="<?= sanitize($_POST['name'] ?? '') ?>">
                </div>
                
                <div class="form-group">
                    <label><i class="fas fa-envelope"></i> Email Address <span style="color:var(--danger);">*</span></label>
                    <input type="email" name="email" class="form-control" placeholder="Enter your email" required value="<?= sanitize($_POST['email'] ?? '') ?>">
                </div>
                
                <div class="form-group">
                    <label><i class="fas fa-phone"></i> Phone Number</label>
                    <input type="tel" name="phone" class="form-control" placeholder="Enter your phone number" value="<?= sanitize($_POST['phone'] ?? '') ?>">
                </div>
                
                <div class="form-group">
                    <label><i class="fas fa-lock"></i> Password <span style="color:var(--danger);">*</span></label>
                    <input type="password" name="password" class="form-control" placeholder="Min 6 characters" required minlength="6">
                </div>
                
                <div class="form-group">
                    <label><i class="fas fa-lock"></i> Confirm Password <span style="color:var(--danger);">*</span></label>
                    <input type="password" name="confirm_password" class="form-control" placeholder="Confirm your password" required>
                </div>
                
                <button type="submit" class="btn btn-primary btn-block btn-lg">
                    <i class="fas fa-user-plus"></i> Create Account
                </button>
            </form>
            
            <div class="auth-footer">
                Already have an account? <a href="<?= BASE_URL ?>login.php">Login Here</a>
            </div>
            
            <div style="text-align:center;margin-top:15px;">
                <a href="<?= BASE_URL ?>" style="color:var(--gray);font-size:0.85rem;">
                    <i class="fas fa-arrow-left"></i> Back to Home
                </a>
            </div>
        </div>
    </div>
    <script src="<?= BASE_URL ?>assets/js/main.js"></script>
</body>
</html>
