/**
 * 异步任务类型定义
 */

// 任务类型
export type TaskType = 
  | 'sync_comments'      // 同步评论
  | 'sync_works'         // 同步作品
  | 'sync_account'       // 同步账号信息
  | 'publish_video'      // 发布视频
  | 'batch_reply'        // 批量回复评论
  | 'delete_work';       // 删除平台作品

// 任务状态
export type TaskStatus = 
  | 'pending'      // 等待中
  | 'running'      // 执行中
  | 'completed'    // 已完成
  | 'failed'       // 失败
  | 'cancelled';   // 已取消

// 任务优先级
export type TaskPriority = 'low' | 'normal' | 'high';

// 任务信息
export interface Task {
  id: string;
  type: TaskType;
  title: string;
  description?: string;
  status: TaskStatus;
  progress: number;        // 0-100
  currentStep?: string;    // 当前步骤描述
  totalSteps?: number;     // 总步骤数
  currentStepIndex?: number; // 当前步骤索引
  priority: TaskPriority;
  silent?: boolean;        // 静默执行，前台不弹框显示
  createdAt: string;
  startedAt?: string;
  completedAt?: string;
  error?: string;
  result?: TaskResult;
  // 关联数据
  accountId?: number;
  accountName?: string;
  platform?: string;
}

// 任务结果
export interface TaskResult {
  success: boolean;
  message: string;
  data?: Record<string, unknown>;
}

// 任务类型配置
export const TASK_TYPE_CONFIG: Record<TaskType, { 
  name: string; 
  icon: string;
  color: string;
}> = {
  sync_comments: {
    name: '同步评论',
    icon: 'ChatDotRound',
    color: '#409eff',
  },
  sync_works: {
    name: '同步作品',
    icon: 'VideoCamera',
    color: '#67c23a',
  },
  sync_account: {
    name: '同步账号',
    icon: 'User',
    color: '#e6a23c',
  },
  publish_video: {
    name: '发布视频',
    icon: 'Upload',
    color: '#f56c6c',
  },
  batch_reply: {
    name: '批量回复',
    icon: 'ChatLineSquare',
    color: '#909399',
  },
  delete_work: {
    name: '删除作品',
    icon: 'Delete',
    color: '#f56c6c',
  },
};

// WebSocket 任务事件类型
export const TASK_WS_EVENTS = {
  // 任务生命周期
  TASK_CREATED: 'task:created',
  TASK_STARTED: 'task:started',
  TASK_PROGRESS: 'task:progress',
  TASK_COMPLETED: 'task:completed',
  TASK_FAILED: 'task:failed',
  TASK_CANCELLED: 'task:cancelled',
  // 任务列表
  TASK_LIST: 'task:list',
} as const;

// 任务进度更新
export interface TaskProgressUpdate {
  taskId: string;
  progress: number;
  currentStep?: string;
  currentStepIndex?: number;
  message?: string;
}

// 创建任务请求
export interface CreateTaskRequest {
  type: TaskType;
  title?: string;
  description?: string;
  priority?: TaskPriority;
  silent?: boolean;        // 静默执行，前台不弹框显示
  accountId?: number;
  data?: Record<string, unknown>;
}
