import React from 'react';
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import { FileExplorer } from '@/components/dms/FileExplorer';
import type { DmsFile } from '@/api/dms';

const mockFiles: DmsFile[] = [
  { id: 'f1', folder_id: null, name: 'document.pdf', mime_type: 'application/pdf', size_bytes: 1024, storage_path: '/path/f1', deleted_at: null, uploaded_by: 'user1', created_at: '2024-01-01T00:00:00Z', updated_at: '2024-01-02T00:00:00Z' },
  { id: 'f2', folder_id: null, name: 'image.png', mime_type: 'image/png', size_bytes: 2048, storage_path: '/path/f2', deleted_at: null, uploaded_by: 'user2', created_at: '2024-01-01T00:00:00Z', updated_at: '2024-01-03T00:00:00Z' },
];

const defaultProps = {
  files: mockFiles,
  selectedFileIds: new Set<string>(),
  selectedFile: null,
  viewMode: 'list' as const,
  onToggleSelect: vi.fn(),
  onFileClick: vi.fn(),
  onFileDoubleClick: vi.fn(),
  onFileShare: vi.fn(),
  onFileDelete: vi.fn(),
  onFilePreview: vi.fn(),
  sortBy: 'name' as const,
  sortOrder: 'asc' as const,
  onSortChange: vi.fn(),
};

describe('FileExplorer', () => {
  it('renders file list in list view', () => {
    render(<FileExplorer {...defaultProps} />);
    expect(screen.getByTestId('file-explorer-list')).toBeInTheDocument();
  });

  it('renders file rows for each file', () => {
    render(<FileExplorer {...defaultProps} />);
    expect(screen.getByTestId('file-row-f1')).toBeInTheDocument();
    expect(screen.getByTestId('file-row-f2')).toBeInTheDocument();
  });

  it('renders file names', () => {
    render(<FileExplorer {...defaultProps} />);
    expect(screen.getByText('document.pdf')).toBeInTheDocument();
    expect(screen.getByText('image.png')).toBeInTheDocument();
  });

  it('shows loading state', () => {
    render(<FileExplorer {...defaultProps} loading={true} />);
    expect(screen.getByTestId('file-explorer-loading')).toBeInTheDocument();
  });

  it('shows empty state when no files', () => {
    render(<FileExplorer {...defaultProps} files={[]} />);
    expect(screen.getByTestId('file-explorer-empty')).toBeInTheDocument();
  });

  it('calls onFileClick when file row clicked', () => {
    render(<FileExplorer {...defaultProps} />);
    fireEvent.click(screen.getByTestId('file-row-f1'));
    expect(defaultProps.onFileClick).toHaveBeenCalledWith(mockFiles[0]);
  });

  it('renders table view when viewMode is table', () => {
    render(<FileExplorer {...defaultProps} viewMode="table" />);
    expect(screen.getByTestId('file-explorer-table')).toBeInTheDocument();
  });

  it('renders icons view when viewMode is icons-md', () => {
    render(<FileExplorer {...defaultProps} viewMode="icons-md" />);
    expect(screen.getByTestId('file-explorer-icons-md')).toBeInTheDocument();
  });
});
