-- ─────────────────────────────────────────────────────────────────
-- FASE 7: Logs do Motor de WhatsApp (NewWhats)
-- ─────────────────────────────────────────────────────────────────

CREATE TABLE IF NOT EXISTS whatsapp_logs (
  id            SERIAL PRIMARY KEY,
  user_id       INTEGER REFERENCES users(id) ON DELETE SET NULL,
  phone         TEXT NOT NULL,
  type          TEXT NOT NULL, -- 'purchase_confirmed', 'winner', 'last_hours', etc
  payload       TEXT,          -- O JSON da request final
  status        TEXT DEFAULT 'queued' CHECK (status IN ('queued', 'processing', 'sent', 'failed', 'retrying')),
  response      TEXT,          -- Resposta de erro ou sucesso do NewWhats HTTP
  created_at    TIMESTAMPTZ DEFAULT NOW(),
  updated_at    TIMESTAMPTZ DEFAULT NOW()
);
