import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { vi } from 'vitest';
import { PrinterManagement } from '../PrinterManagement';

// Mock react-query with factory functions
vi.mock('@tanstack/react-query', () => {
  const mockUseQuery = vi.fn();
  const mockUseMutation = vi.fn();
  const mockUseQueryClient = vi.fn();

  return {
    useQuery: mockUseQuery,
    useMutation: mockUseMutation,
    useQueryClient: mockUseQueryClient,
    __mockUseQuery: mockUseQuery,
    __mockUseMutation: mockUseMutation,
    __mockUseQueryClient: mockUseQueryClient,
  };
});

// Mock API client
vi.mock('../../api/feiePrinterClient', () => ({
  createFeiePrinterClient: vi.fn(() => ({
    setAuthToken: vi.fn(),
    setTenantId: vi.fn(),
    getPrinters: vi.fn(),
    addPrinter: vi.fn(),
    updatePrinter: vi.fn(),
    deletePrinter: vi.fn(),
    setDefaultPrinter: vi.fn(),
    getPrinterStatus: vi.fn(),
  })),
}));

// Mock sonner
vi.mock('sonner', () => ({
  toast: {
    success: vi.fn(),
    error: vi.fn(),
    info: vi.fn(),
    warning: vi.fn(),
  },
}));

// Import mocked modules after mocking
import * as reactQuery from '@tanstack/react-query';

describe('PrinterManagement', () => {
  const defaultProps = {
    tenantId: 1,
    authToken: 'test-token',
  };

  beforeEach(() => {
    vi.clearAllMocks();

    // Setup default mock implementations
    (reactQuery as any).__mockUseQuery.mockReturnValue({
      data: null,
      isLoading: false,
      isError: false,
      refetch: vi.fn(),
    });

    (reactQuery as any).__mockUseMutation.mockReturnValue({
      mutate: vi.fn(),
      isPending: false,
    });

    (reactQuery as any).__mockUseQueryClient.mockReturnValue({
      invalidateQueries: vi.fn(),
    });
  });

  it('renders without crashing', () => {
    render(<PrinterManagement {...defaultProps} />);
    expect(screen.getByText('打印机管理')).toBeInTheDocument();
  });

  it('displays loading state', () => {
    (reactQuery as any).__mockUseQuery.mockReturnValue({
      data: null,
      isLoading: true,
      isError: false,
      refetch: vi.fn(),
    });

    render(<PrinterManagement {...defaultProps} />);
    expect(screen.getByText('加载中...')).toBeInTheDocument();
  });

  it('displays error state', () => {
    (reactQuery as any).__mockUseQuery.mockReturnValue({
      data: null,
      isLoading: false,
      isError: true,
      refetch: vi.fn(),
    });

    render(<PrinterManagement {...defaultProps} />);
    expect(screen.getByText('加载打印机列表失败')).toBeInTheDocument();
  });

  it('displays empty state when no printers', () => {
    (reactQuery as any).__mockUseQuery.mockReturnValue({
      data: { data: [], total: 0, page: 1, pageSize: 10 },
      isLoading: false,
      isError: false,
      refetch: vi.fn(),
    });

    render(<PrinterManagement {...defaultProps} />);
    expect(screen.getByText('暂无打印机')).toBeInTheDocument();
  });

  it('handles search input change', () => {
    render(<PrinterManagement {...defaultProps} />);
    const searchInput = screen.getByPlaceholderText('搜索打印机名称或序列号...');
    fireEvent.change(searchInput, { target: { value: 'test search' } });
    expect(searchInput).toHaveValue('test search');
  });
});