<?php
require_once 'db/config.php';
require_once 'includes/header.php';

$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $username = $_POST['username'] ?? '';
        $email = $_POST['email'] ?? '';
        $password = $_POST['password'] ?? '';
    
        if (empty($username)) {
            $errors[] = 'Username is required';
        }
        if (empty($email)) {
            $errors[] = 'Email is required';
        } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $errors[] = 'Invalid email format';
        }
        if (empty($password)) {
            $errors[] = 'Password is required';
        }
    
        if (empty($errors)) {
            $pdo = db();
            $stmt = $pdo->prepare('SELECT id FROM users WHERE email = ?');
            $stmt->execute([$email]);
            if ($stmt->fetch()) {
                $errors[] = 'Email already in use';
            } else {
                $hashed_password = password_hash($password, PASSWORD_DEFAULT);
                $stmt = $pdo->prepare('INSERT INTO users (username, email, password) VALUES (?, ?, ?)');
                if ($stmt->execute([$username, $email, $hashed_password])) {
                    require_once 'mail/MailService.php';
                    $subject = 'Welcome to RS Learning Lab!';
                    $body = "Hello {$username},<br><br>Thank you for registering at RS Learning Lab. We are excited to have you on board!<br><br>Best regards,<br>The RS Learning Lab Team";
                    $result = MailService::sendMail($email, $subject, $body, $body);
                    if ($result['success']) {
                        //The registration email was sent successfully.
                    } else {
                        error_log('Failed to send registration email: ' . $result['error']);
                    }
                    header('Location: login.php?registered=true');
                    exit;
                } else {
                    $errors[] = 'Failed to register. Please try again.';
                }
            }
        }
    }
    ?>
    
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">Register</div>
                <div class="card-body">
                    <?php if (!empty($errors)):
                        ?>
                        <div class="alert alert-danger">
                            <?php foreach ($errors as $error) {
        echo "<p>{$error}</p>";
    }
    ?>
                        </div>
                    <?php
                    endif; ?>
                    <form action="register.php" method="post">
                        <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="password" class="form-label">Password</label>
                            <input type="password" class="form-control" id="password" name="password" required>
                        </div>
                        <button type="submit" class="btn btn-primary">Register</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

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