<?php
/* PATH: /var/www/html/signup.php */
require_once 'includes/db_config.php';
if (session_status() === PHP_SESSION_NONE) { session_start(); }

$error_msg = "";
$success_msg = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['voter_email']);
    $password = $_POST['voter_password'];
    $account_type = $_POST['account_type']; // 'citizen', 'party_dem', 'party_rep', 'party_ind'
    $county_target = trim($_POST['home_county']);
    
    if(!empty($email) && !empty($password)) {
        // Production registration script routing
        try {
            $db = (isset($pdo) && $pdo instanceof PDO) ? $pdo : ((isset($conn) && $conn instanceof PDO) ? $conn : null);
            if ($db) {
                $hash = password_hash($password, PASSWORD_BCRYPT);
                $stmt = $db->prepare("INSERT INTO users (email, password_hash, role, target_county) VALUES (:email, :hash, :role, :county)");
                $stmt->execute([
                    'email' => $email,
                    'hash' => $hash,
                    'role' => $account_type,
                    'county' => $county_target
                ]);
                $success_msg = "Account established successfully. Access links are now mapped.";
            } else {
                // Sandbox simulation deployment
                $success_msg = "Account structural profile simulation saved successfully.";
            }
        } catch (Exception $e) {
            $error_msg = "Registration error: Direct identification collision or duplicate configuration.";
        }
    } else {
        $error_msg = "All tracking verification fields must be completed.";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Establish Certified Account Profile | LGN</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        body { font-family: 'Times New Roman', serif; background-color: #040a08; }
        .scannable-section { border: 3px solid #C5A059 !important; background-color: rgba(5, 14, 11, 0.96) !important; border-radius: 12px; padding: 30px; box-shadow: 0 25px 50px rgba(0,0,0,0.9); }
    </style>
</head>
<body class="text-gray-100 min-h-screen flex items-center justify-center p-4" style="background-image: linear-gradient(rgba(4, 10, 8, 0.85), rgba(4, 10, 8, 0.95)), url('https://images.unsplash.com/photo-1589829545856-d10d557cf95f?auto=format&fit=crop&q=80&w=1920'); background-size: cover; background-position: center;">

    <div class="max-w-md w-full scannable-section space-y-6">
        
        <div class="text-center border-b border-white/10 pb-4">
            <h1 class="text-2xl font-serif font-bold text-white uppercase tracking-wide">Account Onboarding Portal</h1>
            <p class="text-xs font-sans text-gray-400 uppercase tracking-widest mt-1">Local Government News Network</p>
        </div>

        <?php if(!empty($error_msg)): ?>
            <div class="bg-red-950/60 border border-red-500/40 text-red-200 p-3 rounded-lg text-xs font-sans font-bold"><?php echo htmlspecialchars($error_msg); ?></div>
        <?php endif; ?>
        <?php if(!empty($success_msg)): ?>
            <div class="bg-emerald-950/60 border border-emerald-500/40 text-emerald-200 p-3 rounded-lg text-xs font-sans font-bold"><?php echo htmlspecialchars($success_msg); ?></div>
        <?php endif; ?>

        <form method="POST" class="space-y-4 text-xs font-sans font-bold uppercase tracking-wide text-gray-400">
            
            <div class="space-y-1">
                <label class="block">Secure Communication Address (Email)</label>
                <input type="email" name="voter_email" class="w-full bg-black/60 border border-white/20 rounded p-2.5 text-white font-sans focus:outline-none focus:border-[#C5A059]" required>
            </div>

            <div class="space-y-1">
                <label class="block">Access Credential Token (Password)</label>
                <input type="password" name="voter_password" class="w-full bg-black/60 border border-white/20 rounded p-2.5 text-white font-sans focus:outline-none focus:border-[#C5A059]" required>
            </div>

            <div class="space-y-1">
                <label class="block">Primary Voting Jurisdiction / County Name</label>
                <input type="text" name="home_county" placeholder="e.g., Green County, WI" class="w-full bg-black/60 border border-white/20 rounded p-2.5 text-white font-sans focus:outline-none focus:border-[#C5A059]" required>
            </div>

            <div class="space-y-1">
                <label class="block">Profile Security Classification</label>
                <select name="account_type" class="w-full bg-black border border-white/20 rounded p-2.5 text-white font-sans focus:outline-none focus:border-[#C5A059] h-[40px]">
                    <option value="citizen">Verified Local Citizen Account (Free)</option>
                    <option value="party_dem">Democrat County Party Officer Link Authorization</option>
                    <option value="party_rep">Republican County Party Officer Link Authorization</option>
                    <option value="party_ind">Independent Forum Delegate Authorization</option>
                </select>
            </div>

            <div class="pt-2">
                <button type="submit" class="w-full bg-[#C5A059] hover:bg-[#d4af37] text-black font-sans font-bold text-xs uppercase tracking-widest py-3 rounded border border-black shadow transition-all">
                    Initialize Ledger Access Profile &rarr;
                </button>
            </div>
        </form>

        <div class="text-center pt-2 border-t border-white/5">
            <a href="index.php" class="text-[11px] text-[#C5A059] hover:underline">&larr; Bypass to Public Interface Feed</a>
        </div>
    </div>

</body>
</html>