import assert from 'node:assert/strict';
import test from 'node:test';
import type { XiaobaoRiskSnapshot } from './xiaobao-risk-trend';
import {
  findCachedInsight,
  mergeDailySnapshotCacheForSave,
  mergeInsightCacheForSave,
  upsertDailySnapshot,
  upsertInsight,
  type XiaobaoRiskInsightCacheItem,
} from './xiaobao-risk-cache';

const insightA: XiaobaoRiskInsightCacheItem = {
  versionId: 'v-1',
  riskSignature: 'sig-a',
  insight: {
    summary: '版本风险上升',
    why: ['剩余工作较多'],
    forecast: '可能延期 2 天',
    suggestedActions: ['压缩低优先级范围'],
    ownerHints: ['请项目负责人确认排期'],
    generatedAt: '2026-06-29T08:00:00.000Z',
  },
  generatedAt: '2026-06-29T08:00:00.000Z',
};

test('findCachedInsight returns matching signature only', () => {
  const rows: XiaobaoRiskInsightCacheItem[] = [
    insightA,
    { ...insightA, versionId: 'v-2', riskSignature: 'sig-a' },
    { ...insightA, versionId: 'v-1', riskSignature: 'sig-b' },
  ];

  assert.equal(findCachedInsight(rows, 'v-1', 'sig-a'), insightA);
  assert.equal(findCachedInsight(rows, 'v-1', 'sig-b')?.versionId, 'v-1');
  assert.equal(findCachedInsight(rows, 'v-2', 'sig-b'), undefined);
});

test('upsertDailySnapshot keeps one snapshot per version and date', () => {
  const existing: XiaobaoRiskSnapshot = {
    versionId: 'v-1',
    date: '2026-06-29',
    riskScore: 40,
    riskLevel: 'attention',
    openBugCount: 1,
    failedTestCount: 0,
    blockedCount: 0,
    silentRiskCount: 0,
    confidence: 80,
    createdAt: '2026-06-29T08:00:00.000Z',
  };
  const replacement: XiaobaoRiskSnapshot = { ...existing, riskScore: 72, createdAt: '2026-06-29T09:00:00.000Z' };
  const otherDay: XiaobaoRiskSnapshot = { ...existing, date: '2026-06-28', createdAt: '2026-06-28T09:00:00.000Z' };

  const result = upsertDailySnapshot([existing, otherDay], replacement);

  assert.deepEqual(result, [replacement, otherDay]);
});

test('upsertInsight replaces existing version signature pair', () => {
  const replacement: XiaobaoRiskInsightCacheItem = {
    ...insightA,
    insight: { ...insightA.insight, summary: '已重新生成' },
    generatedAt: '2026-06-29T09:00:00.000Z',
  };
  const otherSignature: XiaobaoRiskInsightCacheItem = { ...insightA, riskSignature: 'sig-b' };
  const otherVersion: XiaobaoRiskInsightCacheItem = { ...insightA, versionId: 'v-2' };

  const result = upsertInsight([insightA, otherSignature, otherVersion], replacement);

  assert.deepEqual(result, [replacement, otherSignature, otherVersion]);
});

test('mergeDailySnapshotCacheForSave preserves remote rows and local-only rows', () => {
  const remoteOnly: XiaobaoRiskSnapshot = {
    versionId: 'remote-version',
    date: '2026-06-29',
    riskScore: 30,
    riskLevel: 'attention',
    openBugCount: 0,
    failedTestCount: 0,
    blockedCount: 0,
    silentRiskCount: 0,
    confidence: 90,
    createdAt: '2026-06-29T08:00:00.000Z',
  };
  const localOnly: XiaobaoRiskSnapshot = { ...remoteOnly, versionId: 'local-version', riskScore: 50 };
  const item: XiaobaoRiskSnapshot = { ...remoteOnly, versionId: 'current-version', riskScore: 70 };

  const result = mergeDailySnapshotCacheForSave([localOnly], [remoteOnly], item);

  assert.deepEqual(result, [item, remoteOnly, localOnly]);
});

test('mergeInsightCacheForSave preserves remote rows and local-only rows', () => {
  const remoteOnly: XiaobaoRiskInsightCacheItem = { ...insightA, versionId: 'remote-version' };
  const localOnly: XiaobaoRiskInsightCacheItem = { ...insightA, versionId: 'local-version' };
  const item: XiaobaoRiskInsightCacheItem = { ...insightA, versionId: 'current-version' };

  const result = mergeInsightCacheForSave([localOnly], [remoteOnly], item);

  assert.deepEqual(result, [item, remoteOnly, localOnly]);
});
