<?php
/**
 * Agentic Square - Premium Multi-Vendor Onboarding Module
 * CodeCanyon Distribution Standard
 */

require_once __DIR__ . '/config/config.php';

// Route authenticated users away from the registration form automatically
if (isset($_SESSION['user_id'])) {
    header('Location: ' . app_url('index.php'));
    exit;
}

// Generate structural CSRF protection tokens
if (empty($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}

require_once __DIR__ . '/sections/header.php';
?>

<div class="min-h-screen flex items-center justify-center py-12 px-4 sm:px-6 lg:px-8 relative bg-nftDark">
    <!-- Atmospheric Visual Ambient Glow Elements[cite: 1] -->
    <div class="absolute top-1/4 left-1/4 w-72 h-72 bg-nftAccentPurple/10 rounded-full blur-[100px] pointer-events-none"></div>
    <div class="absolute bottom-1/4 right-1/4 w-72 h-72 bg-nftAccentCyan/10 rounded-full blur-[100px] pointer-events-none"></div>

    <div class="max-w-md w-full space-y-8 relative z-10">
        <!-- System Core Header Identity Group[cite: 1] -->
        <div class="text-center">
            <div class="mx-auto w-12 h-12 bg-gradient-to-tr from-nftAccentCyan to-nftAccentPurple rounded-xl flex items-center justify-center font-bold text-2xl tracking-wider shadow-lg shadow-cyan-500/20 cursor-pointer" onclick="window.location.href='<?php echo htmlspecialchars(app_url('index.php'), ENT_QUOTES, 'UTF-8'); ?>'">
                A
            </div>
            <h2 class="mt-6 text-3xl font-extrabold tracking-tight text-white">Create your account</h2>
            <p class="mt-2 text-sm text-nftTextGray">
                Join Agentic Square to source or sell advanced AI applications[cite: 1].
            </p>
        </div>

        <!-- System Alert Messages Execution Wrapper -->
        <?php if (isset($_SESSION['register_error'])): ?>
            <div class="bg-rose-500/10 border border-rose-500/20 text-rose-400 p-4 rounded-xl text-sm flex items-center space-x-2">
                <i class="fa-solid fa-circle-exclamation"></i>
                <span><?php echo $_SESSION['register_error']; unset($_SESSION['register_error']); ?></span>
            </div>
        <?php endif; ?>
        
        <?php if (isset($_SESSION['register_success'])): ?>
            <div class="bg-emerald-500/10 border border-emerald-500/20 text-emerald-400 p-4 rounded-xl text-sm flex items-center space-x-2">
                <i class="fa-solid fa-circle-check"></i>
                <span><?php echo $_SESSION['register_success']; unset($_SESSION['register_success']); ?></span>
            </div>
        <?php endif; ?>

        <!-- Primary Form Engine Block[cite: 2] -->
        <form class="glass-card mt-8 space-y-5 p-6 sm:p-8 rounded-3xl border border-slate-800/80" action="<?php echo htmlspecialchars(app_url('register-process.php'), ENT_QUOTES, 'UTF-8'); ?>" method="POST">
            <!-- Inject System Security Key Tracking Row -->
            <input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">

            <div class="space-y-1.5">
                <label for="name" class="text-xs font-semibold text-slate-300 uppercase tracking-wider">Full Name</label>
                <div class="relative">
                    <input id="name" name="full_name" type="text" required placeholder="John Doe" class="w-full bg-slate-950/60 border border-slate-800/80 rounded-xl pl-10 pr-4 py-3 text-sm focus:outline-none focus:border-nftAccentCyan text-slate-200 transition-all">
                    <i class="fa-solid fa-user absolute left-3.5 top-3.5 text-xs text-nftTextGray"></i>
                </div>
            </div>

            <div class="space-y-1.5">
                <label for="email" class="text-xs font-semibold text-slate-300 uppercase tracking-wider">Email Address</label>
                <div class="relative">
                    <input id="email" name="email" type="email" required placeholder="john@example.com" class="w-full bg-slate-950/60 border border-slate-800/80 rounded-xl pl-10 pr-4 py-3 text-sm focus:outline-none focus:border-nftAccentCyan text-slate-200 transition-all">
                    <i class="fa-solid fa-envelope absolute left-3.5 top-3.5 text-xs text-nftTextGray"></i>
                </div>
            </div>

            <div class="space-y-1.5">
                <label for="password" class="text-xs font-semibold text-slate-300 uppercase tracking-wider">Secure Password</label>
                <div class="relative">
                    <input id="password" name="password" type="password" required placeholder="••••••••" class="w-full bg-slate-950/60 border border-slate-800/80 rounded-xl pl-10 pr-4 py-3 text-sm focus:outline-none focus:border-nftAccentCyan text-slate-200 transition-all">
                    <i class="fa-solid fa-lock absolute left-3.5 top-3.5 text-xs text-nftTextGray"></i>
                </div>
            </div>

            <div class="space-y-1.5">
                <label for="role" class="text-xs font-semibold text-slate-300 uppercase tracking-wider">Account Role Profile</label>
                <div class="relative">
                    <select id="role" name="role" required class="w-full bg-slate-950/60 border border-slate-800/80 rounded-xl pl-10 pr-4 py-3 text-sm focus:outline-none focus:border-nftAccentCyan text-slate-200 transition-all appearance-none cursor-pointer">
                        <option value="buyer" class="bg-nftCard">Buyer - Sourcing Scripts & Tools</option>
                        <option value="seller" class="bg-nftCard">Seller - Listing AI Components</option>
                    </select>
                    <i class="fa-solid fa-users-gear absolute left-3.5 top-3.5 text-xs text-nftTextGray"></i>
                    <i class="fa-solid fa-chevron-down absolute right-4 top-4 text-[10px] text-nftTextGray pointer-events-none"></i>
                </div>
            </div>

            <button type="submit" class="w-full btn-gradient py-3.5 rounded-xl font-bold text-sm tracking-wide shadow-xl transition-all[cite: 2]">
                Register Platform Account <i class="fa-solid fa-user-plus ml-2 text-xs"></i>
            </button>

            <div class="text-center text-xs text-nftTextGray pt-2">
                Already registered? <a href="<?php echo htmlspecialchars(app_url('login.php'), ENT_QUOTES, 'UTF-8'); ?>" class="text-nftAccentCyan hover:underline font-semibold">Sign in here</a>
            </div>
        </form>
    </div>
</div>

</body>
</html>
