-- 002_create_jobs.sql
CREATE TABLE IF NOT EXISTS jobs (
  id INT AUTO_INCREMENT PRIMARY KEY,
  project_id INT NOT NULL,
  type ENUM('plan','naskah','storyboard','render','upload') NOT NULL,
  status ENUM('pending','running','done','failed') NOT NULL DEFAULT 'pending',
  payload JSON NULL,
  result JSON NULL,
  external_id VARCHAR(255) NULL,
  attempts INT NOT NULL DEFAULT 0,
  next_retry_at DATETIME NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,
  INDEX idx_status_next (status, next_retry_at),
  INDEX idx_project (project_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
