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

import {
  getDefaultActiveKey,
  getReportViewHeaderMode,
  hasCustomPrintTab,
  normalizeReportViewConfig,
  shouldEnablePrintAction,
  validateReportViewConfig,
} from '../helpers/reportView';
import type { ReportViewConfig } from '../types';

const formDetailTab = {
  id: 'form-id',
  key: 'formDetail',
  modelId: 'model-id',
} as const;

const customPrintTab = {
  key: 'customPrint',
  print: {
    data: { id: 'record-id' },
    template: 'template-id',
  },
} as const;

const attachmentsTab = {
  key: 'attachments',
} as const;

describe('reportView helpers', () => {
  it('rejects config without tabs', () => {
    expect(() => validateReportViewConfig({ tabs: [] })).toThrow('查看报告至少需要一个 tab');
    expect(() => validateReportViewConfig({} as any)).toThrow('查看报告至少需要一个 tab');
  });

  it('uses first tab key as default active key', () => {
    expect(getDefaultActiveKey({ tabs: [formDetailTab, customPrintTab] })).toBe('formDetail');
  });

  it('keeps configured active key when it exists in tabs', () => {
    expect(getDefaultActiveKey({ activeKey: 'customPrint', tabs: [formDetailTab, customPrintTab] })).toBe('customPrint');
  });

  it('normalizes default title and tab titles', () => {
    const normalized = normalizeReportViewConfig({
      tabs: [formDetailTab, customPrintTab, attachmentsTab],
    });

    expect(normalized.title).toBe('查看报告');
    expect(normalized.tabs).toEqual([
      { ...formDetailTab, internalKey: 'formDetail:0', title: '表单详情' },
      { ...customPrintTab, internalKey: 'customPrint:1', title: '报告' },
      { ...attachmentsTab, internalKey: 'attachments:2', title: '附件' },
    ]);
  });

  it('normalizes duplicate form detail tabs with unique internal keys', () => {
    const normalized = normalizeReportViewConfig({
      activeKey: 'formDetail',
      tabs: [
        { ...formDetailTab, id: 'form-id-1', title: '委托信息' },
        { ...formDetailTab, id: 'form-id-2', title: '检验记录' },
      ],
    });

    expect(normalized.activeKey).toBe('formDetail:0');
    expect(normalized.tabs).toEqual([
      { ...formDetailTab, id: 'form-id-1', internalKey: 'formDetail:0', title: '委托信息' },
      { ...formDetailTab, id: 'form-id-2', internalKey: 'formDetail:1', title: '检验记录' },
    ]);
  });

  it('uses form detail id as active key when selecting a duplicate form detail tab', () => {
    const normalized = normalizeReportViewConfig({
      activeKey: 'form-id-2',
      tabs: [
        { ...formDetailTab, id: 'form-id-1', title: '委托信息' },
        { ...formDetailTab, id: 'form-id-2', title: '检验记录' },
      ],
    });

    expect(normalized.activeKey).toBe('formDetail:1');
  });

  it('selects print header mode only when actions is not provided', () => {
    expect(getReportViewHeaderMode({ tabs: [customPrintTab] })).toBe('print');
    expect(getReportViewHeaderMode({ actions: [], tabs: [customPrintTab] })).toBe('actions');

    const config: ReportViewConfig = {
      actions: [
        {
          key: 'approve',
          label: '批准',
          onClick: () => undefined,
        },
      ],
      tabs: [customPrintTab],
    };

    expect(getReportViewHeaderMode(config)).toBe('actions');
  });

  it('detects custom print tab', () => {
    expect(hasCustomPrintTab({ tabs: [formDetailTab] })).toBe(false);
    expect(hasCustomPrintTab({ tabs: [formDetailTab, customPrintTab] })).toBe(true);
  });

  it('enables print action only when current active key is customPrint and customPrint tab exists', () => {
    expect(shouldEnablePrintAction({ tabs: [formDetailTab, customPrintTab] }, 'formDetail')).toBe(false);
    expect(shouldEnablePrintAction({ tabs: [customPrintTab] }, 'customPrint')).toBe(true);
    expect(shouldEnablePrintAction({ tabs: [formDetailTab] }, 'customPrint')).toBe(false);
  });
});
