-- =====================================================
-- 视频发布模块 - 数据库迁移 SQL
-- 执行方式: 登录 MySQL 后 source 此文件
-- =====================================================

-- 平台账号表
CREATE TABLE IF NOT EXISTS `PlatformAccount` (
  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `userId` INT NOT NULL,
  `platform` VARCHAR(20) NOT NULL COMMENT 'douyin, kuaishou, bilibili',
  `nickname` VARCHAR(100) NOT NULL DEFAULT '',
  `avatar` VARCHAR(500) NOT NULL DEFAULT '',
  `cookies` TEXT NOT NULL COMMENT '登录 Cookie',
  `headers` TEXT NULL COMMENT '关键请求头 JSON',
  `isValid` BOOLEAN NOT NULL DEFAULT TRUE,
  `expireTime` DATETIME NULL,
  `createdAt` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updatedAt` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY `userId_platform` (`userId`, `platform`),
  INDEX `idx_userId` (`userId`),
  INDEX `idx_platform` (`platform`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 发布任务表
CREATE TABLE IF NOT EXISTS `PublishTask` (
  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `userId` INT NOT NULL,
  `videoProjectId` INT NOT NULL,
  `platform` VARCHAR(20) NOT NULL COMMENT 'douyin, kuaishou, bilibili',
  `title` TEXT NOT NULL,
  `description` TEXT NULL,
  `tags` TEXT NULL COMMENT 'JSON 数组',
  `coverUrl` TEXT NULL,
  `videoUrl` TEXT NULL COMMENT '本地视频路径',
  `status` VARCHAR(20) NOT NULL DEFAULT 'pending' COMMENT 'pending, uploading, success, failed',
  `errorMsg` TEXT NULL,
  `publishedUrl` TEXT NULL COMMENT '发布后的作品链接',
  `createdAt` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updatedAt` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX `idx_user_platform` (`userId`, `platform`),
  INDEX `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
