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

const mockFolders: DmsFolder[] = [
  { id: 'folder1', name: 'Documents', parent_id: null, created_by: 'user1', created_at: '2024-01-01T00:00:00Z', updated_at: '2024-01-01T00:00:00Z', file_count: 5 },
  { id: 'folder2', name: 'Images', parent_id: null, created_by: 'user1', created_at: '2024-01-01T00:00:00Z', updated_at: '2024-01-01T00:00:00Z', file_count: 3 },
];

const mockSharedFiles: DmsFile[] = [
  { id: 'sf1', folder_id: null, name: 'shared.doc', mime_type: 'application/msword', size_bytes: 512, storage_path: '/path/sf1', deleted_at: null, uploaded_by: 'other', created_at: '2024-01-01T00:00:00Z' },
];

const defaultProps = {
  folders: mockFolders,
  sharedFiles: mockSharedFiles,
  selectedFolderId: null,
  selectedSource: null as string | null,
  onSelectFolder: vi.fn(),
  onSelectSource: vi.fn(),
  onDropFiles: vi.fn(),
  onDropFolder: vi.fn(),
};

describe('SourceTree', () => {
  it('renders source tree container', () => {
    render(<SourceTree {...defaultProps} />);
    expect(screen.getByTestId('source-tree')).toBeInTheDocument();
  });

  it('renders folder names', () => {
    render(<SourceTree {...defaultProps} />);
    expect(screen.getByText('Documents')).toBeInTheDocument();
    expect(screen.getByText('Images')).toBeInTheDocument();
  });

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

  it('calls onSelectFolder when folder clicked', () => {
    render(<SourceTree {...defaultProps} />);
    fireEvent.click(screen.getByText('Documents'));
    expect(defaultProps.onSelectFolder).toHaveBeenCalled();
  });

  it('calls onSelectSource when source clicked', () => {
    render(<SourceTree {...defaultProps} />);
    // Click on "All Files" entry which calls onSelectSource('local')
    const allFilesEntry = screen.getByText(/all files|alle dateien/i);
    if (allFilesEntry) {
      fireEvent.click(allFilesEntry);
      expect(defaultProps.onSelectSource).toHaveBeenCalled();
    }
  });
});
