-- =====================================================
-- SCRIPT 03: Add optional appid to notifications
-- =====================================================
-- Date: 2025-11-25
-- Purpose: Allow notifications to be optionally linked to an application
-- Status: REVERSIBLE (see ROLLBACK_03)
-- Estimated Time: < 1 minute
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

-- =====================================================
-- STEP 1: Add appid column to notifications
-- =====================================================

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

SET @sql = IF(@column_exists = 0,
    'ALTER TABLE notifications ADD COLUMN appid TINYINT(4) NULL AFTER businessunitid',
    'SELECT "Column appid 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 = 'notifications'
    AND CONSTRAINT_NAME = 'fk_notifications_appid'
);

SET @sql = IF(@fk_exists = 0,
    'ALTER TABLE notifications ADD CONSTRAINT fk_notifications_appid FOREIGN KEY (appid) REFERENCES applications(appid) ON DELETE SET NULL ON UPDATE CASCADE',
    'SELECT "FK fk_notifications_appid 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 = 'notifications'
    AND INDEX_NAME = 'idx_notifications_appid'
);

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

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

SELECT '✓ notifications table updated' AS status;

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

SELECT '✓ Script 03 completed successfully' AS status;

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- appid is optional (NULL allowed) - most notifications won't be app-specific
-- Use case: "PC-DMIS upgrade scheduled" notification linked to PC-DMIS app
-- Next: Run VERIFY_PHASE4_MIGRATION.sql
-- Rollback: Run ROLLBACK_03_notifications_appid.sql
-- =====================================================
