-- Migration: rename model config tables to singular naming
-- Target names:
--   llm_model_config
--   audio_model_config

SET @rename_llm_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'llm_model_configs'
    )
    AND NOT EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'llm_model_config'
    ),
    'RENAME TABLE llm_model_configs TO llm_model_config',
    'SELECT 1'
  )
);
PREPARE stmt FROM @rename_llm_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @rename_audio_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'audio_model_configs'
    )
    AND NOT EXISTS (
      SELECT 1 FROM information_schema.tables
      WHERE table_schema = DATABASE() AND table_name = 'audio_model_config'
    ),
    'RENAME TABLE audio_model_configs TO audio_model_config',
    'SELECT 1'
  )
);
PREPARE stmt FROM @rename_audio_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Remove possible redundant audio/voiceprint fields from llm table (idempotent)
SET @drop_audio_scene_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE() AND table_name = 'llm_model_config' AND column_name = 'audio_scene'
    ),
    'ALTER TABLE llm_model_config DROP COLUMN audio_scene',
    'SELECT 1'
  )
);
PREPARE stmt FROM @drop_audio_scene_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_asr_model_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE() AND table_name = 'llm_model_config' AND column_name = 'asr_model_name'
    ),
    'ALTER TABLE llm_model_config DROP COLUMN asr_model_name',
    'SELECT 1'
  )
);
PREPARE stmt FROM @drop_asr_model_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_asr_vocab_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE() AND table_name = 'llm_model_config' AND column_name = 'asr_vocabulary_id'
    ),
    'ALTER TABLE llm_model_config DROP COLUMN asr_vocabulary_id',
    'SELECT 1'
  )
);
PREPARE stmt FROM @drop_asr_vocab_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_vp_tpl_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE() AND table_name = 'llm_model_config' AND column_name = 'vp_template_text'
    ),
    'ALTER TABLE llm_model_config DROP COLUMN vp_template_text',
    'SELECT 1'
  )
);
PREPARE stmt FROM @drop_vp_tpl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_vp_duration_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE() AND table_name = 'llm_model_config' AND column_name = 'vp_duration_seconds'
    ),
    'ALTER TABLE llm_model_config DROP COLUMN vp_duration_seconds',
    'SELECT 1'
  )
);
PREPARE stmt FROM @drop_vp_duration_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_vp_rate_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE() AND table_name = 'llm_model_config' AND column_name = 'vp_sample_rate'
    ),
    'ALTER TABLE llm_model_config DROP COLUMN vp_sample_rate',
    'SELECT 1'
  )
);
PREPARE stmt FROM @drop_vp_rate_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_vp_channels_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE() AND table_name = 'llm_model_config' AND column_name = 'vp_channels'
    ),
    'ALTER TABLE llm_model_config DROP COLUMN vp_channels',
    'SELECT 1'
  )
);
PREPARE stmt FROM @drop_vp_channels_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @drop_vp_size_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE() AND table_name = 'llm_model_config' AND column_name = 'vp_max_size_bytes'
    ),
    'ALTER TABLE llm_model_config DROP COLUMN vp_max_size_bytes',
    'SELECT 1'
  )
);
PREPARE stmt FROM @drop_vp_size_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Clean potential non-LLM rows in llm table
DELETE FROM llm_model_config
WHERE model_code IN ('audio_model', 'voiceprint_model');
