-- Launch pipeline v3: offer + pipeline state, usage/cost log, pipeline batches.
-- AI usage & cost log (every request, direct or batch).
CREATE TABLE IF NOT EXISTS ai_usage (
  id                 BIGINT AUTO_INCREMENT PRIMARY KEY,
  website_id         INT NULL,
  task               VARCHAR(40) NULL,
  model              VARCHAR(80) NOT NULL,
  input_tokens       INT NOT NULL DEFAULT 0,
  output_tokens      INT NOT NULL DEFAULT 0,
  cache_write_tokens INT NOT NULL DEFAULT 0,
  cache_read_tokens  INT NOT NULL DEFAULT 0,
  web_searches       INT NOT NULL DEFAULT 0,
  batch              TINYINT(1) NOT NULL DEFAULT 0,
  cost_usd           DECIMAL(10,5) NOT NULL DEFAULT 0,
  created_at         DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  KEY idx_site (website_id), KEY idx_task (task), KEY idx_created (created_at)
) ENGINE=InnoDB CHARSET=utf8mb4;

ALTER TABLE websites ADD COLUMN offer JSON AFTER layout, ADD COLUMN pipeline JSON AFTER offer;
ALTER TABLE batches
  MODIFY type ENUM('research_market','generate_guide','generate_content','generate_brief','build_deliverable','compare_research','compare_recheck','next_version','free_version','pipeline') NOT NULL;
-- Copy is now written from the product specs, so the brief <-> copy reconciliation loop is off by default.
UPDATE settings SET value = 'off' WHERE `key` = 'brief_reconcile';
