<?php
if (!defined('_ISVALID')) define('_ISVALID', true);
include_once 'f_core/config.core.php';

// Handle registration form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = VSecurity::postParam('username', 'string');
    $email = VSecurity::postParam('email', 'string');
    $password = VSecurity::postParam('password', 'string');
    $confirm_password = VSecurity::postParam('confirm_password', 'string');
    
    $errors = [];
    
    if (!$username || strlen($username) < 3) {
        $errors[] = 'Username must be at least 3 characters';
    }
    
    if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = 'Please enter a valid email address';
    }
    
    if (!$password || strlen($password) < 6) {
        $errors[] = 'Password must be at least 6 characters';
    }
    
    if ($password !== $confirm_password) {
        $errors[] = 'Passwords do not match';
    }
    
    // Check if username exists
    if (empty($errors)) {
        $sql = "SELECT usr_id FROM db_accountuser WHERE usr_user = ?";
        $existing = $class_database->execute($sql, [$username]);
        if ($existing && count($existing) > 0) {
            $errors[] = 'Username already exists';
        }
    }
    
    // Create user if no errors
    if (empty($errors)) {
        $user_key = rand(100000, 999999);
        $sql = "INSERT INTO db_accountuser (usr_user, usr_email, usr_password, usr_verified, usr_joindate, usr_active, usr_key) 
                VALUES (?, ?, MD5(?), 1, NOW(), 1, ?)";
        
        $result = $class_database->execute($sql, [$username, $email, $password, $user_key]);
        
        if ($result) {
            $success = 'Account created successfully! You can now sign in.';
        } else {
            $errors[] = '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">
    <title>Register - EasyStream</title>
    <style>
        body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; margin: 0; padding: 20px; background: #f8f9fa; }
        .container { max-width: 400px; margin: 50px auto; }
        .register-card { background: white; padding: 40px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
        .logo { text-align: center; margin-bottom: 30px; }
        .logo img { max-height: 50px; }
        h1 { text-align: center; margin-bottom: 30px; color: #333; }
        .form-group { margin-bottom: 20px; }
        label { display: block; margin-bottom: 5px; font-weight: 500; }
        input[type="text"], input[type="email"], input[type="password"] { width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 6px; font-size: 14px; box-sizing: border-box; }
        .btn { width: 100%; padding: 12px; background: #28a745; color: white; border: none; border-radius: 6px; font-size: 16px; cursor: pointer; }
        .btn:hover { background: #218838; }
        .error { background: #f8d7da; color: #721c24; padding: 10px; border-radius: 6px; margin-bottom: 20px; }
        .success { background: #d4edda; color: #155724; padding: 10px; border-radius: 6px; margin-bottom: 20px; }
        .links { text-align: center; margin-top: 20px; }
        .links a { color: #007bff; text-decoration: none; }
    </style>
</head>
<body>
    <div class="container">
        <div class="register-card">
            <div class="logo">
                <img src="/f_scripts/fe/img/logo-header-blue.svg" alt="EasyStream">
            </div>
            <h1>Create Account</h1>
            
            <?php if (isset($success)): ?>
                <div class="success"><?php echo htmlspecialchars($success); ?></div>
            <?php endif; ?>
            
            <?php if (!empty($errors)): ?>
                <div class="error">
                    <?php foreach ($errors as $error): ?>
                        <?php echo htmlspecialchars($error); ?><br>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
            
            <form method="post">
                <div class="form-group">
                    <label>Username</label>
                    <input type="text" name="username" value="<?php echo htmlspecialchars($_POST['username'] ?? ''); ?>" required>
                </div>
                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email" value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>" required>
                </div>
                <div class="form-group">
                    <label>Password</label>
                    <input type="password" name="password" required>
                </div>
                <div class="form-group">
                    <label>Confirm Password</label>
                    <input type="password" name="confirm_password" required>
                </div>
                <button type="submit" class="btn">Create Account</button>
            </form>
            
            <div class="links">
                <a href="signin.php">Already have an account? Sign In</a><br>
                <a href="/">← Back to Homepage</a>
            </div>
        </div>
    </div>
</body>
</html>