-- ============================================================================
-- PHASE 2 MIGRATION - SCRIPT 08: Update Schema for API Compatibility
-- ============================================================================
-- Purpose: Rename pcid columns to machineid in tables still using old naming
-- Required for: api.asp to work correctly with Phase 2 schema
-- Run after: 01-07 migration scripts
-- Fixed for MySQL 5.6 compatibility
-- ============================================================================

USE shopdb;

-- ============================================================================
-- STEP 1: Rename pcid to machineid in pc_comm_config
-- ============================================================================

-- Check if column exists first
SET @column_exists = (
    SELECT COUNT(*)
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = 'shopdb'
      AND TABLE_NAME = 'pc_comm_config'
      AND COLUMN_NAME = 'pcid'
);

-- Only rename if pcid column exists
SET @sql = IF(@column_exists > 0,
    'ALTER TABLE pc_comm_config CHANGE COLUMN pcid machineid INT(11)',
    'SELECT "pc_comm_config already has machineid column" AS status'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Add index for performance (check if exists first)
SET @index_exists = (
    SELECT COUNT(*)
    FROM INFORMATION_SCHEMA.STATISTICS
    WHERE TABLE_SCHEMA = 'shopdb'
      AND TABLE_NAME = 'pc_comm_config'
      AND INDEX_NAME = 'idx_machineid'
);

SET @sql = IF(@index_exists = 0,
    'ALTER TABLE pc_comm_config ADD INDEX idx_machineid (machineid)',
    'SELECT "Index idx_machineid already exists on pc_comm_config" AS status'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SELECT 'pc_comm_config updated successfully' AS result;

-- ============================================================================
-- STEP 2: Rename pcid to machineid in pc_dnc_config
-- ============================================================================

-- Check if column exists first
SET @column_exists = (
    SELECT COUNT(*)
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = 'shopdb'
      AND TABLE_NAME = 'pc_dnc_config'
      AND COLUMN_NAME = 'pcid'
);

-- Only rename if pcid column exists
SET @sql = IF(@column_exists > 0,
    'ALTER TABLE pc_dnc_config CHANGE COLUMN pcid machineid INT(11)',
    'SELECT "pc_dnc_config already has machineid column" AS status'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Add index for performance (check if exists first)
SET @index_exists = (
    SELECT COUNT(*)
    FROM INFORMATION_SCHEMA.STATISTICS
    WHERE TABLE_SCHEMA = 'shopdb'
      AND TABLE_NAME = 'pc_dnc_config'
      AND INDEX_NAME = 'idx_machineid'
);

SET @sql = IF(@index_exists = 0,
    'ALTER TABLE pc_dnc_config ADD INDEX idx_machineid (machineid)',
    'SELECT "Index idx_machineid already exists on pc_dnc_config" AS status'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SELECT 'pc_dnc_config updated successfully' AS result;

-- ============================================================================
-- STEP 3: Verification
-- ============================================================================

-- Show table structures
SELECT 'pc_comm_config structure:' AS info;
DESCRIBE pc_comm_config;

SELECT 'pc_dnc_config structure:' AS info;
DESCRIBE pc_dnc_config;

-- Count records
SELECT
    (SELECT COUNT(*) FROM pc_comm_config) AS comm_config_records,
    (SELECT COUNT(*) FROM pc_dnc_config) AS dnc_config_records;

-- ============================================================================
-- COMPLETE
-- ============================================================================

SELECT '✅ Schema update complete - api.asp ready' AS status;
