import type { WorkActivity, WorkActivityAction, WorkActivitySourceType } from './work-activity';

export interface EntityActivityLogEntry {
  id: string;
  occurredAt: string;
  actorId: string;
  label: string;
  summary: string;
}

export const WORK_ACTIVITY_ACTION_LABEL: Record<WorkActivityAction, string> = {
  version_plan_created: '新建计划',
  version_plan_started: '开始计划',
  version_plan_completed: '完成计划',
  version_plan_requirement_progress: '需求进度',
  version_plan_research_direction_progress: '调研方向',
  dev_task_created: '新建开发任务',
  dev_task_started: '开始开发',
  dev_task_self_testing: '进入自测',
  dev_task_submitted: '已提测',
  dev_task_blocked: '标记阻塞',
  dev_task_unblocked: '解除阻塞',
  dev_task_transferred: '转交开发任务',
  test_case_created: '新建测试用例',
  test_case_started: '开始测试',
  test_case_passed: '测试通过',
  test_case_failed: '测试不通过',
  test_case_blocked: '测试阻塞',
  bug_created: '新建 Bug',
  bug_fixing: '开始修复',
  bug_fixed: '已修复',
  bug_closed: '已关闭',
  bug_blocked: 'Bug 阻塞',
  bug_transferred: '转交 Bug',
  progress_note_added: '补充进展',
};

export function getEntityActivityLogEntries(
  activities: WorkActivity[],
  sourceType: WorkActivitySourceType,
  sourceId: string,
  legacyEntries: EntityActivityLogEntry[] = [],
): EntityActivityLogEntry[] {
  const activityEntries = activities
    .filter((activity) => activity.sourceType === sourceType && activity.sourceId === sourceId)
    .map((activity) => ({
      id: activity.id,
      occurredAt: activity.occurredAt,
      actorId: activity.actorId,
      label: WORK_ACTIVITY_ACTION_LABEL[activity.action] || activity.action,
      summary: activity.summary,
    }));

  return [...activityEntries, ...legacyEntries]
    .sort((a, b) => b.occurredAt.localeCompare(a.occurredAt));
}
