<?php
if (session_status() === PHP_SESSION_NONE) { session_start(); }
require 'config/db.php';

// ডাটাবেস থেকে সেটিংস্ ডাটা তুলে আনা
$set = $pdo->query("SELECT * FROM settings WHERE id = 1")->fetch();

// সাইট নামকে দুই ভাগে ভাগ করা
$site_name = $set['site_name'] ?? 'LUDO PRO';
$name_parts = explode(' ', $site_name, 2);
$first_part = $name_parts[0];
$second_part = $name_parts[1] ?? '';

// যদি ইউজার ইতিমধ্যে লগইন করা থাকে তবে ড্যাশবোর্ডে রিডাইরেক্ট করুন
if (isset($_SESSION['user_id'])) {
    header("Location: dashboard.php");
    exit();
}

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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = trim($_POST['username']);
    $email = trim($_POST['email']);
    $phone = trim($_POST['phone']); 
    $password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];

    if (empty($username) || empty($email) || empty($phone) || empty($password)) {
        $error = "সব ফিল্ড পূরণ করা আবশ্যক!";
    } elseif ($password !== $confirm_password) {
        $error = "পাসওয়ার্ড ম্যাচ করেনি!";
    } elseif (strlen($password) < 6) {
        $error = "পাসওয়ার্ড অন্তত ৬ অক্ষরের হতে হবে!";
    } else {
        $stmt = $pdo->prepare("SELECT id FROM users WHERE email = ? OR username = ? OR phone = ?");
        $stmt->execute([$email, $username, $phone]);
        
        if ($stmt->rowCount() > 0) {
            $error = "এই ইমেইল, ইউজারনেম বা ফোন নাম্বার দিয়ে ইতিমধ্যে অ্যাকাউন্ট খোলা আছে!";
        } else {
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);
            // এখানে আপনি চাইলে ডাটাবেস থেকে বোনাস ভ্যালু নিতে পারেন, আপাতত ২০ রাখা হয়েছে
            $bonus = 20.00; 
            $sql = "INSERT INTO users (username, email, phone, password, balance, created_at) VALUES (?, ?, ?, ?, ?, NOW())";
            $stmt = $pdo->prepare($sql);
            
            if ($stmt->execute([$username, $email, $phone, $hashed_password, $bonus])) {
                $success = "অ্যাকাউন্ট তৈরি সফল হয়েছে! লগইন করা হচ্ছে...";
                header("refresh:2;url=login.php"); 
            } else {
                $error = "সার্ভারে সমস্যা হয়েছে, কিছুক্ষণ পর আবার চেষ্টা করুন।";
            }
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Register - <?= htmlspecialchars($site_name) ?></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">
    <link href="https://fonts.googleapis.com/css2?family=Rajdhani:wght@500;600;700&display=swap" rel="stylesheet">

    <style>
        body { 
            background: #020617; 
            color: #f1f5f9; 
            font-family: 'Rajdhani', sans-serif;
            background-image: radial-gradient(circle at 50% 0%, #1e1b4b 0%, #020617 70%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .glass-card { 
            background: rgba(30, 41, 59, 0.4); 
            backdrop-filter: blur(12px); 
            border: 1px solid rgba(255,255,255,0.05); 
            box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.2);
        }
        .input-group {
            background: rgba(15, 23, 42, 0.6);
            border: 1px solid rgba(255,255,255,0.05);
            transition: all 0.3s ease;
        }
        .input-group:focus-within {
            border-color: #3b82f6;
            box-shadow: 0 0 15px rgba(59, 130, 246, 0.2);
        }
        input:-webkit-autofill {
            -webkit-box-shadow: 0 0 0 30px #0f172a inset !important;
            -webkit-text-fill-color: white !important;
        }
    </style>
</head>
<body class="p-4 py-8">

    <div class="fixed top-0 left-0 w-full h-full overflow-hidden -z-10 pointer-events-none">
        <div class="absolute top-[-10%] right-[-10%] w-64 h-64 bg-blue-600/20 rounded-full blur-[80px]"></div>
        <div class="absolute bottom-[-10%] left-[-10%] w-64 h-64 bg-indigo-600/20 rounded-full blur-[80px]"></div>
    </div>

    <div class="w-full max-w-md">
        
        <div class="text-center mb-6">
            <div class="inline-flex items-center justify-center mb-3">
                <?php if(!empty($set['logo'])): ?>
                    <img src="../assets/img/<?= $set['logo'] ?>" alt="Logo" class="h-14 w-auto">
                <?php else: ?>
                    <div class="w-14 h-14 bg-gradient-to-br from-blue-600 to-indigo-600 rounded-2xl flex items-center justify-center shadow-lg shadow-blue-500/20 transform rotate-3">
                        <i class="fas fa-dice text-2xl text-white"></i>
                    </div>
                <?php endif; ?>
            </div>
            <h1 class="text-4xl font-black tracking-tighter text-white uppercase">
                <?= htmlspecialchars($first_part) ?><span class="text-blue-500"><?= htmlspecialchars($second_part) ?></span>
            </h1>
            <p class="text-slate-400 text-sm font-medium mt-1">Create Account & Get Bonus ৳20</p>
        </div>

        <div class="glass-card p-6 rounded-[2rem] relative overflow-hidden">
            <h2 class="text-xl font-bold text-white mb-5 text-center">Sign Up</h2>

            <?php if($error): ?>
                <div class="bg-red-500/10 border border-red-500/20 text-red-400 p-3 mb-5 rounded-xl text-xs font-bold flex items-center gap-2">
                    <i class="fas fa-exclamation-circle"></i>
                    <?= $error ?>
                </div>
            <?php endif; ?>

            <?php if($success): ?>
                <div class="bg-green-500/10 border border-green-500/20 text-green-400 p-3 mb-5 rounded-xl text-xs font-bold flex items-center gap-2">
                    <i class="fas fa-check-circle"></i>
                    <?= $success ?>
                </div>
            <?php endif; ?>

            <form action="" method="POST" class="space-y-4">
                <div>
                    <label class="block text-[10px] font-bold text-slate-400 uppercase tracking-wider mb-1.5 ml-1">Username</label>
                    <div class="input-group flex items-center rounded-xl px-4 py-2.5">
                        <i class="fas fa-user text-slate-500 mr-3 text-sm"></i>
                        <input type="text" name="username" required class="bg-transparent border-none outline-none text-white w-full text-sm placeholder-slate-600 font-medium" placeholder="Ex: KingLudo123">
                    </div>
                </div>

                <div>
                    <label class="block text-[10px] font-bold text-slate-400 uppercase tracking-wider mb-1.5 ml-1">Phone Number</label>
                    <div class="input-group flex items-center rounded-xl px-4 py-2.5">
                        <i class="fas fa-phone-alt text-slate-500 mr-3 text-sm"></i>
                        <input type="tel" name="phone" required class="bg-transparent border-none outline-none text-white w-full text-sm placeholder-slate-600 font-medium" placeholder="017xxxxxxxx">
                    </div>
                </div>

                <div>
                    <label class="block text-[10px] font-bold text-slate-400 uppercase tracking-wider mb-1.5 ml-1">Email Address</label>
                    <div class="input-group flex items-center rounded-xl px-4 py-2.5">
                        <i class="fas fa-envelope text-slate-500 mr-3 text-sm"></i>
                        <input type="email" name="email" required class="bg-transparent border-none outline-none text-white w-full text-sm placeholder-slate-600 font-medium" placeholder="name@example.com">
                    </div>
                </div>

                <div class="grid grid-cols-2 gap-3">
                    <div>
                        <label class="block text-[10px] font-bold text-slate-400 uppercase tracking-wider mb-1.5 ml-1">Password</label>
                        <div class="input-group flex items-center rounded-xl px-4 py-2.5">
                            <i class="fas fa-lock text-slate-500 mr-2 text-sm"></i>
                            <input type="password" name="password" required class="bg-transparent border-none outline-none text-white w-full text-sm placeholder-slate-600 font-medium" placeholder="******">
                        </div>
                    </div>
                    <div>
                        <label class="block text-[10px] font-bold text-slate-400 uppercase tracking-wider mb-1.5 ml-1">Confirm</label>
                        <div class="input-group flex items-center rounded-xl px-4 py-2.5">
                            <i class="fas fa-check-circle text-slate-500 mr-2 text-sm"></i>
                            <input type="password" name="confirm_password" required class="bg-transparent border-none outline-none text-white w-full text-sm placeholder-slate-600 font-medium" placeholder="******">
                        </div>
                    </div>
                </div>

                <button type="submit" class="w-full bg-gradient-to-r from-blue-600 to-indigo-600 hover:from-blue-500 hover:to-indigo-500 text-white py-3.5 rounded-xl font-bold text-sm uppercase tracking-wider shadow-lg shadow-blue-900/20 transition-all active:scale-95 flex items-center justify-center gap-2 mt-4">
                    <span>Create Account</span>
                    <i class="fas fa-user-plus"></i>
                </button>
            </form>

            <div class="mt-6 text-center border-t border-white/5 pt-4">
                <p class="text-slate-400 text-sm">
                    Already have an account? 
                    <a href="login.php" class="text-blue-500 font-bold hover:text-blue-400 transition ml-1">Login Here</a>
                </p>
            </div>
        </div>
        
        <p class="text-center text-[10px] text-slate-600 mt-6 font-bold uppercase tracking-widest">
            &copy; <?= date('Y') ?> <?= htmlspecialchars($site_name) ?>. All rights reserved.
        </p>

    </div>
</body>
</html>