-- Migration 001: Multi-Sender WABA Architecture
-- Task #6: Replace single senderNumber in infobip_configs with infobip_senders table

-- 1. Remove senderNumber from infobip_configs (no longer needed — senders managed separately)
ALTER TABLE infobip_configs DROP COLUMN IF EXISTS sender_number;

-- 2. Create infobip_senders table (multiple senders per service/WABA)
CREATE TABLE IF NOT EXISTS infobip_senders (
  id            VARCHAR PRIMARY KEY DEFAULT gen_random_uuid(),
  service_id    VARCHAR NOT NULL REFERENCES services(id) ON DELETE CASCADE,
  number        VARCHAR NOT NULL,
  display_name  VARCHAR,
  status        VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',   -- ACTIVE | INACTIVE | FAILED
  quality       VARCHAR(20) NOT NULL DEFAULT 'UNKNOWN',  -- HIGH | MEDIUM | LOW | UNKNOWN
  last_used_at  TIMESTAMP,
  ativo         BOOLEAN NOT NULL DEFAULT true,
  created_at    TIMESTAMP DEFAULT NOW()
);

-- 3. Add senderId to waba_messages for per-message sender tracking
ALTER TABLE waba_messages ADD COLUMN IF NOT EXISTS sender_id VARCHAR REFERENCES infobip_senders(id) ON DELETE SET NULL;
