-- +goose Up
-- Remove deprecated nationality module storage.

SET @drop_fk_nationality := (
  SELECT IF(
    EXISTS (
      SELECT 1
      FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
      WHERE TABLE_SCHEMA = DATABASE()
        AND TABLE_NAME = 'patient_data'
        AND COLUMN_NAME = 'nationality_id'
        AND REFERENCED_TABLE_NAME IS NOT NULL
    ),
    (
      SELECT CONCAT('ALTER TABLE patient_data DROP FOREIGN KEY ', CONSTRAINT_NAME)
      FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
      WHERE TABLE_SCHEMA = DATABASE()
        AND TABLE_NAME = 'patient_data'
        AND COLUMN_NAME = 'nationality_id'
        AND REFERENCED_TABLE_NAME IS NOT NULL
      LIMIT 1
    ),
    'SELECT 1'
  )
);
PREPARE stmt_drop_fk_nationality FROM @drop_fk_nationality;
EXECUTE stmt_drop_fk_nationality;
DEALLOCATE PREPARE stmt_drop_fk_nationality;

SET @drop_idx_nationality := (
  SELECT IF(
    EXISTS (
      SELECT 1
      FROM INFORMATION_SCHEMA.STATISTICS
      WHERE TABLE_SCHEMA = DATABASE()
        AND TABLE_NAME = 'patient_data'
        AND INDEX_NAME = 'idx_patient_data_nationality_id'
    ),
    'ALTER TABLE patient_data DROP INDEX idx_patient_data_nationality_id',
    'SELECT 1'
  )
);
PREPARE stmt_drop_idx_nationality FROM @drop_idx_nationality;
EXECUTE stmt_drop_idx_nationality;
DEALLOCATE PREPARE stmt_drop_idx_nationality;

SET @drop_col_nationality := (
  SELECT IF(
    EXISTS (
      SELECT 1
      FROM INFORMATION_SCHEMA.COLUMNS
      WHERE TABLE_SCHEMA = DATABASE()
        AND TABLE_NAME = 'patient_data'
        AND COLUMN_NAME = 'nationality_id'
    ),
    'ALTER TABLE patient_data DROP COLUMN nationality_id',
    'SELECT 1'
  )
);
PREPARE stmt_drop_col_nationality FROM @drop_col_nationality;
EXECUTE stmt_drop_col_nationality;
DEALLOCATE PREPARE stmt_drop_col_nationality;

DROP TABLE IF EXISTS nationalities;

-- +goose Down
-- Recreate minimal nationality storage.

CREATE TABLE IF NOT EXISTS nationalities (
  id BINARY(16) NOT NULL PRIMARY KEY,
  land_id BINARY(16) NULL,
  name VARCHAR(128) NOT NULL,
  sortkey INT NULL,
  is_active TINYINT(1) NOT NULL DEFAULT 1,
  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,
  deleted_at DATETIME NULL,
  deleted_by BINARY(16) NULL,
  KEY idx_nationalities_land_id (land_id),
  KEY idx_nationalities_sortkey (sortkey),
  KEY idx_nationalities_is_active (is_active),
  KEY idx_nationalities_deleted_at (deleted_at)
);

SET @add_col_nationality := (
  SELECT IF(
    EXISTS (
      SELECT 1
      FROM INFORMATION_SCHEMA.COLUMNS
      WHERE TABLE_SCHEMA = DATABASE()
        AND TABLE_NAME = 'patient_data'
        AND COLUMN_NAME = 'nationality_id'
    ),
    'SELECT 1',
    'ALTER TABLE patient_data ADD COLUMN nationality_id BINARY(16) NULL AFTER co_insured'
  )
);
PREPARE stmt_add_col_nationality FROM @add_col_nationality;
EXECUTE stmt_add_col_nationality;
DEALLOCATE PREPARE stmt_add_col_nationality;

SET @add_idx_nationality := (
  SELECT IF(
    EXISTS (
      SELECT 1
      FROM INFORMATION_SCHEMA.STATISTICS
      WHERE TABLE_SCHEMA = DATABASE()
        AND TABLE_NAME = 'patient_data'
        AND INDEX_NAME = 'idx_patient_data_nationality_id'
    ),
    'SELECT 1',
    'ALTER TABLE patient_data ADD KEY idx_patient_data_nationality_id (nationality_id)'
  )
);
PREPARE stmt_add_idx_nationality FROM @add_idx_nationality;
EXECUTE stmt_add_idx_nationality;
DEALLOCATE PREPARE stmt_add_idx_nationality;
