<?php
session_start();
include 'config.php';

$error = "";
$success = "";

// চেক করা হচ্ছে ফর্ম সাবমিট হয়েছে কি না
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // ডাটা রিসিভ করা এবং সিকিউর করা
    $username = isset($_POST['username']) ? $conn->real_escape_string(trim($_POST['username'])) : '';
    $password = isset($_POST['password']) ? $_POST['password'] : '';
    $confirm_password = isset($_POST['confirm_password']) ? $_POST['confirm_password'] : '';

    // ১. খালি ঘর চেক করা
    if (empty($username) || empty($password) || empty($confirm_password)) {
        $error = "সবগুলো ঘর সঠিকভাবে পূরণ করুন!";
    } else {
        // ২. ডাটাবেসে ইউজারনেম চেক করা (এরর হ্যান্ডলিং)
        $check_user = $conn->query("SELECT id FROM users WHERE username='$username'");
        
        if ($check_user->num_rows > 0) {
            $error = "দুঃখিত, এই ইউজারনেমটি ( $username ) অলরেডি আছে!";
        } elseif ($password !== $confirm_password) {
            $error = "পাসওয়ার্ড দুটি মিলছে না!";
        } elseif (strlen($password) < 6) {
            $error = "পাসওয়ার্ড কমপক্ষে ৬ অক্ষরের হতে হবে!";
        } else {
            // ৩. পাসওয়ার্ড এনক্রিপ্ট করা
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);
            
            // ৪. নতুন ইউজার ইনসার্ট করা
            $sql = "INSERT INTO users (username, password, role, balance) VALUES ('$username', '$hashed_password', 'user', 0)";
            
            if ($conn->query($sql)) {
                $success = "রেজিস্ট্রেশন সফল হয়েছে! এখন লগইন করুন।";
            } else {
                $error = "সিস্টেমে সমস্যা হয়েছে: " . $conn->error;
            }
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register | Sensi Pro AI</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@700&family=Rajdhani:wght@600&display=swap');
        body { 
            background: #020202; color: #fff; font-family: 'Rajdhani', sans-serif;
            background-image: radial-gradient(circle at top, #083344 0%, #000 80%);
            min-height: 100vh; display: flex; align-items: center; justify-content: center;
        }
        .orbitron { font-family: 'Orbitron', sans-serif; }
        .glass { background: rgba(15, 15, 15, 0.85); backdrop-filter: blur(20px); border: 1px solid rgba(255,255,255,0.05); border-radius: 35px; box-shadow: 0 25px 50px rgba(0,0,0,0.5); }
        .input-style { background: #000; border: 1px solid #222; border-radius: 18px; padding: 16px; width: 100%; outline: none; transition: 0.3s; color: #fff; }
        .input-style:focus { border-color: #06b6d4; box-shadow: 0 0 15px rgba(6, 182, 212, 0.3); }
    </style>
</head>
<body class="p-6">

    <div class="max-w-md w-full glass p-10">
        <div class="text-center mb-10">
            <h1 class="text-4xl font-black orbitron italic text-cyan-400">SIGN UP</h1>
            <p class="text-gray-500 text-[10px] uppercase tracking-[5px] mt-2 italic">Create New Terminal Identity</p>
        </div>

        <!-- মেসেজ ডিসপ্লে -->
        <?php if($error): ?>
            <div class="bg-red-600/10 border border-red-600/20 text-red-500 p-4 rounded-2xl text-center text-sm mb-6 animate-pulse">
                <i class="fas fa-exclamation-triangle mr-2"></i> <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <?php if($success): ?>
            <div class="bg-green-600/10 border border-green-600/20 text-green-500 p-4 rounded-2xl text-center text-sm mb-6">
                <i class="fas fa-check-circle mr-2"></i> <?php echo $success; ?>
                <br><a href="index.php" class="underline font-bold text-white block mt-2">লগইন করতে এখানে ক্লিক করুন</a>
            </div>
        <?php endif; ?>

        <form method="POST" class="space-y-6">
            <div>
                <label class="text-[10px] text-gray-500 uppercase font-bold ml-1 tracking-widest italic">Username</label>
                <input type="text" name="username" placeholder="Enter username" class="input-style mt-1" required>
            </div>
            <div>
                <label class="text-[10px] text-gray-500 uppercase font-bold ml-1 tracking-widest italic">Password</label>
                <input type="password" name="password" placeholder="••••••••" class="input-style mt-1" required>
            </div>
            <div>
                <label class="text-[10px] text-gray-500 uppercase font-bold ml-1 tracking-widest italic">Confirm Password</label>
                <input type="password" name="confirm_password" placeholder="••••••••" class="input-style mt-1" required>
            </div>

            <button type="submit" class="w-full bg-cyan-600 hover:bg-cyan-700 text-black font-black orbitron py-5 rounded-[22px] transition-all shadow-lg shadow-cyan-600/20 uppercase tracking-[2px] text-xs">
                Access Registration <i class="fas fa-chevron-right ml-2"></i>
            </button>
        </form>

        <div class="mt-10 text-center">
            <p class="text-gray-500 text-sm">Already a member? <a href="index.php" class="text-cyan-400 font-bold hover:underline">Login Terminal</a></p>
        </div>
    </div>

</body>
</html>