/* @vitest-environment jsdom */

import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { beforeEach, expect, test, vi } from 'vitest';

import type { JumpHopSessionResponse } from '../../../packages/shared/src/contracts/jumpHop';
import { jumpHopClient } from '../../services/jump-hop/jumpHopClient';
import { JumpHopWorkspace } from './JumpHopWorkspace';

vi.mock('../../services/jump-hop/jumpHopClient', () => ({
  jumpHopClient: {
    createSession: vi.fn(),
  },
}));

const mockCreateSession = vi.mocked(jumpHopClient.createSession);

beforeEach(() => {
  mockCreateSession.mockReset();
});

function createSessionResponse(): JumpHopSessionResponse {
  return {
    session: {
      sessionId: 'jump-session-1',
      ownerUserId: 'user-1',
      status: 'draft',
      draft: null,
      createdAt: '2026-05-30T10:00:00.000Z',
      updatedAt: '2026-05-30T10:00:00.000Z',
    },
  };
}

test('jump hop workspace submits structured payload after required fields are filled', async () => {
  const user = userEvent.setup();
  const onSubmitted = vi.fn();
  const sessionResponse = createSessionResponse();
  mockCreateSession.mockResolvedValue(sessionResponse);

  render(
    <JumpHopWorkspace onBack={() => {}} onSubmitted={onSubmitted} />,
  );

  const submitButton = screen.getByRole('button', { name: '生成' });
  expect(submitButton).toHaveProperty('disabled', true);

  await user.type(screen.getByLabelText('作品标题'), '云朵跳台');
  await user.type(screen.getByLabelText('作品简介'), '在云端一路跳到星星。');
  await user.type(screen.getByLabelText('主题标签'), '云朵 星星');
  await user.selectOptions(screen.getByLabelText('难度'), 'standard');
  await user.selectOptions(screen.getByLabelText('风格'), 'paper-toy');
  await user.type(screen.getByLabelText('角色提示词'), '一只纸片小兔');
  await user.type(screen.getByLabelText('地块提示词'), '柔软云朵平台');
  await user.type(screen.getByLabelText('终点氛围'), '星光门');

  expect(submitButton).toHaveProperty('disabled', false);
  await user.click(submitButton);

  await waitFor(() => {
    expect(mockCreateSession).toHaveBeenCalledWith({
      templateId: 'jump-hop',
      workTitle: '云朵跳台',
      workDescription: '在云端一路跳到星星。',
      themeTags: ['云朵', '星星'],
      difficulty: 'standard',
      stylePreset: 'paper-toy',
      characterPrompt: '一只纸片小兔',
      tilePrompt: '柔软云朵平台',
      endMoodPrompt: '星光门',
    });
  });
  expect(onSubmitted).toHaveBeenCalledWith(
    sessionResponse,
    expect.objectContaining({
      templateId: 'jump-hop',
      workTitle: '云朵跳台',
    }),
  );
});

test('jump hop workspace calls back when return button is clicked', async () => {
  const user = userEvent.setup();
  const onBack = vi.fn();

  render(<JumpHopWorkspace onBack={onBack} onSubmitted={() => {}} />);

  await user.click(screen.getByRole('button', { name: '返回' }));

  expect(onBack).toHaveBeenCalledTimes(1);
});
