-- +goose Up
-- Prepare staging tables for legacy `user` dump import.
-- Source table in old DB: `user` (not `users`).

CREATE TABLE IF NOT EXISTS user_old_import (
  id int(11) NOT NULL,
  state tinyint(1) DEFAULT 0,
  role int(2) NOT NULL,
  username varchar(30) DEFAULT NULL,
  password varchar(150) DEFAULT NULL,
  fullname varchar(50) DEFAULT NULL,
  shortName varchar(30) DEFAULT NULL,
  flightNumber varchar(20) DEFAULT NULL,
  license varchar(20) DEFAULT NULL,
  techLicense varchar(20) DEFAULT NULL,
  airport_pilot varchar(15) DEFAULT NULL,
  responsible_pilot varchar(15) DEFAULT NULL,
  second_pilot varchar(15) DEFAULT NULL,
  double_tax_pilot varchar(15) DEFAULT NULL,
  flight_instructor_pilot varchar(15) DEFAULT NULL,
  night_pilot varchar(15) DEFAULT NULL,
  fir_pilot varchar(15) DEFAULT NULL,
  start_date varchar(15) DEFAULT NULL,
  date_active varchar(15) DEFAULT NULL,
  address varchar(255) DEFAULT NULL,
  zipCode varchar(15) DEFAULT NULL,
  street varchar(255) DEFAULT NULL,
  phone varchar(50) DEFAULT NULL,
  mobile varchar(50) DEFAULT NULL,
  email varchar(70) DEFAULT NULL,
  photo varchar(50) DEFAULT NULL,
  token varchar(255) DEFAULT NULL,
  hash varchar(255) DEFAULT NULL,
  regDate timestamp NULL DEFAULT NULL,
  lastVisit timestamp NULL DEFAULT NULL,
  reportDate datetime DEFAULT NULL,
  deletedAt varchar(32) DEFAULT NULL,
  isDeleted tinyint(1) DEFAULT 0,
  visible tinyint(1) DEFAULT 1,
  lang varchar(5) DEFAULT 'de',
  bases varchar(255) DEFAULT NULL,
  legacy_url varchar(255) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

CREATE TABLE IF NOT EXISTS user_old_id_map (
  old_id int(11) NOT NULL,
  new_id binary(16) NOT NULL,
  PRIMARY KEY (old_id),
  UNIQUE KEY uq_user_old_id_map_new_id (new_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

CREATE TABLE IF NOT EXISTS legacy_user_role_map (
  legacy_role int(2) NOT NULL,
  role_code varchar(64) NOT NULL,
  staff_role_label varchar(120) DEFAULT NULL,
  pilot_category varchar(16) DEFAULT NULL,
  is_chief_pilot tinyint(1) NOT NULL DEFAULT 0,
  is_chief_physician tinyint(1) NOT NULL DEFAULT 0,
  is_chief_physician_in_charge tinyint(1) NOT NULL DEFAULT 0,
  is_chief_technician tinyint(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (legacy_role)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

INSERT INTO legacy_user_role_map (
  legacy_role,
  role_code,
  staff_role_label,
  pilot_category,
  is_chief_pilot,
  is_chief_physician,
  is_chief_physician_in_charge,
  is_chief_technician
)
VALUES
  (1,  'pilot',            NULL,             'regular',   0, 0, 0, 0),
  (2,  'pilot',            NULL,             'regular',   1, 0, 0, 0),
  (3,  'doctor',           NULL,             NULL,        0, 0, 0, 0),
  (4,  'doctor',           NULL,             NULL,        0, 1, 0, 0),
  (5,  'air_rescuer',      NULL,             NULL,        0, 0, 0, 0),
  (6,  'air_rescuer',      NULL,             NULL,        0, 0, 1, 0),
  (7,  'admin',            'administrator',  NULL,        0, 0, 0, 0),
  (8,  'staff',            'billing',        NULL,        0, 0, 0, 0),
  (9,  'technician',       NULL,             NULL,        0, 0, 0, 0),
  (10, 'technician',       NULL,             NULL,        0, 0, 0, 1),
  (11, 'flight_assistant', NULL,             NULL,        0, 0, 0, 0),
  (12, 'staff',            'manager',        NULL,        0, 0, 0, 0),
  (13, 'pilot',            NULL,             'freelance', 0, 0, 0, 0),
  (14, 'pilot',            NULL,             'dry_lease', 0, 0, 0, 0),
  (15, 'staff',            'control_center', NULL,        0, 0, 0, 0)
ON DUPLICATE KEY UPDATE
  role_code = VALUES(role_code),
  staff_role_label = VALUES(staff_role_label),
  pilot_category = VALUES(pilot_category),
  is_chief_pilot = VALUES(is_chief_pilot),
  is_chief_physician = VALUES(is_chief_physician),
  is_chief_physician_in_charge = VALUES(is_chief_physician_in_charge),
  is_chief_technician = VALUES(is_chief_technician);

-- +goose Down
DROP TABLE IF EXISTS legacy_user_role_map;
DROP TABLE IF EXISTS user_old_id_map;
DROP TABLE IF EXISTS user_old_import;
