import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import { afterEach, expect, test, vi } from 'vitest';
import { WorkspaceSideSheet } from './WorkspaceSideSheet';

afterEach(cleanup);

test('renders one accessible Semi configuration sheet with shared summary and actions', () => {
  const onCancel = vi.fn();
  const onReset = vi.fn();
  const onComplete = vi.fn();
  render(<WorkspaceSideSheet
    className="test-config-sheet"
    visible
    ariaLabel="测试策略配置"
    dialogId="test-strategy-sheet"
    title="策略设置"
    description="统一维护优先级与启停状态"
    badge="2 项已启用"
    summaryItems={[
      { label: '当前首选', value: '来源 A', tone: 'primary' },
      { label: '健康状态', value: '正常', detail: '实时上报', tone: 'success' }
    ]}
    footerNote="修改只在确认后生效"
    secondaryActions={[{ label: '恢复默认', onClick: onReset }]}
    primaryAction={{ label: '完成', onClick: onComplete }}
    onCancel={onCancel}
  >
    <label>优先级<input aria-label="优先级" /></label>
  </WorkspaceSideSheet>);

  const dialog = screen.getByRole('dialog', { name: '测试策略配置' });
  expect(dialog).toHaveAttribute('id', 'test-strategy-sheet');
  expect(dialog.closest('.semi-sidesheet')).toHaveClass('v2-workspace-config-sidesheet', 'test-config-sheet');
  expect(screen.getByText('统一维护优先级与启停状态')).toBeInTheDocument();
  expect(screen.getByRole('list', { name: '测试策略配置摘要' })).toHaveTextContent('当前首选来源 A健康状态正常实时上报');
  fireEvent.click(screen.getByRole('button', { name: '恢复默认' }));
  fireEvent.click(screen.getByRole('button', { name: '完成' }));
  fireEvent.click(screen.getByRole('button', { name: '关闭测试策略配置' }));
  expect(onReset).toHaveBeenCalledTimes(1);
  expect(onComplete).toHaveBeenCalledTimes(1);
  expect(onCancel).toHaveBeenCalledTimes(1);
});

test('exposes task, editor, help, action and filter variants without changing the accessible dialog contract', () => {
  const view = render(<>
    <WorkspaceSideSheet
      visible
      variant="task"
      ariaLabel="后台任务"
      title="任务"
      description="查看生成进度"
      onCancel={() => undefined}
    >
      <span>任务内容</span>
    </WorkspaceSideSheet>
    <WorkspaceSideSheet
      visible
      variant="editor"
      ariaLabel="策略编辑"
      title="编辑策略"
      description="保存后生效"
      onCancel={() => undefined}
    >
      <span>编辑内容</span>
    </WorkspaceSideSheet>
    <WorkspaceSideSheet
      visible
      variant="help"
      ariaLabel="页面帮助"
      title="帮助"
      description="查看当前模块说明"
      icon={<span data-testid="help-icon">?</span>}
      onCancel={() => undefined}
    >
      <span>帮助内容</span>
    </WorkspaceSideSheet>
    <WorkspaceSideSheet
      visible
      variant="action"
      ariaLabel="快捷操作"
      title="操作"
      description="选择下一步"
      onCancel={() => undefined}
    >
      <span>操作内容</span>
    </WorkspaceSideSheet>
    <WorkspaceSideSheet
      visible
      variant="filter"
      ariaLabel="范围筛选"
      title="筛选"
      description="应用后更新"
      onCancel={() => undefined}
    >
      <span>筛选内容</span>
    </WorkspaceSideSheet>
  </>);

  expect(screen.getByRole('dialog', { name: '后台任务' }).closest('.semi-sidesheet')).toHaveClass('v2-workspace-task-sidesheet');
  expect(screen.getByRole('dialog', { name: '策略编辑' }).closest('.semi-sidesheet')).toHaveClass('v2-workspace-editor-sidesheet');
  expect(screen.getByRole('dialog', { name: '页面帮助' }).closest('.semi-sidesheet')).toHaveClass('v2-workspace-help-sidesheet');
  expect(screen.getByRole('dialog', { name: '快捷操作' }).closest('.semi-sidesheet')).toHaveClass('v2-workspace-action-sidesheet');
  expect(screen.getByRole('dialog', { name: '范围筛选' }).closest('.semi-sidesheet')).toHaveClass('v2-workspace-filter-sidesheet');
  expect(screen.getByText('任务内容')).toBeInTheDocument();
  expect(screen.getByText('编辑内容')).toBeInTheDocument();
  expect(screen.getByText('帮助内容')).toBeInTheDocument();
  expect(screen.getByText('操作内容')).toBeInTheDocument();
  expect(screen.getByText('筛选内容')).toBeInTheDocument();
  expect(screen.getByTestId('help-icon').closest('.v2-workspace-config-title-icon')).toHaveAttribute('aria-hidden', 'true');
  expect(view.baseElement).toHaveTextContent('查看生成进度');
  expect(view.baseElement).toHaveTextContent('保存后生效');
});
