-- Social Listening Pipeline — Cost Tracking Schema

CREATE TABLE IF NOT EXISTS runs (
  id            SERIAL PRIMARY KEY,
  client_name   TEXT NOT NULL,
  category      TEXT NOT NULL,
  platforms     TEXT[] NOT NULL DEFAULT '{}',
  brief_json    JSONB NOT NULL,
  status        TEXT NOT NULL DEFAULT 'running',   -- running | completed | failed
  started_at    TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  finished_at   TIMESTAMPTZ,
  total_cost_usd  NUMERIC(10,6) NOT NULL DEFAULT 0,
  claude_cost_usd NUMERIC(10,6) NOT NULL DEFAULT 0,
  apify_cost_usd  NUMERIC(10,6) NOT NULL DEFAULT 0,
  total_input_tokens  INTEGER NOT NULL DEFAULT 0,
  total_output_tokens INTEGER NOT NULL DEFAULT 0,
  report_path   TEXT
);

CREATE TABLE IF NOT EXISTS cost_events (
  id          SERIAL PRIMARY KEY,
  run_id      INTEGER NOT NULL REFERENCES runs(id) ON DELETE CASCADE,
  created_at  TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  stage       INTEGER NOT NULL,
  stage_name  TEXT NOT NULL,
  source      TEXT NOT NULL,             -- 'claude' | 'apify'
  label       TEXT NOT NULL,             -- e.g. 'CM Review', 'TikTok hashtag: hm'
  model       TEXT,                      -- claude model name or apify actor id
  input_tokens  INTEGER NOT NULL DEFAULT 0,
  output_tokens INTEGER NOT NULL DEFAULT 0,
  cost_usd    NUMERIC(10,6) NOT NULL DEFAULT 0,
  metadata    JSONB                      -- extra info (run_id for apify, etc.)
);

CREATE INDEX idx_cost_events_run_id ON cost_events(run_id);
CREATE INDEX idx_runs_started_at ON runs(started_at DESC);
