import { buildAppDataSeed } from './seed-data';

function byKey(key: string) {
  const entry = buildAppDataSeed().find((item) => item.key === key);
  if (!entry) throw new Error(`Missing seed key: ${key}`);
  return entry.value as any;
}

describe('clean app data seed', () => {
  it('clears business, workspace, warning, and overtime records', () => {
    const emptyArrayKeys = [
      'version-plans',
      'dev-tasks',
      'test-cases',
      'bugs',
      'task-worklogs',
      'work-activities',
      'xiaobao-risk-insights',
      'xiaobao-risk-snapshots',
      'xiaobao-warning-views',
      'task-categories',
    ];

    for (const key of emptyArrayKeys) {
      expect(byKey(key)).toEqual([]);
    }

    expect(byKey('requirements').requirements).toEqual([]);
    expect(byKey('overtime').records).toEqual([]);
  });

  it('seeds deduped customer source targets split from the customer spreadsheet', () => {
    const requirements = byKey('requirements');
    const targets = requirements.sourceTargets;
    const names = targets.map((target: { name: string }) => target.name);

    expect(targets).toHaveLength(67);
    expect(targets.every((target: { sourceType: string }) => target.sourceType === 'customer')).toBe(true);
    expect(new Set(names.map((name: string) => name.trim().toLowerCase())).size).toBe(names.length);
    expect(names.some((name: string) => name.includes(',') || name.includes('，'))).toBe(false);
    expect(names.slice(0, 5)).toEqual(['方砖厂', '全部客户', '晓秧锅', '老船长', '失眠烧烤']);
    expect(names).toContain('秦门西安小馆');
    expect(names.at(-1)).toBe('杨记兴');
  });

  it('creates the default FTB product with deduped projects from the spreadsheet', () => {
    const products = byKey('products-overview');

    expect(products).toHaveLength(1);
    expect(products[0]).toEqual(
      expect.objectContaining({
        id: 'product-ftb-app',
        name: '翻台宝应用',
      }),
    );
    expect(products[0].projects).toHaveLength(56);
    expect(products[0]._count.projects).toBe(56);
    expect(products[0].versions).toEqual([]);

    const names = products[0].projects.map((project: { name: string }) => project.name);
    expect(new Set(names.map((name: string) => name.trim().toLowerCase())).size).toBe(names.length);
    expect(names.slice(0, 5)).toEqual(['通知公告', '产品库', 'AI帮助', '读书', '工作计划']);
    expect(names.at(-1)).toBe('增长管理');
  });

  it('keeps dictionaries and role configuration while only retaining the super admin member', () => {
    const requirements = byKey('requirements');
    const overtime = byKey('overtime');
    const members = byKey('members');

    expect(requirements.types.length).toBeGreaterThan(0);
    expect(requirements.platforms.length).toBeGreaterThan(0);
    expect(overtime.reasons.length).toBeGreaterThan(0);

    expect(members.members).toEqual([
      expect.objectContaining({
        id: 'm-8',
        name: '超级管理员',
        username: 'admin',
        email: '',
        departmentId: '',
        roleId: 'role-admin',
        isSystem: true,
        password: 'Ftb@2024',
      }),
    ]);
    expect(members.roles.map((role: { id: string }) => role.id)).toEqual([
      'role-admin',
      'role-pm',
      'role-lead',
      'role-dev',
      'role-test',
      'role-design',
    ]);
    expect(members.passwordRule).toBeTruthy();
  });
});
