<?php
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../classes/User.php';

if (isLoggedIn()) {
    redirect('dashboard.php');
}

$user = new User();
$error = null;
$success = null;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $firstName = trim($_POST['first_name'] ?? '');
    $lastName = trim($_POST['last_name'] ?? '');
    $username = trim($_POST['username'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    $confirmPassword = $_POST['password_confirmation'] ?? '';
    $region = trim($_POST['region'] ?? '');

    if ($password !== $confirmPassword) {
        $error = 'Passwords do not match.';
    } else {
        // Validate region input (allowed regions are usa, europe, africa)
        $allowedRegions = ['usa','europe','africa'];
        if (!in_array($region, $allowedRegions, true)) {
            $region = DEFAULT_REGION;
        }

        $data = [
            'first_name' => $firstName,
            'last_name'  => $lastName,
            'username'   => $username,
            'email'      => $email,
            'password'   => $password,
            'region'     => $region,
        ];

        try {
            $userId = $user->register($data);

            $db = Database::getInstance();
            $db->update('users', [
                'status' => 'active',
                'email_verified' => 1,
                'email_verification_token' => null,
            ], 'id = ?', [$userId]);

            setFlashMessage('success', 'Registration successful. You can now log in.');
            redirect('login.php');
        } catch (Exception $e) {
            $error = $e->getMessage();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register - SmartThink</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            background: radial-gradient(circle at top, #22c55e, #020617);
            font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
        }
        .auth-card {
            background: rgba(15, 23, 42, 0.96);
            border-radius: 1rem;
            box-shadow: 0 24px 80px rgba(15, 23, 42, 0.7);
            overflow: hidden;
            max-width: 960px;
            width: 100%;
            color: #e5e7eb;
        }
        .auth-left {
            background: radial-gradient(circle at top left, #22c55e, #1e293b);
            padding: 2.5rem;
            color: white;
        }
        .auth-right {
            padding: 2.5rem;
            background: #020617;
        }
        .brand {
            display: flex;
            align-items: center;
            gap: 0.75rem;
            margin-bottom: 2rem;
        }
        .brand-logo {
            width: 40px;
            height: 40px;
            border-radius: 999px;
            background: rgba(15, 23, 42, 0.9);
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .brand-logo i {
            color: #22c55e;
        }
        .form-control {
            background-color: rgba(15, 23, 42, 0.9);
            border-color: #1f2937;
            color: #e5e7eb;
        }
        .form-control:focus {
            border-color: #22c55e;
            box-shadow: 0 0 0 1px rgba(34, 197, 94, 0.6);
            background-color: rgba(15, 23, 42, 0.95);
            color: #e5e7eb;
        }
        .btn-primary {
            background: linear-gradient(to right, #22c55e, #16a34a);
            border: none;
        }
        .btn-primary:hover {
            background: linear-gradient(to right, #16a34a, #15803d);
        }
        .text-muted-link {
            color: #9ca3af;
            text-decoration: none;
        }
        .text-muted-link:hover {
            color: #e5e7eb;
        }
        .alert {
            border-radius: 0.75rem;
        }
    </style>
</head>
<body>
<div class="container px-3 px-md-0">
    <div class="auth-card row g-0">
        <div class="col-lg-5 auth-left d-none d-lg-flex flex-column justify-content-between">
            <div>
                <div class="brand">
                    <div class="brand-logo">
                        <i class="fas fa-graduation-cap"></i>
                    </div>
                    <div>
                        <div class="fw-bold">SmartThink</div>
                        <small class="text-white-50">Global Learning Platform</small>
                    </div>
                </div>

                <h2 class="mb-3">Create your learning account</h2>
                <p class="mb-4 text-white-75">Join SmartThink3 to access global courses, track your progress, and earn certificates.</p>

                <ul class="list-unstyled small text-white-75">
                    <li class="mb-2"><i class="fas fa-check-circle me-2"></i>Access regional content for USA, Europe and Africa</li>
                    <li class="mb-2"><i class="fas fa-check-circle me-2"></i>Earn certificates and track achievements</li>
                    <li class="mb-2"><i class="fas fa-check-circle me-2"></i>Personalised recommendations based on your interests</li>
                </ul>
            </div>
            <div class="small text-white-50 mt-4">
                &copy; <?php echo date('Y'); ?> SmartThink. All rights reserved.
            </div>
        </div>
        <div class="col-lg-7 auth-right">
            <h3 class="mb-1">Sign up</h3>
            <p class="text-muted mb-4">Create your learner account to get started.</p>

            <?php if (!empty($error)): ?>
                <div class="alert alert-danger py-2 mb-3" role="alert">
                    <?php echo htmlspecialchars($error); ?>
                </div>
            <?php endif; ?>

            <form method="post" novalidate>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label for="first_name" class="form-label">First Name</label>
                        <input type="text" class="form-control" id="first_name" name="first_name" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label for="last_name" class="form-label">Last Name</label>
                        <input type="text" class="form-control" id="last_name" name="last_name" required>
                    </div>
                </div>

                <div class="mb-3">
                    <label for="username" class="form-label">Username</label>
                    <input type="text" class="form-control" id="username" name="username" required>
                </div>

                <div class="mb-3">
                    <label for="email" class="form-label">Email Address</label>
                    <input type="email" class="form-control" id="email" name="email" required>
                </div>

                <div class="mb-3">
                    <label for="region" class="form-label">Continent / Region</label>
                    <select name="region" id="region" class="form-select" required>
                        <option value="usa">USA</option>
                        <option value="europe">Europe</option>
                        <option value="africa">Africa</option>
                    </select>
                    <div class="form-text">Select the continent/region for content and course visibility.</div>
                </div>

                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label for="password" class="form-label">Password</label>
                        <input type="password" class="form-control" id="password" name="password" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label for="password_confirmation" class="form-label">Confirm Password</label>
                        <input type="password" class="form-control" id="password_confirmation" name="password_confirmation" required>
                    </div>
                </div>

                <button type="submit" class="btn btn-primary w-100 mb-3">
                    <i class="fas fa-user-plus me-2"></i>Create Account
                </button>

                <div class="text-center small text-muted">
                    Already have an account?
                    <a href="login.php" class="text-muted-link">Login here</a>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
