-- Migration fuer Mehrfach-Zeitfenster in der Impfwarteliste.
-- Idempotent: kann mehrfach ausgefuehrt werden.
-- Bestehende Tabellen werden nicht neu aufgebaut, sondern nur erweitert.

CREATE TABLE IF NOT EXISTS `impf_workflow_meta` (
    `meta_key` VARCHAR(100) NOT NULL,
    `meta_value` VARCHAR(255) NOT NULL DEFAULT '',
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`meta_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP PROCEDURE IF EXISTS `migrate_warteliste_multi_zeitfenster_20260322`;
DELIMITER $$
CREATE PROCEDURE `migrate_warteliste_multi_zeitfenster_20260322`()
BEGIN
    DECLARE v_warteliste_exists INT DEFAULT 0;
    DECLARE v_zeitraum_id_exists INT DEFAULT 0;
    DECLARE v_warteliste_index_exists INT DEFAULT 0;
    DECLARE v_warteliste_zeitraum_exists INT DEFAULT 0;
    DECLARE v_warteliste_zeitraum_index_exists INT DEFAULT 0;

    SELECT COUNT(*)
      INTO v_warteliste_exists
      FROM information_schema.tables
     WHERE table_schema = DATABASE()
       AND table_name = 'warteliste';

    IF v_warteliste_exists = 0 THEN
        SIGNAL SQLSTATE '45000'
            SET MESSAGE_TEXT = 'Tabelle warteliste wurde nicht gefunden.';
    END IF;

    SELECT COUNT(*)
      INTO v_zeitraum_id_exists
      FROM information_schema.columns
     WHERE table_schema = DATABASE()
       AND table_name = 'warteliste'
       AND column_name = 'zeitraum_id';

    IF v_zeitraum_id_exists = 0 THEN
        ALTER TABLE `warteliste`
            ADD COLUMN `zeitraum_id` INT NULL AFTER `impfenzeitraum`;
    END IF;

    SELECT COUNT(*)
      INTO v_warteliste_index_exists
      FROM information_schema.statistics
     WHERE table_schema = DATABASE()
       AND table_name = 'warteliste'
       AND index_name = 'idx_warteliste_zeitraum';

    IF v_warteliste_index_exists = 0 THEN
        ALTER TABLE `warteliste`
            ADD INDEX `idx_warteliste_zeitraum` (`zeitraum_id`);
    END IF;

    SELECT COUNT(*)
      INTO v_warteliste_zeitraum_exists
      FROM information_schema.tables
     WHERE table_schema = DATABASE()
       AND table_name = 'warteliste_zeitraum';

    IF v_warteliste_zeitraum_exists = 0 THEN
        CREATE TABLE `warteliste_zeitraum` (
            `warteid` INT NOT NULL,
            `zeitraum_id` INT NOT NULL,
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (`warteid`, `zeitraum_id`),
            INDEX `idx_warteliste_zeitraum_zeitraum` (`zeitraum_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
        SET v_warteliste_zeitraum_exists = 1;
    END IF;

    SELECT COUNT(*)
      INTO v_warteliste_zeitraum_index_exists
      FROM information_schema.statistics
     WHERE table_schema = DATABASE()
       AND table_name = 'warteliste_zeitraum'
       AND index_name = 'idx_warteliste_zeitraum_zeitraum';

    IF v_warteliste_zeitraum_index_exists = 0 THEN
        ALTER TABLE `warteliste_zeitraum`
            ADD INDEX `idx_warteliste_zeitraum_zeitraum` (`zeitraum_id`);
    END IF;

    INSERT IGNORE INTO `warteliste_zeitraum` (`warteid`, `zeitraum_id`)
    SELECT `warteid`, `zeitraum_id`
      FROM `warteliste`
     WHERE `zeitraum_id` IS NOT NULL
       AND `zeitraum_id` > 0;

    INSERT INTO `impf_workflow_meta` (`meta_key`, `meta_value`)
    VALUES ('legacy_warteliste_zeitraeume_migrated', '1') AS `incoming`
    ON DUPLICATE KEY UPDATE `meta_value` = `incoming`.`meta_value`;
END $$
DELIMITER ;

CALL `migrate_warteliste_multi_zeitfenster_20260322`();
DROP PROCEDURE IF EXISTS `migrate_warteliste_multi_zeitfenster_20260322`;
