-- +goose Up
CREATE TABLE IF NOT EXISTS base_contact_roles (
  id BINARY(16) NOT NULL,
  base_id BINARY(16) NOT NULL,
  contact_id BINARY(16) NOT NULL,
  role_code VARCHAR(64) NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  created_by BINARY(16) NULL,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  updated_by BINARY(16) NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_base_contact_roles_base_contact_role (base_id, contact_id, role_code),
  KEY idx_base_contact_roles_base_id (base_id),
  KEY idx_base_contact_roles_contact_id (contact_id),
  KEY idx_base_contact_roles_role_code (role_code),
  CONSTRAINT fk_base_contact_roles_base FOREIGN KEY (base_id) REFERENCES bases(id) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT fk_base_contact_roles_contact FOREIGN KEY (contact_id) REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT chk_base_contact_roles_role_code CHECK (role_code IN ('responsible_flight_rescuer', 'chief_physician_in_charge'))
);

-- +goose Down
DROP TABLE IF EXISTS base_contact_roles;
