<?php
/**
 * Registration Page
 * Video Portal Management System
 */

require_once 'config/database.php';
require_once 'includes/functions.php';
require_once 'includes/auth.php';

// Redirect if already logged in
if (isLoggedIn()) {
    redirect(isAdmin() ? 'admin/dashboard.php' : 'index.php');
}

$settings = getAllSettings();

// Check if registration is allowed
if (($settings['allow_registration'] ?? '1') != '1') {
    redirect('login.php');
}

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

// Handle registration form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = sanitize($_POST['username'] ?? '');
    $email = sanitize($_POST['email'] ?? '');
    $fullName = sanitize($_POST['full_name'] ?? '');
    $password = $_POST['password'] ?? '';
    $confirmPassword = $_POST['confirm_password'] ?? '';
    
    // Validation
    if (empty($username) || empty($email) || empty($password)) {
        $error = 'Please fill in all required fields';
    } elseif (strlen($username) < 3) {
        $error = 'Username must be at least 3 characters';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Please enter a valid email address';
    } elseif (strlen($password) < 6) {
        $error = 'Password must be at least 6 characters';
    } elseif ($password !== $confirmPassword) {
        $error = 'Passwords do not match';
    } else {
        $result = registerUser($username, $email, $password, $fullName);
        if ($result['success']) {
            $success = 'Registration successful! You can now login.';
        } else {
            $error = $result['error'];
        }
    }
}

$siteName = $settings['site_name'] ?? 'Video Portal';
$loginLogo = $settings['login_logo'] ?? '';
$loginBg = $settings['login_background'] ?? '';
$rtl = $settings['rtl_direction'] ?? '0';
?>
<!DOCTYPE html>
<html lang="<?php echo $rtl == '1' ? 'he' : 'en'; ?>" dir="<?php echo $rtl == '1' ? 'rtl' : 'ltr'; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register - <?php echo htmlspecialchars($siteName); ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="assets/css/style.css">
    <?php if (!empty($settings['primary_color'])): ?>
    <style>
        :root {
            --primary-color: <?php echo $settings['primary_color']; ?>;
            --secondary-color: <?php echo $settings['secondary_color'] ?? '#2c3e50'; ?>;
        }
    </style>
    <?php endif; ?>
</head>
<body class="<?php echo $rtl == '1' ? 'rtl' : ''; ?>">
    <div class="login-page <?php echo !empty($loginBg) ? 'has-bg' : ''; ?>"
         <?php if (!empty($loginBg)): ?>style="background-image: url('assets/uploads/logos/<?php echo $loginBg; ?>');"<?php endif; ?>>
        <div class="login-container">
            <div class="login-logo">
                <?php if (!empty($loginLogo)): ?>
                    <img src="assets/uploads/logos/<?php echo $loginLogo; ?>" alt="<?php echo htmlspecialchars($siteName); ?>">
                <?php else: ?>
                    <i class="fas fa-play-circle" style="font-size: 60px; color: var(--primary-color);"></i>
                <?php endif; ?>
                <h1><?php echo htmlspecialchars($siteName); ?></h1>
            </div>
            
            <?php if ($error): ?>
                <div class="alert alert-danger">
                    <i class="fas fa-exclamation-circle"></i> <?php echo $error; ?>
                </div>
            <?php endif; ?>
            
            <?php if ($success): ?>
                <div class="alert alert-success">
                    <i class="fas fa-check-circle"></i> <?php echo $success; ?>
                </div>
            <?php endif; ?>
            
            <form class="login-form" method="POST" action="">
                <div class="form-group">
                    <label for="username">
                        <i class="fas fa-user"></i> Username *
                    </label>
                    <input type="text" id="username" name="username" required
                           value="<?php echo htmlspecialchars($_POST['username'] ?? ''); ?>"
                           placeholder="Choose a username">
                </div>
                
                <div class="form-group">
                    <label for="email">
                        <i class="fas fa-envelope"></i> Email *
                    </label>
                    <input type="email" id="email" name="email" required
                           value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>"
                           placeholder="Enter your email">
                </div>
                
                <div class="form-group">
                    <label for="full_name">
                        <i class="fas fa-id-card"></i> Full Name
                    </label>
                    <input type="text" id="full_name" name="full_name"
                           value="<?php echo htmlspecialchars($_POST['full_name'] ?? ''); ?>"
                           placeholder="Enter your full name">
                </div>
                
                <div class="form-group">
                    <label for="password">
                        <i class="fas fa-lock"></i> Password *
                    </label>
                    <input type="password" id="password" name="password" required
                           placeholder="Choose a password (min 6 characters)">
                </div>
                
                <div class="form-group">
                    <label for="confirm_password">
                        <i class="fas fa-lock"></i> Confirm Password *
                    </label>
                    <input type="password" id="confirm_password" name="confirm_password" required
                           placeholder="Confirm your password">
                </div>
                
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-user-plus"></i> Register
                </button>
            </form>
            
            <div class="login-footer">
                Already have an account? <a href="login.php">Login here</a>
            </div>
        </div>
    </div>
</body>
</html>
