import { modifyCharacterOrScene } from '@/api/video_flow';

/**
 * 保存编辑用例类
 * @description 保存接口所需的参数数据，提供保存和清空方法
 */
export class SaveEditUseCase {
   static projectId: string = '';
   static characterId: {
    /** 角色ID */
    character_id: string;
    /** 角色名称 */
    name: string;
  }[] = [];
   static videoTasks: Array<{
    task_id: string;
    video_ids: string[];
  }> = [];

  /**
   * 设置项目ID
   * @param projectId 项目ID
   */
  static setProjectId(projectId: string): void {
    SaveEditUseCase.projectId = projectId;
  }

  /**
   * 设置角色ID
   * @param characterId 角色ID
   */
  static setCharacterId(characterId: {
    /** 角色ID */
    character_id: string;
    /** 角色名称 */
    name: string;
  }[]): void {
    // 使用Set根据character_id去重
    const seenIds = new Set<string>();
    SaveEditUseCase.characterId = characterId.filter(character => {
      if (seenIds.has(character.character_id)) {
        return false;
      }
      seenIds.add(character.character_id);
      return true;
    });
  }

  /**
   * 设置视频任务列表
   * @param videoTasks 视频任务列表
   */
  static setVideoTasks(videoTasks: Array<{
    task_id: string;
    video_ids: string[];
  }>): void {
    // 使用Set根据task_id去重
    const seenIds = new Set<string>();
    SaveEditUseCase.videoTasks = videoTasks.filter(task => {
      if (seenIds.has(task.task_id)) {
        return false;
      }
      seenIds.add(task.task_id);
      return true;
    });
  }

  /**
   * 添加视频任务
   * @param taskId 任务ID
   * @param videoIds 视频ID列表
   */
  static addVideoTask(taskId: string, videoIds: string[]): void {
    SaveEditUseCase.videoTasks.push({
      task_id: taskId,
      video_ids: videoIds,
    });
  }

  /**
   * 主要保存方法
   * @description 调用保存接口，传入当前类内保存的数据
   * @returns Promise<保存结果>
   */
  static async saveData(): Promise<{
    success: boolean;
    message: string;
    data?: any;
  }> {
    try {
      if (!SaveEditUseCase.projectId) {
        throw new Error('项目ID不能为空');
      }

      const response = await modifyCharacterOrScene({
        project_id: SaveEditUseCase.projectId,
        character_id: SaveEditUseCase.characterId,
        video_tasks: SaveEditUseCase.videoTasks,
      });

      if (!response.successful) {
        throw new Error(response.message || '保存数据失败');
      }

      return {
        success: true,
        message: '数据保存成功',
        data: response.data,
      };
    } catch (error) {
      console.error('保存数据失败:', error);
      return {
        success: false,
        message: error instanceof Error ? error.message : '保存数据失败',
      };
    }
  }

  /**
   * 保存角色数据方法
   * @description 保存character_id数据
   * @returns Promise<保存结果>
   */
  static async saveRoleData(): Promise<{
    success: boolean;
    message: string;
    data?: any;
  }> {
    // 角色数据保存就是保存character_id
    return SaveEditUseCase.saveData();
  }

  /**
   * 保存镜头数据方法
   * @description 保存video_tasks数据
   * @returns Promise<保存结果>
   */
  static async saveShotData(): Promise<{
    success: boolean;
    message: string;
    data?: any;
  }> {
    // 镜头数据保存就是保存video_tasks
    return SaveEditUseCase.saveData();
  }

  /**
   * 清空数据方法
   * @description 清空当前类内保存的所有数据
   */
  static clearData(): void {
    SaveEditUseCase.projectId = '';
    SaveEditUseCase.characterId = [];
    SaveEditUseCase.videoTasks = [];
  }

  /**
   * 获取当前保存的数据
   * @returns 当前保存的数据对象
   */
  static getCurrentData(): {
    projectId: string;
    characterId: {
      character_id: string;
      name: string;
    }[];
    videoTasks: Array<{
      task_id: string;
      video_ids: string[];
    }>;
  } {
    return {
      projectId: SaveEditUseCase.projectId,
      characterId: [...SaveEditUseCase.characterId],
      videoTasks: [...SaveEditUseCase.videoTasks],
    };
  }
}

/**
 * 创建保存编辑用例实例
 * @returns {SaveEditUseCase} 保存编辑用例实例
 */
export const createSaveEditUseCase = (): SaveEditUseCase => {
  return new SaveEditUseCase();
};
