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

import { addVersionMembers, canWriteVersionWork, filterVersionMemberCandidates, inferVersionMemberRole } from './version-members';

test('filterVersionMemberCandidates searches people and excludes already added members', () => {
  const result = filterVersionMemberCandidates(
    [
      { id: 'm-1', name: 'Alice' },
      { id: 'm-2', name: 'Bob' },
      { id: 'm-3', name: 'Alicia' },
    ],
    [{ name: 'Alice', role: 'frontend' }],
    'ali',
  );

  assert.deepEqual(result.map((member) => member.name), ['Alicia']);
});

test('addVersionMembers appends selected people once with the default version role', () => {
  const result = addVersionMembers(
    [{ name: 'Alice', role: 'product' }],
    ['Bob', 'Alicia', 'Alice'],
    [
      { id: 'm-1', name: 'Alice' },
      { id: 'm-2', name: 'Bob' },
      { id: 'm-3', name: 'Alicia' },
    ],
  );

  assert.deepEqual(result, [
    { name: 'Alice', role: 'product' },
    { name: 'Bob', role: 'frontend' },
    { name: 'Alicia', role: 'frontend' },
  ]);
});

test('inferVersionMemberRole maps department names to project roles', () => {
  assert.equal(inferVersionMemberRole({ id: 'm-1', name: 'Alice', departmentName: '产品部' }), 'product');
  assert.equal(inferVersionMemberRole({ id: 'm-2', name: 'Bob', departmentName: '设计部' }), 'ui');
  assert.equal(inferVersionMemberRole({ id: 'm-3', name: 'Cathy', departmentName: '前端组' }), 'frontend');
  assert.equal(inferVersionMemberRole({ id: 'm-4', name: 'David', departmentName: '后端组' }), 'backend');
  assert.equal(inferVersionMemberRole({ id: 'm-5', name: 'Eve', departmentName: '测试组' }), 'testing');
  assert.equal(inferVersionMemberRole({ id: 'm-6', name: 'Frank', departmentName: '运营部' }), 'frontend');
});

test('addVersionMembers infers each selected person role from department', () => {
  const result = addVersionMembers(
    [],
    ['Product', 'Designer', 'Backend', 'QA'],
    [
      { id: 'm-1', name: 'Product', departmentName: '产品部' },
      { id: 'm-2', name: 'Designer', departmentName: 'UI设计部' },
      { id: 'm-3', name: 'Backend', departmentName: '后端组' },
      { id: 'm-4', name: 'QA', departmentName: '质量测试部' },
    ],
  );

  assert.deepEqual(result, [
    { name: 'Product', role: 'product' },
    { name: 'Designer', role: 'ui' },
    { name: 'Backend', role: 'backend' },
    { name: 'QA', role: 'testing' },
  ]);
});

test('canWriteVersionWork requires role permission and version membership', () => {
  const devRole = { id: 'role-dev', name: '开发', permissions: ['version.devtask:manage'] } as any;
  const testRole = { id: 'role-test', name: '测试', permissions: ['version.testcase:manage'] } as any;
  const adminRole = { id: 'role-admin', name: '超管', permissions: ['*'] } as any;
  const versionMembers = [{ name: 'Dev One', role: 'frontend' as const }];

  assert.equal(canWriteVersionWork({
    role: devRole,
    permission: 'version.devtask:manage',
    currentUserName: 'Dev One',
    versionMembers,
  }), true);

  assert.equal(canWriteVersionWork({
    role: devRole,
    permission: 'version.devtask:manage',
    currentUserName: 'Outside Dev',
    versionMembers,
  }), false);

  assert.equal(canWriteVersionWork({
    role: testRole,
    permission: 'version.devtask:manage',
    currentUserName: 'Dev One',
    versionMembers,
  }), false);

  assert.equal(canWriteVersionWork({
    role: devRole,
    permission: 'version.devtask:manage',
    currentUserName: 'Dev One',
    versionMembers: [],
  }), false);

  assert.equal(canWriteVersionWork({
    role: adminRole,
    permission: 'version.devtask:manage',
    currentUserName: 'Admin',
    versionMembers: [],
  }), true);
});
