/* @vitest-environment jsdom */

import { render, screen } from '@testing-library/react';
import { describe, expect, test } from 'vitest';

import type { CustomWorldGenerationProgress } from '../../packages/shared/src/contracts/runtime';
import { CustomWorldGenerationView } from './CustomWorldGenerationView';

function createProgress(
  overrides: Partial<CustomWorldGenerationProgress> = {},
): CustomWorldGenerationProgress {
  return {
    phaseId: 'draft_foundation',
    phaseLabel: '整理草稿',
    phaseDetail: '正在整理当前生成步骤。',
    batchLabel: '第 2 批',
    overallProgress: 42,
    completedWeight: 21,
    totalWeight: 50,
    elapsedMs: 125_000,
    estimatedRemainingMs: 75_000,
    activeStepIndex: 1,
    steps: [
      {
        id: 'step-1',
        label: '收集设定',
        detail: '整理初始输入。',
        completed: 1,
        total: 1,
        status: 'completed',
      },
      {
        id: 'step-2',
        label: '编译草稿',
        detail: '生成首版结构。',
        completed: 2,
        total: 4,
        status: 'active',
      },
      {
        id: 'step-3',
        label: '写回结果',
        detail: '同步结果页。',
        completed: 0,
        total: 4,
        status: 'pending',
      },
    ],
    ...overrides,
  };
}

describe('CustomWorldGenerationView', () => {
  test.each(['拼图草稿生成进度', '抓大鹅草稿生成进度'])(
    'hides batch module and keeps wait/timer in one row for %s',
    (progressTitle) => {
      render(
        <CustomWorldGenerationView
          settingText="竖屏生成题材"
          progress={createProgress()}
          isGenerating
          error={null}
          onBack={() => {}}
          onEditSetting={() => {}}
          onRetry={() => {}}
          settingDescription={null}
          settingActionLabel={null}
          progressTitle={progressTitle}
        />,
      );

      expect(screen.queryByText('当前批次')).toBeNull();
      expect(screen.getByText('预计等待')).toBeTruthy();
      expect(screen.getByText('计时')).toBeTruthy();

      const statsNode = screen
        .getByText('预计等待')
        .closest('.custom-world-generation-stats');
      expect(statsNode?.className).toContain(
        'custom-world-generation-stats--two-column',
      );
      expect(statsNode?.getAttribute('style')).toContain(
        'grid-template-columns: repeat(2, minmax(0, 1fr))',
      );

      const stepNodes = [
        screen.getByText('收集设定'),
        screen.getByText('编译草稿'),
        screen.getByText('写回结果'),
      ].map((node) => node.closest('.custom-world-generation-step'));

      expect(stepNodes.every(Boolean)).toBe(true);
      expect(stepNodes[0]?.getAttribute('style')).toContain(
        '--generation-step-delay: 0ms',
      );
      expect(stepNodes[1]?.getAttribute('style')).toContain(
        '--generation-step-delay: 90ms',
      );
      expect(stepNodes[2]?.getAttribute('style')).toContain(
        '--generation-step-delay: 180ms',
      );
    },
  );

  test('keeps batch module for other generation pages', () => {
    render(
      <CustomWorldGenerationView
        settingText="大鱼吃小鱼题材"
        progress={createProgress()}
        isGenerating
        error={null}
        onBack={() => {}}
        onEditSetting={() => {}}
        onRetry={() => {}}
        settingDescription={null}
        settingActionLabel={null}
        progressTitle="大鱼吃小鱼草稿生成进度"
      />,
    );

    expect(screen.getByText('当前批次')).toBeTruthy();
    expect(
      screen
        .getByText('预计等待')
        .closest('.custom-world-generation-stats')
        ?.className,
    ).not.toContain('custom-world-generation-stats--two-column');
  });
});
