-- Remove campos de provedores externos e ajusta para assinatura própria

-- Remover campos de provedores externos se existirem (um por vez para evitar erros)
SET @preparedStatement = (SELECT IF(
  (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
   WHERE TABLE_SCHEMA = DATABASE() 
   AND TABLE_NAME = 'contratos_config' 
   AND COLUMN_NAME = 'provider_assinatura') > 0,
  'ALTER TABLE contratos_config DROP COLUMN provider_assinatura',
  'SELECT 1'
));
PREPARE alterIfExists FROM @preparedStatement;
EXECUTE alterIfExists;
DEALLOCATE PREPARE alterIfExists;

SET @preparedStatement = (SELECT IF(
  (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
   WHERE TABLE_SCHEMA = DATABASE() 
   AND TABLE_NAME = 'contratos_config' 
   AND COLUMN_NAME = 'docusign_api_key') > 0,
  'ALTER TABLE contratos_config DROP COLUMN docusign_api_key',
  'SELECT 1'
));
PREPARE alterIfExists FROM @preparedStatement;
EXECUTE alterIfExists;
DEALLOCATE PREPARE alterIfExists;

SET @preparedStatement = (SELECT IF(
  (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
   WHERE TABLE_SCHEMA = DATABASE() 
   AND TABLE_NAME = 'contratos_config' 
   AND COLUMN_NAME = 'docusign_api_secret') > 0,
  'ALTER TABLE contratos_config DROP COLUMN docusign_api_secret',
  'SELECT 1'
));
PREPARE alterIfExists FROM @preparedStatement;
EXECUTE alterIfExists;
DEALLOCATE PREPARE alterIfExists;

SET @preparedStatement = (SELECT IF(
  (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
   WHERE TABLE_SCHEMA = DATABASE() 
   AND TABLE_NAME = 'contratos_config' 
   AND COLUMN_NAME = 'docusign_account_id') > 0,
  'ALTER TABLE contratos_config DROP COLUMN docusign_account_id',
  'SELECT 1'
));
PREPARE alterIfExists FROM @preparedStatement;
EXECUTE alterIfExists;
DEALLOCATE PREPARE alterIfExists;

SET @preparedStatement = (SELECT IF(
  (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
   WHERE TABLE_SCHEMA = DATABASE() 
   AND TABLE_NAME = 'contratos_config' 
   AND COLUMN_NAME = 'clicksign_api_key') > 0,
  'ALTER TABLE contratos_config DROP COLUMN clicksign_api_key',
  'SELECT 1'
));
PREPARE alterIfExists FROM @preparedStatement;
EXECUTE alterIfExists;
DEALLOCATE PREPARE alterIfExists;

SET @preparedStatement = (SELECT IF(
  (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
   WHERE TABLE_SCHEMA = DATABASE() 
   AND TABLE_NAME = 'contratos_config' 
   AND COLUMN_NAME = 'clicksign_api_secret') > 0,
  'ALTER TABLE contratos_config DROP COLUMN clicksign_api_secret',
  'SELECT 1'
));
PREPARE alterIfExists FROM @preparedStatement;
EXECUTE alterIfExists;
DEALLOCATE PREPARE alterIfExists;

-- Adicionar campos de assinatura própria se não existirem
SET @preparedStatement = (SELECT IF(
  (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
   WHERE TABLE_SCHEMA = DATABASE() 
   AND TABLE_NAME = 'contratos_config' 
   AND COLUMN_NAME = 'usar_assinatura_digital') = 0,
  'ALTER TABLE contratos_config ADD COLUMN usar_assinatura_digital BOOLEAN DEFAULT TRUE, ADD COLUMN usar_certificado_icp_brasil BOOLEAN DEFAULT FALSE',
  'SELECT 1'
));
PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;

