-- Page preview images.
-- Page preview images rendered from the real deliverable files.
CREATE TABLE IF NOT EXISTS preview_jobs (
  id            INT AUTO_INCREMENT PRIMARY KEY,
  product_id    INT NOT NULL,
  website_id    INT NOT NULL,
  status        ENUM('queued','running','paused','collect','ready','failed') NOT NULL DEFAULT 'queued',
  options       JSON,                                -- watermark, styled, collage, crops, max_width
  sources       JSON,                                -- [{file_id (product_files.id), name}]
  container_id  VARCHAR(120) NULL,
  messages      LONGTEXT,
  iterations    INT NOT NULL DEFAULT 0,
  uploads       JSON,
  images        JSON,                                -- [{stored, kind, source, page, alt, w, h, added}]
  report        JSON,
  usage_json    JSON,
  error         TEXT,
  started_at    DATETIME,
  finished_at   DATETIME,
  created_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY idx_product (product_id), KEY idx_status (status),
  CONSTRAINT fk_prev_product FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE
) ENGINE=InnoDB CHARSET=utf8mb4;

ALTER TABLE product_images ADD COLUMN origin VARCHAR(20) NULL AFTER sort_order;
