<?php
// workflow_matrix.php
require_once __DIR__ . '/../../security/core/DB.php';
$pdo = DB::getConnection();

// Načítanie dát
$stmt = $pdo->query("SELECT id, datum, cisla FROM vsetkoalebonic_tahy ORDER BY datum DESC LIMIT 50");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Spracovanie čísel
$counts = [];
foreach ($rows as $r) {
    $nums = explode(',', $r['cisla']);
    foreach ($nums as $n) {
        $n = (int)trim($n);
        if ($n > 0) $counts[$n] = ($counts[$n] ?? 0) + 1;
    }
}
arsort($counts);

// Rozdelenie
$hot = array_slice(array_keys($counts), 0, 6);
$cold = array_slice(array_keys(array_reverse($counts, true)), 0, 5);
$top = array_slice($hot, 0, 4);
$antitip = array_slice($cold, 0, 2);

// Matrix
echo "<!DOCTYPE html><html lang='sk'><head>
<meta charset='UTF-8'>
<title>Workflow Matrix</title>
<style>
body { font-family: Arial, sans-serif; background:#111; color:#eee; text-align:center; }
.grid { display:grid; grid-template-columns: repeat(10, 1fr); gap:5px; width:600px; margin:20px auto; }
.cell { padding:8px; border-radius:6px; font-weight:bold; }
.hot { background:#e74c3c; color:#fff; }
.cold { background:#3498db; color:#fff; }
.top { border:2px solid #fff; }
.antitip { border:2px dashed #f1c40f; }
.legend { background:#222; padding:10px; border-radius:8px; margin-top:20px; text-align:left; width:600px; margin:auto; }
.legend h3 { margin-bottom:5px; color:#f39c12; }
.legend p { font-size:14px; margin:3px 0; }
</style></head><body>";

echo "<h2>Matrix Workflow</h2>";
echo "<div class='grid'>";
for ($i=1; $i<=50; $i++) {
    $cls = "cell";
    if (in_array($i, $hot)) $cls .= " hot";
    if (in_array($i, $cold)) $cls .= " cold";
    if (in_array($i, $top)) $cls .= " top";
    if (in_array($i, $antitip)) $cls .= " antitip";
    echo "<div class='$cls'>$i</div>";
}
echo "</div>";

// Legenda
echo "<div class='legend'>
<h3>Legenda Workflow</h3>
<p><b>A.</b> Vyber pozície v matrixe (napr. polovica A/B podľa ťahov)</p>
<p><b>B.</b> Vlož 6 HOT čísel a 5 COLD čísel</p>
<p><b>C.</b> Z HOT vyber 4 TOP čísla (najčastejšie)</p>
<p><b>D.</b> Z COLD vyber 2 ANTITIP (najmenej časté)</p>
<p><b>E.</b> Sleduj opakovania po každom ťahu – uprav podľa trendu</p>
</div>";

echo "</body></html>";