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

import {
  buildWorkflowFormParameterSummaries,
  buildWorkflowFormSubmissionImages,
  buildWorkflowFormSubmissionText,
  hasRequiredWorkflowFormParameters,
} from '../workflowFormPresentation';

describe('workflowFormPresentation', () => {
  it('only requires the opening form when a required parameter exists', () => {
    expect(
      hasRequiredWorkflowFormParameters([
        { name: 'optional', required: false },
      ]),
    ).toBe(false);
    expect(
      hasRequiredWorkflowFormParameters([
        { name: 'optional', required: false },
        { name: 'required', required: true },
      ]),
    ).toBe(true);
  });

  it('shows entered form values using their configured parameter labels', () => {
    expect(
      buildWorkflowFormSubmissionText(
        [
          { name: '补充信息', formLabel: '补充信息' },
          { name: '附件', formLabel: '附件' },
          { name: '未填写', formLabel: '未填写' },
        ],
        {
          补充信息: '项目背景',
          附件: [{ fileName: '需求说明.pdf' }, { fileName: '接口定义.docx' }],
          未填写: '',
        },
      ),
    ).toBe('补充信息：项目背景\n附件：需求说明.pdf、接口定义.docx');
  });

  it('renders image fields as image attachments instead of filename text', () => {
    const parameters = [
      { name: '图片', formLabel: '图片', contentType: 'image' },
      { name: '说明', formLabel: '说明', contentType: 'text' },
    ];
    const values = {
      图片: {
        sourceType: 'upload',
        fileName: '界面截图.png',
        filePath: '/uploads/interface.png',
        contentType: 'image/png',
        size: 1024,
      },
      说明: '请检查布局',
    };

    expect(buildWorkflowFormSubmissionText(parameters, values)).toBe(
      '说明：请检查布局',
    );
    expect(buildWorkflowFormSubmissionImages(parameters, values)).toEqual([
      {
        mimeType: 'image/png',
        name: '界面截图.png',
        previewUrl: '/uploads/interface.png',
        size: 1024,
        status: 'ready',
      },
    ]);
  });

  it('builds compact summaries for configured workflow parameters', () => {
    expect(
      buildWorkflowFormParameterSummaries(
        [
          { name: 'customer', formLabel: '客户名称', required: true },
          { name: 'scene', formLabel: '业务场景', required: false },
          { name: 'files', formLabel: '需求附件', required: true },
        ],
        {
          customer: '华北分公司',
          scene: '',
          files: [{ fileName: '需求说明.pdf' }],
        },
      ),
    ).toEqual([
      {
        key: 'customer',
        label: 'customer',
        ready: true,
        required: true,
        value: '华北分公司',
      },
      {
        key: 'scene',
        label: 'scene',
        ready: false,
        required: false,
        value: '待填写',
      },
      {
        key: 'files',
        label: 'files',
        ready: true,
        required: true,
        value: '需求说明.pdf',
      },
    ]);
  });
});
