USE bulo;

SET @col := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'usuarios' AND COLUMN_NAME = 'passageiro_documento_url'
);
SET @sql := IF(@col = 0, 'ALTER TABLE usuarios ADD COLUMN passageiro_documento_url VARCHAR(255) NULL AFTER foto_url', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'usuarios' AND COLUMN_NAME = 'passageiro_documento_selfie_url'
);
SET @sql := IF(@col = 0, 'ALTER TABLE usuarios ADD COLUMN passageiro_documento_selfie_url VARCHAR(255) NULL AFTER passageiro_documento_url', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'usuarios' AND COLUMN_NAME = 'passageiro_documento_status'
);
SET @sql := IF(@col = 0, 'ALTER TABLE usuarios ADD COLUMN passageiro_documento_status ENUM(''nao_enviado'',''pendente'',''aprovado'',''reprovado'') NOT NULL DEFAULT ''nao_enviado'' AFTER passageiro_documento_selfie_url', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'usuarios' AND COLUMN_NAME = 'passageiro_documento_observacao'
);
SET @sql := IF(@col = 0, 'ALTER TABLE usuarios ADD COLUMN passageiro_documento_observacao VARCHAR(255) NULL AFTER passageiro_documento_status', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'usuarios' AND COLUMN_NAME = 'passageiro_documento_aprovado_por'
);
SET @sql := IF(@col = 0, 'ALTER TABLE usuarios ADD COLUMN passageiro_documento_aprovado_por VARCHAR(80) NULL AFTER passageiro_documento_observacao', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'usuarios' AND COLUMN_NAME = 'passageiro_documento_aprovado_em'
);
SET @sql := IF(@col = 0, 'ALTER TABLE usuarios ADD COLUMN passageiro_documento_aprovado_em DATETIME NULL AFTER passageiro_documento_aprovado_por', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
