-- Bringt den SQL-Dump auf den vom aktuellen Quellcode erwarteten Stand.
-- Die Migration ist idempotent und kann mehrfach ausgefuehrt werden.

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;

CREATE TABLE IF NOT EXISTS `impfstoff_workflow` (
    `impfstoff_id` INT NOT NULL,
    `dosen_pro_flasche` INT NOT NULL,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`impfstoff_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `impfstoff_wochenplan` (
    `plan_id` INT NOT NULL AUTO_INCREMENT,
    `impfstoff_id` INT NOT NULL,
    `wochentag` TINYINT NOT NULL,
    `start` TIME NOT NULL,
    `ende` TIME NOT NULL,
    `impfortid` INT NOT NULL,
    `aktiv` TINYINT NOT NULL DEFAULT 1,
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`plan_id`),
    INDEX `idx_impfstoff_wochenplan_impfstoff` (`impfstoff_id`),
    INDEX `idx_impfstoff_wochenplan_wochentag` (`wochentag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `impf_zeitraum` (
    `zeitraum_id` INT NOT NULL AUTO_INCREMENT,
    `bezeichnung` VARCHAR(120) NOT NULL DEFAULT '',
    `wochentag` TINYINT NOT NULL,
    `start` TIME NOT NULL,
    `ende` TIME NOT NULL,
    `impfortid` INT NOT NULL,
    `aktiv` TINYINT NOT NULL DEFAULT 1,
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`zeitraum_id`),
    INDEX `idx_impf_zeitraum_wochentag` (`wochentag`),
    INDEX `idx_impf_zeitraum_impfort` (`impfortid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `impf_zeitraum_impfstoff` (
    `zeitraum_id` INT NOT NULL,
    `impfstoff_id` INT NOT NULL,
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`zeitraum_id`, `impfstoff_id`),
    INDEX `idx_impf_zeitraum_impfstoff_impfstoff` (`impfstoff_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `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;

DROP PROCEDURE IF EXISTS `migrate_praxis_schema_20260320`;
DELIMITER $$
CREATE PROCEDURE `migrate_praxis_schema_20260320`()
BEGIN
    DECLARE v_impf_zeitraum_exists INT DEFAULT 0;
    DECLARE v_bezeichnung_exists INT DEFAULT 0;
    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;
    DECLARE v_legacy_plan_exists INT DEFAULT 0;

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

    IF v_impf_zeitraum_exists > 0 THEN
        SELECT COUNT(*)
          INTO v_bezeichnung_exists
          FROM information_schema.columns
         WHERE table_schema = DATABASE()
           AND table_name = 'impf_zeitraum'
           AND column_name = 'bezeichnung';

        IF v_bezeichnung_exists = 0 THEN
            ALTER TABLE `impf_zeitraum`
                ADD COLUMN `bezeichnung` VARCHAR(120) NOT NULL DEFAULT '' AFTER `zeitraum_id`;
        END IF;
    END IF;

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

    IF v_warteliste_exists > 0 THEN
        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;
    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;
    ELSE
        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;
    END IF;

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

    IF v_legacy_plan_exists > 0 AND v_impf_zeitraum_exists > 0 THEN
        INSERT INTO `impf_zeitraum` (`bezeichnung`, `wochentag`, `start`, `ende`, `impfortid`, `aktiv`)
        SELECT DISTINCT
               '',
               wp.`wochentag`,
               wp.`start`,
               wp.`ende`,
               wp.`impfortid`,
               wp.`aktiv`
          FROM `impfstoff_wochenplan` wp
          LEFT JOIN `impf_zeitraum` z
            ON z.`wochentag` = wp.`wochentag`
           AND z.`start` = wp.`start`
           AND z.`ende` = wp.`ende`
           AND z.`impfortid` = wp.`impfortid`
           AND z.`aktiv` = wp.`aktiv`
         WHERE z.`zeitraum_id` IS NULL;

        INSERT INTO `impf_zeitraum_impfstoff` (`zeitraum_id`, `impfstoff_id`)
        SELECT DISTINCT
               z.`zeitraum_id`,
               wp.`impfstoff_id`
          FROM `impfstoff_wochenplan` wp
          INNER JOIN `impf_zeitraum` z
             ON z.`wochentag` = wp.`wochentag`
            AND z.`start` = wp.`start`
            AND z.`ende` = wp.`ende`
            AND z.`impfortid` = wp.`impfortid`
            AND z.`aktiv` = wp.`aktiv`
          LEFT JOIN `impf_zeitraum_impfstoff` m
            ON m.`zeitraum_id` = z.`zeitraum_id`
           AND m.`impfstoff_id` = wp.`impfstoff_id`
         WHERE wp.`impfstoff_id` > 0
           AND m.`zeitraum_id` IS NULL;

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

    IF v_warteliste_exists > 0 AND v_warteliste_zeitraum_exists > 0 THEN
        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 IF;
END $$
DELIMITER ;

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