<?php

require_once __DIR__ . '/config.php';

function scitems_column_exists(PDO $db, string $table, string $column): bool
{
    $stmt = $db->query("SHOW COLUMNS FROM `{$table}` LIKE " . $db->quote($column));

    return (bool) $stmt->fetch();
}

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

    if ($bootstrapped) {
        return;
    }

    $db = db();

    $db->exec(
        "CREATE TABLE IF NOT EXISTS tbl_scobjs (
            cl_scobjs_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
            cl_scobjs_name VARCHAR(255) NOT NULL,
            cl_scobjs_type VARCHAR(100) DEFAULT NULL,
            cl_scobjs_subtype VARCHAR(100) DEFAULT NULL,
            cl_scobjs_uuid VARCHAR(100) NOT NULL,
            cl_scobjs_rarity VARCHAR(10) DEFAULT '',
            cl_scobjs_about TEXT DEFAULT NULL,
            cl_scobjs_description TEXT DEFAULT NULL,
            PRIMARY KEY (cl_scobjs_id),
            UNIQUE KEY cl_scobjs_uuid (cl_scobjs_uuid)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"
    );

    if (!scitems_column_exists($db, 'tbl_scobjs', 'cl_scobjs_description')) {
        $db->exec(
            'ALTER TABLE tbl_scobjs
             ADD COLUMN cl_scobjs_description TEXT DEFAULT NULL AFTER cl_scobjs_about'
        );
    }

    $bootstrapped = true;
}
