-- Cleanup legacy hems_bases table after consolidation to unified bases table.
-- Safe/idempotent behavior:
-- 1) If only hems_bases exists -> rename to bases.
-- 2) If both tables exist -> copy missing rows from hems_bases into bases as HEMS category.
-- 3) Drop hems_bases afterwards when possible.

SET @schema := DATABASE();

SET @has_hems_bases := (
  SELECT COUNT(*)
  FROM information_schema.TABLES
  WHERE TABLE_SCHEMA = @schema AND TABLE_NAME = 'hems_bases'
);

SET @has_bases := (
  SELECT COUNT(*)
  FROM information_schema.TABLES
  WHERE TABLE_SCHEMA = @schema AND TABLE_NAME = 'bases'
);

-- Ensure HEMS category exists when unified bases table is present.
SET @has_base_categories := (
  SELECT COUNT(*)
  FROM information_schema.TABLES
  WHERE TABLE_SCHEMA = @schema AND TABLE_NAME = 'base_categories'
);

SET @sql_seed_hems_category := IF(@has_bases = 1 AND @has_base_categories = 1,
  'INSERT INTO base_categories (id, `key`, name, created_at, updated_at)
   SELECT UUID_TO_BIN(UUID(), true), ''hems'', ''HEMS Base'', UTC_TIMESTAMP(3), UTC_TIMESTAMP(3)
   WHERE NOT EXISTS (
     SELECT 1 FROM base_categories WHERE `key` = ''hems''
   )',
  'SELECT 1'
);
PREPARE stmt FROM @sql_seed_hems_category;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Case A: legacy only => rename directly.
SET @sql_rename_hems_bases := IF(@has_hems_bases = 1 AND @has_bases = 0,
  'RENAME TABLE hems_bases TO bases',
  'SELECT 1'
);
PREPARE stmt FROM @sql_rename_hems_bases;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Re-evaluate after possible rename.
SET @has_hems_bases := (
  SELECT COUNT(*)
  FROM information_schema.TABLES
  WHERE TABLE_SCHEMA = @schema AND TABLE_NAME = 'hems_bases'
);

SET @has_bases := (
  SELECT COUNT(*)
  FROM information_schema.TABLES
  WHERE TABLE_SCHEMA = @schema AND TABLE_NAME = 'bases'
);

-- Case B: both exist => backfill rows from legacy table into unified table.
SET @sql_backfill := IF(@has_hems_bases = 1 AND @has_bases = 1,
  'INSERT INTO bases (
      id, base, base_category_id, base_abbreviation, address,
      landline_number, mobile_number, email,
      position, sms_alert, checklist, leg_time,
      default_shift_time, utc, foto,
      dry, control_center, dul, visible,
      notes, is_active,
      created_by, updated_by, created_at, updated_at
    )
    SELECT
      hb.id,
      hb.base,
      bc.id,
      hb.base_abbreviation,
      hb.address,
      hb.landline_number,
      hb.mobile_number,
      hb.email,
      hb.position,
      hb.sms_alert,
      hb.checklist,
      hb.leg_time,
      hb.default_shift_time,
      hb.utc,
      hb.foto,
      hb.dry,
      hb.control_center,
      hb.dul,
      hb.visible,
      hb.notes,
      hb.is_active,
      hb.created_by,
      hb.updated_by,
      hb.created_at,
      hb.updated_at
    FROM hems_bases hb
    LEFT JOIN base_categories bc ON bc.`key` = ''hems''
    LEFT JOIN bases b ON b.id = hb.id
    WHERE b.id IS NULL',
  'SELECT 1'
);
PREPARE stmt FROM @sql_backfill;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Drop legacy table if still present.
SET @sql_drop_hems_bases := IF(@has_hems_bases = 1,
  'DROP TABLE hems_bases',
  'SELECT 1'
);
PREPARE stmt FROM @sql_drop_hems_bases;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
