<?php

require_once __DIR__ . '/config.php';

function scstatsitem_bootstrap(): void
{
    static $bootstrapped = false;

    if ($bootstrapped) {
        return;
    }

    $db = db();
    $db->exec(
        "CREATE TABLE IF NOT EXISTS tbl_scstatsitem (
            cl_scstatsitem_id INT(11) NOT NULL AUTO_INCREMENT,
            cl_scstatsitem_name VARCHAR(255) NOT NULL,
            cl_scstatsitem_unit VARCHAR(10) NOT NULL DEFAULT '%',
            cl_scstatsitem_created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (cl_scstatsitem_id),
            UNIQUE KEY uq_scstatsitem_name (cl_scstatsitem_name)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci"
    );

    $columns_stmt = $db->query("SHOW COLUMNS FROM tbl_scstatsitem LIKE 'cl_scstatsitem_unit'");
    $has_unit = (bool) $columns_stmt->fetch();
    if (!$has_unit) {
        $db->exec("ALTER TABLE tbl_scstatsitem ADD COLUMN cl_scstatsitem_unit VARCHAR(10) NOT NULL DEFAULT '%' AFTER cl_scstatsitem_name");
    }

    $bootstrapped = true;
}
