-- +goose Up
ALTER TABLE after_flight_inspections
  ADD COLUMN oil_engine_nr1_checked_tmp VARCHAR(255) NULL DEFAULT NULL,
  ADD COLUMN oil_engine_nr2_checked_tmp VARCHAR(255) NULL DEFAULT NULL,
  ADD COLUMN hydraulic_lh_checked_tmp VARCHAR(255) NULL DEFAULT NULL,
  ADD COLUMN hydraulic_rh_checked_tmp VARCHAR(255) NULL DEFAULT NULL,
  ADD COLUMN oil_transmission_mgb_checked_tmp VARCHAR(255) NULL DEFAULT NULL,
  ADD COLUMN oil_transmission_igb_checked_tmp VARCHAR(255) NULL DEFAULT NULL,
  ADD COLUMN oil_transmission_tgb_checked_tmp VARCHAR(255) NULL DEFAULT NULL;

UPDATE after_flight_inspections
SET oil_engine_nr1_checked_tmp = CASE
  WHEN oil_engine_nr1_checked IS NULL THEN NULL
  WHEN oil_engine_nr1_checked IN (1, TRUE) THEN 'true'
  ELSE 'false'
END,
oil_engine_nr2_checked_tmp = CASE
  WHEN oil_engine_nr2_checked IS NULL THEN NULL
  WHEN oil_engine_nr2_checked IN (1, TRUE) THEN 'true'
  ELSE 'false'
END,
hydraulic_lh_checked_tmp = CASE
  WHEN hydraulic_lh_checked IS NULL THEN NULL
  WHEN hydraulic_lh_checked IN (1, TRUE) THEN 'true'
  ELSE 'false'
END,
hydraulic_rh_checked_tmp = CASE
  WHEN hydraulic_rh_checked IS NULL THEN NULL
  WHEN hydraulic_rh_checked IN (1, TRUE) THEN 'true'
  ELSE 'false'
END,
oil_transmission_mgb_checked_tmp = CASE
  WHEN oil_transmission_mgb_checked IS NULL THEN NULL
  WHEN oil_transmission_mgb_checked IN (1, TRUE) THEN 'true'
  ELSE 'false'
END,
oil_transmission_igb_checked_tmp = CASE
  WHEN oil_transmission_igb_checked IS NULL THEN NULL
  WHEN oil_transmission_igb_checked IN (1, TRUE) THEN 'true'
  ELSE 'false'
END,
oil_transmission_tgb_checked_tmp = CASE
  WHEN oil_transmission_tgb_checked IS NULL THEN NULL
  WHEN oil_transmission_tgb_checked IN (1, TRUE) THEN 'true'
  ELSE 'false'
END;

ALTER TABLE after_flight_inspections
  DROP COLUMN oil_engine_nr1_checked,
  DROP COLUMN oil_engine_nr2_checked,
  DROP COLUMN hydraulic_lh_checked,
  DROP COLUMN hydraulic_rh_checked,
  DROP COLUMN oil_transmission_mgb_checked,
  DROP COLUMN oil_transmission_igb_checked,
  DROP COLUMN oil_transmission_tgb_checked;

ALTER TABLE after_flight_inspections
  CHANGE COLUMN oil_engine_nr1_checked_tmp oil_engine_nr1_checked VARCHAR(255) NULL DEFAULT NULL,
  CHANGE COLUMN oil_engine_nr2_checked_tmp oil_engine_nr2_checked VARCHAR(255) NULL DEFAULT NULL,
  CHANGE COLUMN hydraulic_lh_checked_tmp hydraulic_lh_checked VARCHAR(255) NULL DEFAULT NULL,
  CHANGE COLUMN hydraulic_rh_checked_tmp hydraulic_rh_checked VARCHAR(255) NULL DEFAULT NULL,
  CHANGE COLUMN oil_transmission_mgb_checked_tmp oil_transmission_mgb_checked VARCHAR(255) NULL DEFAULT NULL,
  CHANGE COLUMN oil_transmission_igb_checked_tmp oil_transmission_igb_checked VARCHAR(255) NULL DEFAULT NULL,
  CHANGE COLUMN oil_transmission_tgb_checked_tmp oil_transmission_tgb_checked VARCHAR(255) NULL DEFAULT NULL;

-- +goose Down
ALTER TABLE after_flight_inspections
  ADD COLUMN oil_engine_nr1_checked_tmp TINYINT(1) NULL,
  ADD COLUMN oil_engine_nr2_checked_tmp TINYINT(1) NULL,
  ADD COLUMN hydraulic_lh_checked_tmp TINYINT(1) NULL,
  ADD COLUMN hydraulic_rh_checked_tmp TINYINT(1) NULL,
  ADD COLUMN oil_transmission_mgb_checked_tmp TINYINT(1) NULL,
  ADD COLUMN oil_transmission_igb_checked_tmp TINYINT(1) NULL,
  ADD COLUMN oil_transmission_tgb_checked_tmp TINYINT(1) NULL;

UPDATE after_flight_inspections
SET oil_engine_nr1_checked_tmp = CASE
  WHEN oil_engine_nr1_checked IS NULL THEN NULL
  WHEN LOWER(TRIM(oil_engine_nr1_checked)) IN ('true', 'checked', 'yes', '1') THEN 1
  ELSE 0
END,
oil_engine_nr2_checked_tmp = CASE
  WHEN oil_engine_nr2_checked IS NULL THEN NULL
  WHEN LOWER(TRIM(oil_engine_nr2_checked)) IN ('true', 'checked', 'yes', '1') THEN 1
  ELSE 0
END,
hydraulic_lh_checked_tmp = CASE
  WHEN hydraulic_lh_checked IS NULL THEN NULL
  WHEN LOWER(TRIM(hydraulic_lh_checked)) IN ('true', 'checked', 'yes', '1') THEN 1
  ELSE 0
END,
hydraulic_rh_checked_tmp = CASE
  WHEN hydraulic_rh_checked IS NULL THEN NULL
  WHEN LOWER(TRIM(hydraulic_rh_checked)) IN ('true', 'checked', 'yes', '1') THEN 1
  ELSE 0
END,
oil_transmission_mgb_checked_tmp = CASE
  WHEN oil_transmission_mgb_checked IS NULL THEN NULL
  WHEN LOWER(TRIM(oil_transmission_mgb_checked)) IN ('true', 'checked', 'yes', '1') THEN 1
  ELSE 0
END,
oil_transmission_igb_checked_tmp = CASE
  WHEN oil_transmission_igb_checked IS NULL THEN NULL
  WHEN LOWER(TRIM(oil_transmission_igb_checked)) IN ('true', 'checked', 'yes', '1') THEN 1
  ELSE 0
END,
oil_transmission_tgb_checked_tmp = CASE
  WHEN oil_transmission_tgb_checked IS NULL THEN NULL
  WHEN LOWER(TRIM(oil_transmission_tgb_checked)) IN ('true', 'checked', 'yes', '1') THEN 1
  ELSE 0
END;

ALTER TABLE after_flight_inspections
  DROP COLUMN oil_engine_nr1_checked,
  DROP COLUMN oil_engine_nr2_checked,
  DROP COLUMN hydraulic_lh_checked,
  DROP COLUMN hydraulic_rh_checked,
  DROP COLUMN oil_transmission_mgb_checked,
  DROP COLUMN oil_transmission_igb_checked,
  DROP COLUMN oil_transmission_tgb_checked;

ALTER TABLE after_flight_inspections
  CHANGE COLUMN oil_engine_nr1_checked_tmp oil_engine_nr1_checked TINYINT(1) NULL,
  CHANGE COLUMN oil_engine_nr2_checked_tmp oil_engine_nr2_checked TINYINT(1) NULL,
  CHANGE COLUMN hydraulic_lh_checked_tmp hydraulic_lh_checked TINYINT(1) NULL,
  CHANGE COLUMN hydraulic_rh_checked_tmp hydraulic_rh_checked TINYINT(1) NULL,
  CHANGE COLUMN oil_transmission_mgb_checked_tmp oil_transmission_mgb_checked TINYINT(1) NULL,
  CHANGE COLUMN oil_transmission_igb_checked_tmp oil_transmission_igb_checked TINYINT(1) NULL,
  CHANGE COLUMN oil_transmission_tgb_checked_tmp oil_transmission_tgb_checked TINYINT(1) NULL;
