CREATE TABLE IF NOT EXISTS ai_usage (
  id                 UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  family_id          UUID,
  user_id            UUID,
  intent             TEXT,                -- structured_query | memory_search | general_parenting | medical_redirect
  model_used         TEXT,
  prompt_tokens      INTEGER,
  completion_tokens  INTEGER,
  total_tokens       INTEGER,
  cost_estimate_paise NUMERIC(10,4),      -- rough estimate for monitoring
  duration_ms        INTEGER,
  created_at         TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS ai_usage_family_idx    ON ai_usage (family_id);
CREATE INDEX IF NOT EXISTS ai_usage_created_idx   ON ai_usage (created_at DESC);
