-- +goose Up
-- +goose StatementBegin

ALTER TABLE configuracoes_crm
  ADD COLUMN IF NOT EXISTS openai_key        TEXT DEFAULT '',
  ADD COLUMN IF NOT EXISTS deepseek_key      TEXT DEFAULT '',
  ADD COLUMN IF NOT EXISTS groq_key          TEXT DEFAULT '',
  ADD COLUMN IF NOT EXISTS whatsapp_api_url  TEXT DEFAULT '',
  ADD COLUMN IF NOT EXISTS whatsapp_api_key  TEXT DEFAULT '',
  ADD COLUMN IF NOT EXISTS whatsapp_instance TEXT DEFAULT '',
  ADD COLUMN IF NOT EXISTS ia_ativa          BOOLEAN NOT NULL DEFAULT true;

-- Migra whatsapp_token antigo para whatsapp_api_key
UPDATE configuracoes_crm
SET whatsapp_api_key = COALESCE(whatsapp_token, '')
WHERE COALESCE(whatsapp_token,'') != '' AND COALESCE(whatsapp_api_key,'') = '';

-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
ALTER TABLE configuracoes_crm
  DROP COLUMN IF EXISTS openai_key,
  DROP COLUMN IF EXISTS deepseek_key,
  DROP COLUMN IF EXISTS groq_key,
  DROP COLUMN IF EXISTS whatsapp_api_url,
  DROP COLUMN IF EXISTS whatsapp_api_key,
  DROP COLUMN IF EXISTS whatsapp_instance,
  DROP COLUMN IF EXISTS ia_ativa;
-- +goose StatementEnd
