-- Migration 000: Controle de migrations aplicadas
-- Este arquivo roda sempre (antes do loop principal) e e idempotente.
-- Cria a tabela de rastreamento e marca todas as migrations existentes
-- como ja aplicadas para que nao re-executem em ambientes ja provisionados.

CREATE TABLE IF NOT EXISTS schema_migrations (
  filename   VARCHAR(255) PRIMARY KEY,
  applied_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO schema_migrations (filename) VALUES
  ('001_auth.sql'),
  ('002_area.sql'),
  ('003_whatsapp.sql'),
  ('004_whatsapp_templates.sql'),
  ('005_whatsapp_assignment_queue.sql'),
  ('006_whatsapp_triage_state.sql'),
  ('007_agent_notes.sql'),
  ('008_mensagens.sql'),
  ('009_customer_contacts.sql'),
  ('010_agenda_contatos.sql'),
  ('011_whatsapp_awaiting_customer_reply.sql'),
  ('012_whatsapp_template_workflow.sql'),
  ('013_agent_presence_pause.sql'),
  ('014_hr_decision_tree_keywords.sql'),
  ('015_configurable_triage_flow.sql'),
  ('016_triage_resolution_step.sql'),
  ('017_bot_flow_builder.sql'),
  ('018_bot_flow_close_node.sql'),
  ('019_admin_audit_ai_contents.sql'),
  ('020_whatsapp_template_category.sql'),
  ('021_agenda_contact_channels.sql'),
  ('022_whatsapp_config.sql'),
  ('023_whatsapp_templates_hsm.sql'),
  ('024_mensagens_media_columns.sql'),
  ('025_mensagens_media_filename.sql'),
  ('026_remove_fake_templates.sql')
ON CONFLICT DO NOTHING;
