
<?php
/**
 * INSTALLER & DATABASE DUMPER (MARIA DB SAFE)
 * ✅ Re-runnable
 * ✅ No reserved keywords
 * ✅ No JSON type (uses TEXT for MariaDB compatibility)
 * ✅ Safe CREATE IF NOT EXISTS
 * DELETE AFTER USE
 */

/* =========================
   CONFIG
   ========================= */
$dbHost = "localhost";
$dbName = "tasmoybb_hub";
$dbUser = "tasmoybb_hub";
$dbPass = "LEXUSis350";

/* =========================
   CONNECT
   ========================= */
$pdo = new PDO(
    "mysql:host=$dbHost;charset=utf8mb4",
    $dbUser,
    $dbPass,
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);

$pdo->exec("CREATE DATABASE IF NOT EXISTS `$dbName` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
$pdo->exec("USE `$dbName`");

/* =========================
   TABLES
   ========================= */
$sql = [

"CREATE TABLE IF NOT EXISTS users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(50) UNIQUE NOT NULL,
  password_hash VARCHAR(255) NOT NULL,
  race_id INT NOT NULL,
  cosmetic_settings TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",

"CREATE TABLE IF NOT EXISTS matches (
  id INT AUTO_INCREMENT PRIMARY KEY,
  player_1_id INT,
  player_2_id INT,
  turn_number INT DEFAULT 1,
  initiative_player_id INT,
  active_event INT,
  status ENUM('active','finished') DEFAULT 'active',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",

"CREATE TABLE IF NOT EXISTS turns (
  id INT AUTO_INCREMENT PRIMARY KEY,
  match_id INT,
  turn_number INT,
  player_id INT,
  submitted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  turn_payload TEXT
)",

"CREATE TABLE IF NOT EXISTS unit_definitions (
  id INT AUTO_INCREMENT PRIMARY KEY,
  race_id INT,
  name VARCHAR(50),
  cost INT,
  max_hp INT,
  damage INT,
  attack_range INT,
  speed INT,
  armor INT,
  role VARCHAR(20),
  abilities TEXT
)",

"CREATE TABLE IF NOT EXISTS unit_instances (
  id INT AUTO_INCREMENT PRIMARY KEY,
  match_id INT,
  unit_definition_id INT,
  owner_id INT,
  current_hp INT,
  x INT,
  y INT,
  status_effects TEXT,
  spawned_turn INT
)",

"CREATE TABLE IF NOT EXISTS events (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(50),
  description TEXT,
  type VARCHAR(20),
  payload TEXT
)",

"CREATE TABLE IF NOT EXISTS replays (
  id INT AUTO_INCREMENT PRIMARY KEY,
  match_id INT,
  turn_number INT,
  resolved_state TEXT
)",

"CREATE TABLE IF NOT EXISTS unit_skins (
  id INT AUTO_INCREMENT PRIMARY KEY,
  user_id INT,
  unit_definition_id INT,
  file_path VARCHAR(255),
  approved TINYINT DEFAULT 0,
  uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)"
];

foreach ($sql as $q) {
    $pdo->exec($q);
}

/* =========================
   DIRECTORIES
   ========================= */
$dirs = [
  "public/assets/css",
  "public/assets/js",
  "public/assets/images",
  "public/assets/skins",
  "api",
  "engine",
  "data",
  "auth",
  "logs"
];

foreach ($dirs as $d) {
    if (!is_dir($d)) mkdir($d, 0777, true);
}

/* =========================
   FILES
   ========================= */
$files = [

"data/config.php" => "<?php
return [
  'host' => '$dbHost',
  'db'   => '$dbName',
  'user' => '$dbUser',
  'pass' => '$dbPass'
];
",

"data/db.php" => "<?php
\$cfg = require __DIR__.'/config.php';
\$pdo = new PDO(
  \"mysql:host={\$cfg['host']};dbname={\$cfg['db']};charset=utf8mb4\",
  \$cfg['user'],
  \$cfg['pass'],
  [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
",

"public/index.php" => "<?php header('Location: game.php');",

"public/game.php" => "<!DOCTYPE html>
<html>
<head>
assets/css/style.css
assets/js/game.jsscript>
</head>
<body>
<div id='game-board'></div>
</body>
</html>",

"public/assets/css/style.css" => "
body{background:#0f1220;color:#e6e6eb;font-family:Arial}
#game-board{
  width:960px;height:640px;margin:auto;
  display:grid;
  grid-template-columns:repeat(12,1fr);
  grid-template-rows:repeat(8,1fr);
}
.tile{border:1px solid #222}
",

"public/assets/js/game.js" => "
fetch('/api/getGameState.php')
.then(r=>r.json())
.then(d=>console.log(d));
",

"api/getGameState.php" => "<?php
require '../data/db.php';
echo json_encode(['status'=>'ok']);
",

"api/submitTurn.php" => "<?php require '../data/db.php';",

"engine/TurnResolver.php" => "<?php class TurnResolver{ public static function resolve(int \$matchId){} }",
"engine/CombatResolver.php" => "<?php class CombatResolver{}",
"engine/TargetingLogic.php" => "<?php class TargetingLogic{}",
"engine/EventResolver.php" => "<?php class EventResolver{}",

"auth/login.php" => "<?php",
"auth/register.php" => "<?php",
"auth/auth_check.php" => "<?php",

"logs/.gitkeep" => ""
];

foreach ($files as $path => $content) {
    if (!file_exists($path)) file_put_contents($path, $content);
}

echo "✅ INSTALL COMPLETE – MARIA DB SAFE";