-- Migration: Rename batch_settings to admin_settings_batch
-- Date: 2025-01-XX
-- Description: Renames batch_settings table to admin_settings_batch and migrates existing data

-- Check if batch_settings table exists and has data
-- If it exists, rename it to admin_settings_batch
-- If admin_settings_batch already exists, migrate data from batch_settings

-- Step 1: Create admin_settings_batch table if it doesn't exist
CREATE TABLE IF NOT EXISTS admin_settings_batch (
    id VARCHAR(36) PRIMARY KEY,
    setting_key VARCHAR(255) NOT NULL UNIQUE,
    setting_value TEXT NOT NULL,
    description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_setting_key (setting_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Step 2: Migrate data from batch_settings to admin_settings_batch if batch_settings exists
-- This will only insert records that don't already exist (based on setting_key)
-- Note: If batch_settings table doesn't exist, this will be skipped by the migration runner
INSERT INTO admin_settings_batch (id, setting_key, setting_value, description, created_at, updated_at)
SELECT id, setting_key, setting_value, description, created_at, updated_at
FROM batch_settings
WHERE NOT EXISTS (
    SELECT 1 FROM admin_settings_batch WHERE admin_settings_batch.setting_key = batch_settings.setting_key
);

-- Step 3: Drop batch_settings table if it exists
DROP TABLE IF EXISTS batch_settings;

