-- Prueft die fuer den aktuellen Impf-Workflow relevanten Tabellen, Spalten,
-- Indizes und den Legacy-Migrationsstatus.
-- Das Skript veraendert keine Daten.

SELECT 'database' AS check_type, DATABASE() AS object_name, 'INFO' AS status;

SELECT
    'table' AS check_type,
    t.required_name AS object_name,
    CASE
        WHEN it.table_name IS NOT NULL THEN 'OK'
        ELSE 'MISSING'
    END AS status
FROM (
    SELECT 'impf_workflow_meta' AS required_name
    UNION ALL SELECT 'impfstoff_workflow'
    UNION ALL SELECT 'impfstoff_wochenplan'
    UNION ALL SELECT 'impf_zeitraum'
    UNION ALL SELECT 'impf_zeitraum_impfstoff'
    UNION ALL SELECT 'warteliste'
) t
LEFT JOIN information_schema.tables it
    ON it.table_schema = DATABASE()
   AND it.table_name = t.required_name
ORDER BY t.required_name;

SELECT
    'column' AS check_type,
    CONCAT(c.table_name, '.', c.column_name) AS object_name,
    CASE
        WHEN ic.column_name IS NOT NULL THEN 'OK'
        ELSE 'MISSING'
    END AS status
FROM (
    SELECT 'impf_workflow_meta' AS table_name, 'meta_key' AS column_name
    UNION ALL SELECT 'impf_workflow_meta', 'meta_value'
    UNION ALL SELECT 'impf_workflow_meta', 'updated_at'
    UNION ALL SELECT 'impfstoff_workflow', 'impfstoff_id'
    UNION ALL SELECT 'impfstoff_workflow', 'dosen_pro_flasche'
    UNION ALL SELECT 'impfstoff_wochenplan', 'plan_id'
    UNION ALL SELECT 'impfstoff_wochenplan', 'impfstoff_id'
    UNION ALL SELECT 'impfstoff_wochenplan', 'wochentag'
    UNION ALL SELECT 'impfstoff_wochenplan', 'start'
    UNION ALL SELECT 'impfstoff_wochenplan', 'ende'
    UNION ALL SELECT 'impfstoff_wochenplan', 'impfortid'
    UNION ALL SELECT 'impf_zeitraum', 'zeitraum_id'
    UNION ALL SELECT 'impf_zeitraum', 'bezeichnung'
    UNION ALL SELECT 'impf_zeitraum', 'wochentag'
    UNION ALL SELECT 'impf_zeitraum', 'start'
    UNION ALL SELECT 'impf_zeitraum', 'ende'
    UNION ALL SELECT 'impf_zeitraum', 'impfortid'
    UNION ALL SELECT 'impf_zeitraum_impfstoff', 'zeitraum_id'
    UNION ALL SELECT 'impf_zeitraum_impfstoff', 'impfstoff_id'
    UNION ALL SELECT 'warteliste', 'warteid'
    UNION ALL SELECT 'warteliste', 'userid'
    UNION ALL SELECT 'warteliste', 'impfenzeitraum'
    UNION ALL SELECT 'warteliste', 'zeitraum_id'
) c
LEFT JOIN information_schema.columns ic
    ON ic.table_schema = DATABASE()
   AND ic.table_name = c.table_name
   AND ic.column_name = c.column_name
ORDER BY c.table_name, c.column_name;

SELECT
    'index' AS check_type,
    CONCAT(i.table_name, '.', i.index_name) AS object_name,
    CASE
        WHEN isx.index_name IS NOT NULL THEN 'OK'
        ELSE 'MISSING'
    END AS status
FROM (
    SELECT 'warteliste' AS table_name, 'idx_warteliste_zeitraum' AS index_name
    UNION ALL SELECT 'impfstoff_wochenplan', 'idx_impfstoff_wochenplan_impfstoff'
    UNION ALL SELECT 'impfstoff_wochenplan', 'idx_impfstoff_wochenplan_wochentag'
    UNION ALL SELECT 'impf_zeitraum', 'idx_impf_zeitraum_wochentag'
    UNION ALL SELECT 'impf_zeitraum', 'idx_impf_zeitraum_impfort'
    UNION ALL SELECT 'impf_zeitraum_impfstoff', 'idx_impf_zeitraum_impfstoff_impfstoff'
) i
LEFT JOIN information_schema.statistics isx
    ON isx.table_schema = DATABASE()
   AND isx.table_name = i.table_name
   AND isx.index_name = i.index_name
GROUP BY i.table_name, i.index_name, isx.index_name
ORDER BY i.table_name, i.index_name;

SELECT
    'meta' AS check_type,
    'impf_workflow_meta.legacy_wochenplan_migrated' AS object_name,
    CASE
        WHEN EXISTS (
            SELECT 1
            FROM information_schema.tables
            WHERE table_schema = DATABASE()
              AND table_name = 'impf_workflow_meta'
        ) THEN COALESCE((
            SELECT CONCAT('VALUE=', meta_value)
            FROM impf_workflow_meta
            WHERE meta_key = 'legacy_wochenplan_migrated'
            LIMIT 1
        ), 'MISSING')
        ELSE 'TABLE_MISSING'
    END AS status;

SELECT
    'data' AS check_type,
    'impfstoff_wochenplan rows' AS object_name,
    CASE
        WHEN EXISTS (
            SELECT 1
            FROM information_schema.tables
            WHERE table_schema = DATABASE()
              AND table_name = 'impfstoff_wochenplan'
        ) THEN CAST((SELECT COUNT(*) FROM impfstoff_wochenplan) AS CHAR)
        ELSE 'TABLE_MISSING'
    END AS status
UNION ALL
SELECT
    'data' AS check_type,
    'impf_zeitraum rows' AS object_name,
    CASE
        WHEN EXISTS (
            SELECT 1
            FROM information_schema.tables
            WHERE table_schema = DATABASE()
              AND table_name = 'impf_zeitraum'
        ) THEN CAST((SELECT COUNT(*) FROM impf_zeitraum) AS CHAR)
        ELSE 'TABLE_MISSING'
    END AS status
UNION ALL
SELECT
    'data' AS check_type,
    'impf_zeitraum_impfstoff rows' AS object_name,
    CASE
        WHEN EXISTS (
            SELECT 1
            FROM information_schema.tables
            WHERE table_schema = DATABASE()
              AND table_name = 'impf_zeitraum_impfstoff'
        ) THEN CAST((SELECT COUNT(*) FROM impf_zeitraum_impfstoff) AS CHAR)
        ELSE 'TABLE_MISSING'
    END AS status
UNION ALL
SELECT
    'data' AS check_type,
    'warteliste with zeitraum_id' AS object_name,
    CASE
        WHEN EXISTS (
            SELECT 1
            FROM information_schema.columns
            WHERE table_schema = DATABASE()
              AND table_name = 'warteliste'
              AND column_name = 'zeitraum_id'
        ) THEN CAST((SELECT COUNT(*) FROM warteliste WHERE zeitraum_id IS NOT NULL) AS CHAR)
        ELSE 'COLUMN_MISSING'
    END AS status;
