-- +goose Up
-- Apply legacy medicine staging data into:
-- - medicine_groups
-- - motor_reactions
-- - medicines
--
-- Expected flow before this migration:
-- 1) Run staging migration that creates *_old_import tables.
-- 2) Load legacy dump data into:
--    - medicine_group_old_import
--    - motor_reaction_old_import
--    - medicine_old_import

START TRANSACTION;

INSERT INTO medicine_group_old_id_map (old_id, new_id)
SELECT
  s.id AS old_id,
  UNHEX(REPLACE(
    LOWER(CONCAT(
      LPAD(HEX(FLOOR(CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED) / 65536)), 8, '0'),
      '-',
      LPAD(HEX(MOD(CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED), 65536)), 4, '0'),
      '-',
      '7', LPAD(HEX(FLOOR(RAND() * 4096)), 3, '0'),
      '-',
      LPAD(HEX(FLOOR(RAND() * 16384) + 32768), 4, '0'),
      '-',
      LPAD(HEX(FLOOR(RAND() * 281474976710656)), 12, '0')
    )),
    '-', ''
  )) AS new_id
FROM medicine_group_old_import s
LEFT JOIN medicine_group_old_id_map m ON m.old_id = s.id
WHERE m.old_id IS NULL;

INSERT INTO motor_reaction_old_id_map (old_id, new_id)
SELECT
  s.id AS old_id,
  UNHEX(REPLACE(
    LOWER(CONCAT(
      LPAD(HEX(FLOOR(CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED) / 65536)), 8, '0'),
      '-',
      LPAD(HEX(MOD(CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED), 65536)), 4, '0'),
      '-',
      '7', LPAD(HEX(FLOOR(RAND() * 4096)), 3, '0'),
      '-',
      LPAD(HEX(FLOOR(RAND() * 16384) + 32768), 4, '0'),
      '-',
      LPAD(HEX(FLOOR(RAND() * 281474976710656)), 12, '0')
    )),
    '-', ''
  )) AS new_id
FROM motor_reaction_old_import s
LEFT JOIN motor_reaction_old_id_map m ON m.old_id = s.id
WHERE m.old_id IS NULL;

INSERT INTO medicine_old_id_map (old_id, new_id)
SELECT
  s.id AS old_id,
  UNHEX(REPLACE(
    LOWER(CONCAT(
      LPAD(HEX(FLOOR(CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED) / 65536)), 8, '0'),
      '-',
      LPAD(HEX(MOD(CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED), 65536)), 4, '0'),
      '-',
      '7', LPAD(HEX(FLOOR(RAND() * 4096)), 3, '0'),
      '-',
      LPAD(HEX(FLOOR(RAND() * 16384) + 32768), 4, '0'),
      '-',
      LPAD(HEX(FLOOR(RAND() * 281474976710656)), 12, '0')
    )),
    '-', ''
  )) AS new_id
FROM medicine_old_import s
LEFT JOIN medicine_old_id_map m ON m.old_id = s.id
WHERE m.old_id IS NULL;

INSERT INTO medicine_groups (
  id,
  name,
  is_active,
  created_at,
  updated_at
)
SELECT
  m.new_id,
  LEFT(COALESCE(NULLIF(TRIM(s.name), ''), CONCAT('Medicine Group ', s.id)), 255) AS name,
  1 AS is_active,
  UTC_TIMESTAMP() AS created_at,
  UTC_TIMESTAMP() AS updated_at
FROM medicine_group_old_import s
JOIN medicine_group_old_id_map m ON m.old_id = s.id
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  is_active = 1,
  updated_at = UTC_TIMESTAMP();

INSERT INTO motor_reactions (
  id,
  name,
  score,
  is_active,
  created_at,
  updated_at
)
SELECT
  m.new_id,
  LEFT(COALESCE(NULLIF(TRIM(s.name), ''), CONCAT('Motor Reaction ', s.id)), 255) AS name,
  s.ball AS score,
  1 AS is_active,
  UTC_TIMESTAMP() AS created_at,
  UTC_TIMESTAMP() AS updated_at
FROM motor_reaction_old_import s
JOIN motor_reaction_old_id_map m ON m.old_id = s.id
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  score = VALUES(score),
  is_active = 1,
  updated_at = UTC_TIMESTAMP();

INSERT INTO medicines (
  id,
  name,
  dose,
  weight,
  `column`,
  medicine_group_id,
  motor_reaction_id,
  is_active,
  created_at,
  updated_at
)
SELECT
  med_map.new_id,
  LEFT(COALESCE(NULLIF(TRIM(s.name), ''), CONCAT('Medicine ', s.id)), 128) AS name,
  LEFT(TRIM(COALESCE(s.dose, '')), 128) AS dose,
  s.weight AS weight,
  COALESCE(s.`col`, 0) AS `column`,
  grp_map.new_id AS medicine_group_id,
  react_map.new_id AS motor_reaction_id,
  1 AS is_active,
  UTC_TIMESTAMP() AS created_at,
  UTC_TIMESTAMP() AS updated_at
FROM medicine_old_import s
JOIN medicine_old_id_map med_map ON med_map.old_id = s.id
LEFT JOIN medicine_group_old_id_map grp_map ON grp_map.old_id = s.cmedikamente_group_id
LEFT JOIN motor_reaction_old_id_map react_map ON react_map.old_id = s.motor_reaction_old_id
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  dose = VALUES(dose),
  weight = VALUES(weight),
  `column` = VALUES(`column`),
  medicine_group_id = VALUES(medicine_group_id),
  motor_reaction_id = VALUES(motor_reaction_id),
  is_active = 1,
  updated_at = UTC_TIMESTAMP();

COMMIT;

-- +goose Down
-- Data migration is intentionally non-reversible.
SELECT 1;
