USE bulo;

SET @col := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'tarifa_regioes' AND COLUMN_NAME = 'dinamica_ativa'
);
SET @sql := IF(@col = 0, 'ALTER TABLE tarifa_regioes ADD COLUMN dinamica_ativa TINYINT(1) NOT NULL DEFAULT 0 AFTER pro_ativo', '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 = 'tarifa_regioes' AND COLUMN_NAME = 'dinamica_multiplicador_max'
);
SET @sql := IF(@col = 0, 'ALTER TABLE tarifa_regioes ADD COLUMN dinamica_multiplicador_max DECIMAL(4,2) NOT NULL DEFAULT 1.80 AFTER dinamica_ativa', '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 = 'tarifa_regioes' AND COLUMN_NAME = 'dinamica_sensibilidade'
);
SET @sql := IF(@col = 0, 'ALTER TABLE tarifa_regioes ADD COLUMN dinamica_sensibilidade VARCHAR(12) NOT NULL DEFAULT ''media'' AFTER dinamica_multiplicador_max', '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 = 'tarifa_regioes' AND COLUMN_NAME = 'dinamica_celula_km'
);
SET @sql := IF(@col = 0, 'ALTER TABLE tarifa_regioes ADD COLUMN dinamica_celula_km DECIMAL(5,2) NOT NULL DEFAULT 2.00 AFTER dinamica_sensibilidade', '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 = 'tarifa_regioes' AND COLUMN_NAME = 'dinamica_janela_minutos'
);
SET @sql := IF(@col = 0, 'ALTER TABLE tarifa_regioes ADD COLUMN dinamica_janela_minutos INT NOT NULL DEFAULT 12 AFTER dinamica_celula_km', '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 = 'corridas' AND COLUMN_NAME = 'valor_base'
);
SET @sql := IF(@col = 0, 'ALTER TABLE corridas ADD COLUMN valor_base DECIMAL(10,2) NULL AFTER valor', '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 = 'corridas' AND COLUMN_NAME = 'multiplicador_dinamico'
);
SET @sql := IF(@col = 0, 'ALTER TABLE corridas ADD COLUMN multiplicador_dinamico DECIMAL(4,2) NOT NULL DEFAULT 1.00 AFTER valor_base', '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 = 'corridas' AND COLUMN_NAME = 'valor_dinamico_adicional'
);
SET @sql := IF(@col = 0, 'ALTER TABLE corridas ADD COLUMN valor_dinamico_adicional DECIMAL(10,2) NOT NULL DEFAULT 0.00 AFTER multiplicador_dinamico', '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 = 'corridas' AND COLUMN_NAME = 'dinamica_celula_id'
);
SET @sql := IF(@col = 0, 'ALTER TABLE corridas ADD COLUMN dinamica_celula_id VARCHAR(80) NULL AFTER valor_dinamico_adicional', '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 = 'corridas' AND COLUMN_NAME = 'dinamica_motivo'
);
SET @sql := IF(@col = 0, 'ALTER TABLE corridas ADD COLUMN dinamica_motivo VARCHAR(255) NULL AFTER dinamica_celula_id', '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 = 'corridas' AND COLUMN_NAME = 'dinamica_dados_json'
);
SET @sql := IF(@col = 0, 'ALTER TABLE corridas ADD COLUMN dinamica_dados_json LONGTEXT NULL AFTER dinamica_motivo', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @idx := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'corridas' AND INDEX_NAME = 'idx_corridas_dinamica_celula'
);
SET @sql := IF(@idx = 0, 'ALTER TABLE corridas ADD INDEX idx_corridas_dinamica_celula (dinamica_celula_id)', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
