-- =====================================================
-- SCRIPT 02: Add appversionid to installedapps
-- =====================================================
-- Date: 2025-11-25
-- Purpose: Add version tracking column to installedapps table
-- Status: REVERSIBLE (see ROLLBACK_02)
-- Estimated Time: < 1 minute
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

-- =====================================================
-- STEP 1: Add appversionid column to installedapps
-- =====================================================

-- Check if column exists before adding
SET @column_exists = (
    SELECT COUNT(*)
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = 'shopdb'
    AND TABLE_NAME = 'installedapps'
    AND COLUMN_NAME = 'appversionid'
);

SET @sql = IF(@column_exists = 0,
    'ALTER TABLE installedapps ADD COLUMN appversionid INT(11) NULL AFTER appid',
    'SELECT "Column appversionid already exists" AS status'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- =====================================================
-- STEP 2: Add foreign key constraint
-- =====================================================

-- Check if FK exists before adding
SET @fk_exists = (
    SELECT COUNT(*)
    FROM information_schema.TABLE_CONSTRAINTS
    WHERE TABLE_SCHEMA = 'shopdb'
    AND TABLE_NAME = 'installedapps'
    AND CONSTRAINT_NAME = 'fk_installedapps_appversionid'
);

SET @sql = IF(@fk_exists = 0,
    'ALTER TABLE installedapps ADD CONSTRAINT fk_installedapps_appversionid FOREIGN KEY (appversionid) REFERENCES appversions(appversionid) ON DELETE SET NULL ON UPDATE CASCADE',
    'SELECT "FK fk_installedapps_appversionid already exists" AS status'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- =====================================================
-- STEP 3: Add index for performance
-- =====================================================

SET @idx_exists = (
    SELECT COUNT(*)
    FROM information_schema.STATISTICS
    WHERE TABLE_SCHEMA = 'shopdb'
    AND TABLE_NAME = 'installedapps'
    AND INDEX_NAME = 'idx_appversionid'
);

SET @sql = IF(@idx_exists = 0,
    'ALTER TABLE installedapps ADD INDEX idx_appversionid (appversionid)',
    'SELECT "Index idx_appversionid already exists" AS status'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- =====================================================
-- VERIFICATION
-- =====================================================

SELECT '✓ installedapps table updated' AS status;

SELECT
    COLUMN_NAME,
    DATA_TYPE,
    IS_NULLABLE,
    COLUMN_DEFAULT
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'shopdb'
AND TABLE_NAME = 'installedapps'
ORDER BY ORDINAL_POSITION;

SELECT '✓ Script 02 completed successfully' AS status;

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- Existing installedapps records will have appversionid = NULL
-- PowerShell API will need updating to populate this field
-- Next: Run script 03_add_appid_to_notifications.sql
-- Rollback: Run ROLLBACK_02_installedapps_appversionid.sql
-- =====================================================
