import { describe, expect, it } from 'vitest';

import {
  buildSkillImportConfirmPayload,
  countBlockingImportIssues,
  invalidateConsumedImportPreview,
  resolveSkillImportConflictReasonLabel,
  resolveSkillImportStep,
} from './skill-import';

describe('skill import confirmation', () => {
  it('confirms with the preview token and never requires the uploaded file again', () => {
    const payload = buildSkillImportConfirmPayload(
      {
        format: 'EASYFLOW',
        importToken: 'one-time-token',
        skills: [{ conflict: false, name: 'research', packageId: 'pkg-1' }],
      },
      {
        categoryId: 12,
        conflictStrategy: 'RENAME',
        renames: { 'pkg-1': 'research-copy' },
      },
      [
        {
          bindingKey: 'binding-1',
          capabilityType: 'WORKFLOW',
          disabled: false,
          status: 'RESOLVED',
          targetId: 42,
        },
      ],
    );

    expect(payload.importToken).toBe('one-time-token');
    expect(payload).not.toHaveProperty('file');
    expect(payload.capabilityMappings).toEqual([
      { bindingKey: 'binding-1', disabled: false, targetId: 42 },
    ]);
  });

  it('blocks package and item-level validation errors', () => {
    expect(
      countBlockingImportIssues({
        format: 'STANDARD',
        importToken: 'token',
        issues: [{ message: 'unsafe zip', severity: 'ERROR' }],
        skills: [
          {
            conflict: false,
            name: 'research',
            packageId: 'pkg-1',
            validationIssues: [{ message: 'missing name', severity: 'ERROR' }],
          },
        ],
      }),
    ).toBe(2);
  });

  it('removes an auto-resolved target when the mapping is explicitly disabled', () => {
    const payload = buildSkillImportConfirmPayload(
      {
        format: 'EASYFLOW',
        importToken: 'one-time-token',
        skills: [{ conflict: false, name: 'research', packageId: 'pkg-1' }],
      },
      { conflictStrategy: 'REJECT' },
      [
        {
          bindingKey: 'binding-1',
          capabilityType: 'WORKFLOW',
          disabled: true,
          status: 'RESOLVED',
          targetId: 42,
        },
      ],
    );

    expect(payload.capabilityMappings).toEqual([
      { bindingKey: 'binding-1', disabled: true, targetId: undefined },
    ]);
  });

  it('invalidates a consumed preview token after any confirm failure', () => {
    const preview = {
      format: 'STANDARD' as const,
      importToken: 'one-time-token',
      skills: [{ conflict: false, name: 'research', packageId: 'pkg-1' }],
    };

    expect(invalidateConsumedImportPreview(preview)).toEqual({
      ...preview,
      importToken: '',
    });
    expect(preview.importToken).toBe('one-time-token');
  });

  it('redacts inaccessible and unknown conflict reasons as name unavailable', () => {
    expect(resolveSkillImportConflictReasonLabel('NAME_UNAVAILABLE')).toBe(
      '名称不可用',
    );
    expect(resolveSkillImportConflictReasonLabel('NO_PERMISSION')).toBe(
      '名称不可用',
    );
    expect(resolveSkillImportConflictReasonLabel('NOT_DRAFT')).toBe(
      '当前状态不可覆盖',
    );
  });

  it('skips the empty capability step for a standard package', () => {
    expect(resolveSkillImportStep(0, 'next', false)).toBe(2);
    expect(resolveSkillImportStep(2, 'visible', false)).toBe(1);
    expect(resolveSkillImportStep(2, 'previous', false)).toBe(0);
    expect(resolveSkillImportStep(0, 'next', true)).toBe(1);
  });
});
