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

const mockFile: DmsFile = {
  id: 'f1',
  folder_id: 'folder1',
  name: 'document.pdf',
  mime_type: 'application/pdf',
  size_bytes: 1024,
  storage_path: '/path/f1',
  deleted_at: null,
  uploaded_by: 'user1',
  created_at: '2024-01-01T10:00:00Z',
  updated_at: '2024-01-02T12:00:00Z',
  access_level: 'read',
};

const defaultProps = {
  file: mockFile,
  onPreview: vi.fn(),
  onShare: vi.fn(),
  onDelete: vi.fn(),
  onClose: vi.fn(),
};

describe('FileDetails', () => {
  it('renders file details container', () => {
    render(<FileDetails {...defaultProps} />);
    expect(screen.getByTestId('file-details')).toBeInTheDocument();
  });

  it('renders file name in header', () => {
    render(<FileDetails {...defaultProps} />);
    expect(screen.getAllByText('document.pdf').length).toBeGreaterThan(0);
  });

  it('renders empty state when no file selected', () => {
    render(<FileDetails {...defaultProps} file={null} />);
    expect(screen.getByTestId('file-details-empty')).toBeInTheDocument();
  });

  it('renders close button', () => {
    render(<FileDetails {...defaultProps} />);
    expect(screen.getByLabelText(/close|schließen/i)).toBeInTheDocument();
  });

  it('calls onClose when close button clicked', () => {
    render(<FileDetails {...defaultProps} />);
    fireEvent.click(screen.getByLabelText(/close|schließen/i));
    expect(defaultProps.onClose).toHaveBeenCalled();
  });

  it('renders preview button', () => {
    render(<FileDetails {...defaultProps} />);
    const previewBtn = screen.getByText(/preview|vorschau/i);
    expect(previewBtn).toBeInTheDocument();
  });

  it('calls onPreview when preview button clicked', () => {
    render(<FileDetails {...defaultProps} />);
    fireEvent.click(screen.getByText(/preview|vorschau/i));
    expect(defaultProps.onPreview).toHaveBeenCalledWith(mockFile);
  });

  it('renders delete button', () => {
    render(<FileDetails {...defaultProps} />);
    expect(screen.getByText(/delete|löschen/i)).toBeInTheDocument();
  });

  it('calls onDelete when delete button clicked', () => {
    render(<FileDetails {...defaultProps} />);
    fireEvent.click(screen.getByText(/delete|löschen/i));
    expect(defaultProps.onDelete).toHaveBeenCalledWith(mockFile);
  });
});
