-- ============================================================================
-- ShopDB Database Migration: Cleanup Redundant PC Machinetypes
-- Date: 2025-12-12
-- Purpose: Remove redundant PC machinetypes (34-46) since pctypeid handles categorization
-- ============================================================================
--
-- CHANGES:
-- 1. Drop duplicate appname_2 index on applications table
-- 2. Add primary key to installedapps table (if not exists)
-- 3. Migrate machines using PC-specific machinetypes to generic PC (33) + pctypeid
-- 4. Update models to use generic PC machinetype
-- 5. Remove unused PC machinetypes (34-43, 45-46), keep USB Device (44)
--
-- NOTE: This migration is IDEMPOTENT - safe to run multiple times
--
-- RUN ON: Production database
-- BACKUP FIRST: mysqldump -u root -p shopdb > shopdb_backup_$(date +%Y%m%d).sql
-- ============================================================================

-- Start transaction for safety
START TRANSACTION;

-- ============================================================================
-- 1. DROP DUPLICATE INDEX ON APPLICATIONS
-- ============================================================================
-- Check if appname_2 index exists before dropping
SET @index_exists = (SELECT COUNT(*) FROM information_schema.STATISTICS
                     WHERE table_schema = DATABASE()
                     AND table_name = 'applications'
                     AND index_name = 'appname_2');

SET @sql = IF(@index_exists > 0, 'DROP INDEX appname_2 ON applications', 'SELECT "Index appname_2 does not exist" AS status');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SELECT IF(@index_exists > 0, 'Dropped duplicate appname_2 index', 'Index appname_2 already removed') AS status;

-- ============================================================================
-- 2. ADD PRIMARY KEY TO INSTALLEDAPPS TABLE (if not exists)
-- ============================================================================
SET @col_exists = (SELECT COUNT(*) FROM information_schema.COLUMNS
                   WHERE table_schema = DATABASE()
                   AND table_name = 'installedapps'
                   AND column_name = 'installedappid');

SET @sql = IF(@col_exists = 0,
    'ALTER TABLE installedapps ADD COLUMN installedappid INT AUTO_INCREMENT PRIMARY KEY FIRST',
    'SELECT "Column installedappid already exists" AS status');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SELECT IF(@col_exists = 0, 'Added PK to installedapps', 'PK installedappid already exists') AS status;

-- ============================================================================
-- 3. MIGRATE MACHINES FROM PC-SPECIFIC TYPES TO GENERIC PC (33) + PCTYPEID
-- ============================================================================
-- These UPDATEs are already idempotent (won't change rows that don't match)

-- PC - Standard (36) → machinetypeid=33, pctypeid=1 (Standard)
UPDATE machines
SET machinetypeid = 33, pctypeid = 1
WHERE machinetypeid = 36;

SELECT CONCAT('Migrated ', ROW_COUNT(), ' PC-Standard machines') AS status;

-- PC - CMM (41) → machinetypeid=33, pctypeid=5 (CMM)
UPDATE machines
SET machinetypeid = 33, pctypeid = 5
WHERE machinetypeid = 41;

SELECT CONCAT('Migrated ', ROW_COUNT(), ' PC-CMM machines') AS status;

-- Handle any other PC types that might exist in production
-- PC - Shopfloor (37) → machinetypeid=33, pctypeid=3 (Shopfloor)
UPDATE machines
SET machinetypeid = 33, pctypeid = 3
WHERE machinetypeid = 37 AND pctypeid IS NULL;

-- PC - Engineer (38) → machinetypeid=33, pctypeid=2 (Engineer)
UPDATE machines
SET machinetypeid = 33, pctypeid = 2
WHERE machinetypeid = 38 AND pctypeid IS NULL;

-- PC - Wax Trace (42) → machinetypeid=33, pctypeid=6 (Wax / Trace)
UPDATE machines
SET machinetypeid = 33, pctypeid = 6
WHERE machinetypeid = 42 AND pctypeid IS NULL;

-- Catch-all: Any remaining 34-46 → machinetypeid=33, pctypeid=4 (Uncategorized)
UPDATE machines
SET machinetypeid = 33, pctypeid = 4
WHERE machinetypeid BETWEEN 34 AND 46 AND pctypeid IS NULL;

SELECT CONCAT('Total machines still using machinetypeid 34-46: ',
    (SELECT COUNT(*) FROM machines WHERE machinetypeid BETWEEN 34 AND 46)) AS status;

-- ============================================================================
-- 4. UPDATE MODELS TO USE GENERIC PC MACHINETYPE (33)
-- ============================================================================
UPDATE models
SET machinetypeid = 33
WHERE machinetypeid BETWEEN 34 AND 46;

SELECT CONCAT('Updated ', ROW_COUNT(), ' models to generic PC type') AS status;

-- ============================================================================
-- 5. DELETE REDUNDANT MACHINETYPES
-- ============================================================================
-- Keep 33 (PC) and 44 (USB Device), remove 34-43 and 45-46
-- These DELETEs are already idempotent (won't delete if rows don't exist)

DELETE FROM machinetypes WHERE machinetypeid BETWEEN 34 AND 43;
SELECT CONCAT('Deleted ', ROW_COUNT(), ' machinetypes (34-43)') AS status;

DELETE FROM machinetypes WHERE machinetypeid BETWEEN 45 AND 46;
SELECT CONCAT('Deleted ', ROW_COUNT(), ' machinetypes (45-46)') AS status;

-- ============================================================================
-- VERIFICATION
-- ============================================================================
SELECT 'VERIFICATION - Remaining machinetypes >= 33:' AS info;
SELECT machinetypeid, machinetype FROM machinetypes WHERE machinetypeid >= 33;

SELECT 'VERIFICATION - Machines by pctypeid:' AS info;
SELECT pt.typename, COUNT(*) as count
FROM machines m
JOIN pctype pt ON m.pctypeid = pt.pctypeid
WHERE m.pctypeid IS NOT NULL
GROUP BY m.pctypeid
ORDER BY count DESC;

SELECT 'VERIFICATION - Applications indexes:' AS info;
SELECT index_name, column_name FROM information_schema.STATISTICS
WHERE table_schema = DATABASE() AND table_name = 'applications';

-- ============================================================================
-- COMMIT
-- ============================================================================
COMMIT;

SELECT 'Migration completed successfully!' AS status;
