import test from 'node:test';
import assert from 'node:assert/strict';

import * as versionPlan from './version-plan';
import type { VersionPlan } from './version-plan';

function plan(patch: Partial<VersionPlan>): VersionPlan {
  return {
    id: 'plan-1',
    versionId: 'version-1',
    type: 'research',
    title: '调研方案',
    owner: 'PM',
    startTime: '2026-06-30T09:00',
    endTime: '2026-06-30T18:00',
    status: 'in_progress',
    createdAt: '2026-06-30',
    addedBy: 'PM',
    ...patch,
  };
}

test('summarizes research direction progress from direction task status', () => {
  const getResearchDirectionProgressSummary = (versionPlan as any).getResearchDirectionProgressSummary as undefined | ((item: VersionPlan) => {
    total: number;
    completed: number;
    partial: number;
    notStarted: number;
    percent: number;
  });
  assert.equal(typeof getResearchDirectionProgressSummary, 'function');

  const summary = getResearchDirectionProgressSummary!(plan({
    tasks: [
      { id: 'task-1', title: '竞品分析', status: 'completed' },
      { id: 'task-2', title: '用户访谈', status: 'in_progress' },
      { id: 'task-3', title: '数据调研', status: 'pending' },
    ],
  }));

  assert.deepEqual(summary, {
    total: 3,
    completed: 1,
    partial: 1,
    notStarted: 1,
    percent: 33,
  });
});

test('updates research direction progress and creates a direction log', () => {
  const updateResearchDirectionProgress = (versionPlan as any).updateResearchDirectionProgress as undefined | ((item: VersionPlan, input: {
    taskId: string;
    status: string;
    completedContent?: string;
    remainingContent?: string;
    updatedBy: string;
    updatedAt: string;
  }) => any);
  assert.equal(typeof updateResearchDirectionProgress, 'function');

  const next = updateResearchDirectionProgress!(plan({
    tasks: [
      { id: 'task-1', title: '竞品分析', status: 'in_progress', currentWorkStartedAt: '2026-06-30T09:30:00.000Z' },
      { id: 'task-2', title: '用户访谈', status: 'pending' },
    ],
  }), {
    taskId: 'task-1',
    status: 'partial',
    completedContent: '完成竞品登录流程对比',
    remainingContent: '补充支付流程差异',
    updatedBy: 'PM',
    updatedAt: '2026-06-30T10:00:00.000Z',
  });

  assert.equal(next.tasks?.[0]?.status, 'in_progress');
  assert.equal(next.tasks?.[0]?.completedContent, '完成竞品登录流程对比');
  assert.equal(next.tasks?.[0]?.remainingContent, '补充支付流程差异');
  assert.equal(next.tasks?.[0]?.currentWorkStartedAt, undefined);
  assert.equal(next.logs?.[0]?.type, 'research_direction_progress');
  assert.equal(next.logs?.[0]?.directionTaskId, 'task-1');
  assert.equal(next.logs?.[0]?.directionTitle, '竞品分析');
  assert.equal(next.logs?.[0]?.coverageStatus, 'partial');
  assert.equal(next.logs?.[0]?.workStartedAt, '2026-06-30T09:30:00.000Z');
});

test('starts research direction work and marks the direction in progress', () => {
  const startResearchDirectionWork = (versionPlan as any).startResearchDirectionWork as undefined | ((item: VersionPlan, input: {
    taskId: string;
    startedAt: string;
    updatedBy: string;
  }) => any);
  assert.equal(typeof startResearchDirectionWork, 'function');

  const next = startResearchDirectionWork!(plan({
    tasks: [
      { id: 'task-1', title: '竞品分析', status: 'pending' },
      { id: 'task-2', title: '用户访谈', status: 'pending' },
    ],
  }), {
    taskId: 'task-1',
    startedAt: '2026-06-30T09:30:00.000Z',
    updatedBy: 'PM',
  });

  assert.equal(next.tasks?.[0]?.status, 'in_progress');
  assert.equal(next.tasks?.[0]?.currentWorkStartedAt, '2026-06-30T09:30:00.000Z');
  assert.equal(next.tasks?.[0]?.updatedBy, 'PM');
  assert.equal(next.tasks?.[1]?.status, 'pending');
});
