import { fireEvent, render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { describe, expect, it } from 'vitest';

import { AppSidebar } from '@/components/layout/AppSidebar';

describe('AppSidebar', () => {
  function renderSidebar(initialEntry: string) {
    return render(
      <MemoryRouter initialEntries={[initialEntry]}>
        <AppSidebar />
      </MemoryRouter>,
    );
  }

  it('shows only the active directory drawer and closes the previous one when switching', () => {
    renderSidebar('/standardized?tab=failed');

    const standardizationLink = screen.getByRole('link', { name: 'AI标准化' });

    expect(standardizationLink).toBeInTheDocument();
    expect(standardizationLink).toHaveAttribute('aria-expanded', 'true');
    expect(screen.getByRole('link', { name: '标准化成功数据' })).toHaveAttribute('href', '/standardized');
    expect(screen.getByRole('link', { name: '标准化失败数据' })).toHaveAttribute('href', '/standardized?tab=failed');
    expect(screen.getByRole('link', { name: '标准化任务状态' })).toHaveAttribute('href', '/standardized?tab=tasks');
    expect(screen.queryByRole('link', { name: '搜索结果' })).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: '话术配置' })).not.toBeInTheDocument();

    fireEvent.click(standardizationLink);

    expect(standardizationLink).toHaveAttribute('aria-expanded', 'false');
    expect(screen.queryByRole('link', { name: '标准化成功数据' })).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: '标准化失败数据' })).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: '标准化任务状态' })).not.toBeInTheDocument();

    fireEvent.click(standardizationLink);

    expect(standardizationLink).toHaveAttribute('aria-expanded', 'true');
    expect(screen.getByRole('link', { name: '标准化成功数据' })).toHaveAttribute('href', '/standardized');

    fireEvent.click(screen.getByRole('link', { name: '智能搜索' }));

    expect(screen.getByRole('link', { name: '搜索结果' })).toHaveAttribute('href', '/search-results');
    expect(screen.getByRole('link', { name: '搜索任务状态' })).toHaveAttribute('href', '/search-results?tab=tasks');
    expect(screen.queryByRole('link', { name: '标准化成功数据' })).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: '话术配置' })).not.toBeInTheDocument();

    fireEvent.click(screen.getByRole('link', { name: '采购清单' }));

    expect(screen.queryByRole('link', { name: '搜索结果' })).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: '搜索任务状态' })).not.toBeInTheDocument();

    fireEvent.click(screen.getByRole('link', { name: '智能询价' }));

    expect(screen.getByRole('link', { name: '话术配置' })).toHaveAttribute('href', '/smart-inquiry');
    expect(screen.getByRole('link', { name: '询价任务' })).toHaveAttribute('href', '/smart-inquiry?tab=tasks');
    expect(screen.queryByRole('link', { name: '标准化成功数据' })).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: '搜索结果' })).not.toBeInTheDocument();
  });

  it('highlights the active standardization child from the tab query', () => {
    renderSidebar('/standardized?tab=tasks');

    expect(screen.getByRole('link', { name: '标准化任务状态' })).toHaveClass('bg-blue-50', 'text-blue-700');
    expect(screen.getByRole('link', { name: '标准化成功数据' })).not.toHaveClass('bg-blue-50');
    expect(screen.getByRole('link', { name: '标准化失败数据' })).not.toHaveClass('bg-blue-50');
  });

  it('highlights the active search-results child from tab query params', () => {
    renderSidebar('/search-results?tab=tasks&batch_id=CG-20260420-001');

    expect(screen.getByRole('link', { name: '搜索任务状态' })).toHaveClass('bg-blue-50', 'text-blue-700');
    expect(screen.getByRole('link', { name: '搜索结果' })).not.toHaveClass('bg-blue-50');
  });

  it('highlights the active smart-inquiry child from the tab query', () => {
    renderSidebar('/smart-inquiry?tab=tasks');

    expect(screen.getByRole('link', { name: '询价任务' })).toHaveClass('bg-blue-50', 'text-blue-700');
    expect(screen.getByRole('link', { name: '话术配置' })).not.toHaveClass('bg-blue-50');
  });
});
