SET @schema_name := DATABASE();

SET @lang_column_exists := (
    SELECT COUNT(1)
    FROM information_schema.columns
    WHERE table_schema = @schema_name
      AND table_name = 'chat_character_banner'
      AND column_name = 'lang'
);

SET @add_lang_column_sql := IF(
    @lang_column_exists = 0,
    'ALTER TABLE chat_character_banner ADD COLUMN lang VARCHAR(10) NULL COMMENT ''배너 노출 언어'' AFTER sort_order',
    'SELECT ''chat_character_banner.lang already exists'' AS message'
);

PREPARE add_lang_column_stmt FROM @add_lang_column_sql;
EXECUTE add_lang_column_stmt;
DEALLOCATE PREPARE add_lang_column_stmt;

UPDATE chat_character_banner
SET lang = 'KO'
WHERE lang IS NULL;

SET @lang_column_nullable := (
    SELECT IS_NULLABLE
    FROM information_schema.columns
    WHERE table_schema = @schema_name
      AND table_name = 'chat_character_banner'
      AND column_name = 'lang'
);

SET @alter_lang_column_sql := IF(
    @lang_column_nullable = 'YES',
    'ALTER TABLE chat_character_banner MODIFY COLUMN lang VARCHAR(10) NOT NULL DEFAULT ''KO'' COMMENT ''배너 노출 언어 (KO 기본, EN/JA 추가 가능)''',
    'SELECT ''chat_character_banner.lang already normalized'' AS message'
);

PREPARE alter_lang_column_stmt FROM @alter_lang_column_sql;
EXECUTE alter_lang_column_stmt;
DEALLOCATE PREPARE alter_lang_column_stmt;
