<?php
session_start();

// ডাটা ম্যাপ
$data_map = [
    0 => ['t' => 'Small', 'c' => ['Red', 'Violet'], 'class' => 'num-0', 'dots' => ['red', 'violet']],
    1 => ['t' => 'Small', 'c' => ['Green'], 'class' => 'num-1', 'dots' => ['green']],
    2 => ['t' => 'Small', 'c' => ['Red'], 'class' => 'num-2', 'dots' => ['red']],
    3 => ['t' => 'Small', 'c' => ['Green'], 'class' => 'num-3', 'dots' => ['green']],
    4 => ['t' => 'Small', 'c' => ['Red'], 'class' => 'num-4', 'dots' => ['red']],
    5 => ['t' => 'Big',   'c' => ['Green', 'Violet'], 'class' => 'num-5', 'dots' => ['green', 'violet']],
    6 => ['t' => 'Big',   'c' => ['Red'], 'class' => 'num-6', 'dots' => ['red']],
    7 => ['t' => 'Big',   'c' => ['Green'], 'class' => 'num-7', 'dots' => ['green']],
    8 => ['t' => 'Big',   'c' => ['Red'], 'class' => 'num-8', 'dots' => ['red']],
    9 => ['t' => 'Big',   'c' => ['Green'], 'class' => 'num-9', 'dots' => ['green']],
];

// বর্তমান লিস্ট ম্যানেজমেন্ট
if (!isset($_SESSION['history'])) {
    $_SESSION['history'] = [];
}

$limit = isset($_POST['limit']) ? (int)$_POST['limit'] : 7;

if (isset($_POST['add_number'])) {
    $new_num = $_POST['new_num'];
    if ($new_num !== "") {
        // নতুন নম্বর সবার শুরুতে যোগ করা
        array_unshift($_SESSION['history'], (int)$new_num);
        // লিমিট অনুযায়ী অতিরিক্ত ডাটা রিমুভ করা
        $_SESSION['history'] = array_slice($_SESSION['history'], 0, $limit);
    }
}

if (isset($_POST['reset'])) {
    $_SESSION['history'] = [];
}

// AI লজিক
$analysis = null;
if (count($_SESSION['history']) > 0) {
    $history = $_SESSION['history'];
    $big_count = 0; $red_count = 0; $green_count = 0;
    foreach ($history as $n) {
        $info = $data_map[$n];
        if ($info['t'] == 'Big') $big_count++;
        if (in_array('Red', $info['c'])) $red_count++;
        if (in_array('Green', $info['c'])) $green_count++;
    }
    $analysis = [
        'type' => ($big_count >= (count($history)/2)) ? "Small" : "Big",
        'color' => ($red_count >= $green_count) ? "Green" : "Red",
        'violet' => ($history[0] == 0 || $history[0] == 5)
    ];
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Number Brain Predictor</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { background-color: #f4f6f9; font-family: 'Segoe UI', sans-serif; }
        .container { max-width: 450px; padding: 20px; }
        .brain-card { background: white; border-radius: 20px; padding: 20px; box-shadow: 0 10px 30px rgba(0,0,0,0.08); margin-bottom: 20px; }
        .prediction-box { padding: 20px; border-radius: 15px; text-align: center; color: white; margin-bottom: 20px; font-weight: bold; }
        .bg-red { background: linear-gradient(135deg, #ff416c, #ff4b2b); }
        .bg-green { background: linear-gradient(135deg, #11998e, #38ef7d); }
        .bg-violet-red { background: linear-gradient(135deg, #ff416c, #9b59b6); }
        .bg-violet-green { background: linear-gradient(135deg, #11998e, #9b59b6); }
        
        .dot { height: 12px; width: 12px; border-radius: 50%; display: inline-block; margin-left: 5px; }
        .dot-red { background: #fa3c3c; } .dot-green { background: #00b977; } .dot-violet { background: #eb43ff; }
        
        .num-list-item { display: flex; justify-content: space-between; align-items: center; padding: 10px; border-bottom: 1px solid #eee; }
        .num-circle { width: 35px; height: 35px; border-radius: 50%; display: flex; align-items: center; justify-content: center; color: white; font-weight: bold; }
        
        /* নম্বর অনুযায়ী রঙ */
        .color-0, .color-5 { background: #9b59b6; }
        .color-1, .color-3, .color-7, .color-9 { background: #00b977; }
        .color-2, .color-4, .color-6, .color-8 { background: #fa3c3c; }
    </style>
</head>
<body>

<div class="container">
    <h4 class="text-center mb-4">🧠 AI Pattern Brain</h4>

    <!-- AI Prediction -->
    <?php if ($analysis): ?>
        <?php 
            $box_class = ($analysis['color'] == 'Red') ? 'bg-red' : 'bg-green';
            if ($analysis['violet']) $box_class = ($analysis['color'] == 'Red') ? 'bg-violet-red' : 'bg-violet-green';
        ?>
        <div class="prediction-box <?php echo $box_class; ?> shadow-lg">
            <div>NEXT PREDICTION</div>
            <div class="display-4"><?php echo $analysis['type']; ?></div>
            <div>Color: <?php echo $analysis['color']; ?> <?php echo $analysis['violet'] ? '+ Violet' : ''; ?></div>
        </div>
    <?php endif; ?>

    <div class="brain-card">
        <form method="POST">
            <div class="row g-2 mb-3">
                <div class="col-6">
                    <label class="small fw-bold">Data Limit (Type)</label>
                    <input type="number" name="limit" class="form-control" value="<?php echo $limit; ?>" min="1" max="50">
                </div>
                <div class="col-6">
                    <label class="small fw-bold">New Number</label>
                    <input type="number" name="new_num" class="form-control" min="0" max="9" autofocus>
                </div>
            </div>
            <div class="d-flex gap-2">
                <button type="submit" name="add_number" class="btn btn-primary flex-grow-1 fw-bold">Add & Analyze</button>
                <button type="submit" name="reset" class="btn btn-outline-danger">Reset</button>
            </div>
        </form>
    </div>

    <!-- লিস্টিং এরিয়া -->
    <div class="brain-card p-0 overflow-hidden">
        <div class="bg-dark text-white p-2 text-center small">Active History (Max: <?php echo $limit; ?>)</div>
        <div id="historyList">
            <?php if (empty($_SESSION['history'])): ?>
                <p class="text-center p-4 text-muted">কোনো ডাটা নেই</p>
            <?php else: ?>
                <?php foreach ($_SESSION['history'] as $num): 
                    $info = $data_map[$num];
                ?>
                <div class="num-list-item">
                    <div class="d-flex align-items-center">
                        <div class="num-circle color-<?php echo $num; ?> me-3"><?php echo $num; ?></div>
                        <strong><?php echo $info['t']; ?></strong>
                    </div>
                    <div>
                        <?php foreach ($info['dots'] as $d) echo "<span class='dot dot-$d'></span>"; ?>
                    </div>
                </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>
    
    <div class="text-center text-muted mt-3">
        <small>নিখুঁত ফলাফলের জন্য অন্তত ৫-১০টি ইনপুট দিন।</small>
    </div>
</div>

</body>
</html>