-- =====================================================
-- VERIFY PHASE 4 MIGRATION: Application Versions
-- =====================================================
-- Date: 2025-11-25
-- Purpose: Verify all Phase 4 schema changes are in place
-- Run this after all migration scripts complete
-- =====================================================

USE shopdb;

SELECT '=================================================' AS '';
SELECT 'PHASE 4 MIGRATION VERIFICATION' AS '';
SELECT '=================================================' AS '';

-- =====================================================
-- CHECK 1: appversions table exists
-- =====================================================
SELECT '--- CHECK 1: appversions table ---' AS '';

SELECT
    CASE WHEN COUNT(*) > 0 THEN '✓ PASS' ELSE '✗ FAIL' END AS appversions_table_exists
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'shopdb' AND TABLE_NAME = 'appversions';

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

-- =====================================================
-- CHECK 2: installedapps.appversionid column exists
-- =====================================================
SELECT '--- CHECK 2: installedapps.appversionid column ---' AS '';

SELECT
    CASE WHEN COUNT(*) > 0 THEN '✓ PASS' ELSE '✗ FAIL' END AS appversionid_column_exists
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'shopdb'
AND TABLE_NAME = 'installedapps'
AND COLUMN_NAME = 'appversionid';

-- Check FK
SELECT
    CASE WHEN COUNT(*) > 0 THEN '✓ PASS' ELSE '✗ FAIL' END AS installedapps_fk_exists
FROM information_schema.TABLE_CONSTRAINTS
WHERE TABLE_SCHEMA = 'shopdb'
AND TABLE_NAME = 'installedapps'
AND CONSTRAINT_NAME = 'fk_installedapps_appversionid';

-- =====================================================
-- CHECK 3: notifications.appid column exists
-- =====================================================
SELECT '--- CHECK 3: notifications.appid column ---' AS '';

SELECT
    CASE WHEN COUNT(*) > 0 THEN '✓ PASS' ELSE '✗ FAIL' END AS notifications_appid_column_exists
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'shopdb'
AND TABLE_NAME = 'notifications'
AND COLUMN_NAME = 'appid';

-- Check FK
SELECT
    CASE WHEN COUNT(*) > 0 THEN '✓ PASS' ELSE '✗ FAIL' END AS notifications_appid_fk_exists
FROM information_schema.TABLE_CONSTRAINTS
WHERE TABLE_SCHEMA = 'shopdb'
AND TABLE_NAME = 'notifications'
AND CONSTRAINT_NAME = 'fk_notifications_appid';

-- =====================================================
-- CHECK 4: Foreign key relationships
-- =====================================================
SELECT '--- CHECK 4: Foreign key relationships ---' AS '';

SELECT
    CONSTRAINT_NAME,
    TABLE_NAME,
    REFERENCED_TABLE_NAME
FROM information_schema.REFERENTIAL_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = 'shopdb'
AND (
    CONSTRAINT_NAME LIKE '%appversion%'
    OR (TABLE_NAME = 'notifications' AND CONSTRAINT_NAME LIKE '%appid%')
);

-- =====================================================
-- SUMMARY: Current record counts
-- =====================================================
SELECT '--- SUMMARY: Record counts ---' AS '';

SELECT
    'applications' AS table_name,
    COUNT(*) AS record_count
FROM applications
UNION ALL
SELECT
    'appversions' AS table_name,
    COUNT(*) AS record_count
FROM appversions
UNION ALL
SELECT
    'installedapps' AS table_name,
    COUNT(*) AS record_count
FROM installedapps
UNION ALL
SELECT
    'installedapps (with version)' AS table_name,
    COUNT(*) AS record_count
FROM installedapps WHERE appversionid IS NOT NULL
UNION ALL
SELECT
    'notifications' AS table_name,
    COUNT(*) AS record_count
FROM notifications
UNION ALL
SELECT
    'notifications (app-linked)' AS table_name,
    COUNT(*) AS record_count
FROM notifications WHERE appid IS NOT NULL;

SELECT '=================================================' AS '';
SELECT 'PHASE 4 VERIFICATION COMPLETE' AS '';
SELECT '=================================================' AS '';
