import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen } from '@testing-library/react';
import { AudioPlayer } from './AudioPlayer';
import { usePlayer } from '../hooks/usePlayer';

// Mock dependencies
vi.mock('../hooks/usePlayer', () => ({
  usePlayer: vi.fn(),
}));

describe('AudioPlayer', () => {
  const mockPlayer = {
    currentTrack: null,
    isPlaying: false,
    currentTime: 0,
    duration: 0,
    volume: 100,
    muted: false,
    queue: [],
    currentIndex: -1,
    repeat: 'off' as const,
    shuffle: false,
    play: vi.fn(),
    pause: vi.fn(),
    resume: vi.fn(),
    stop: vi.fn(),
    next: vi.fn(),
    previous: vi.fn(),
    seek: vi.fn(),
    setVolume: vi.fn(),
    toggleMute: vi.fn(),
    toggleShuffle: vi.fn(),
    setRepeat: vi.fn(),
    addToQueue: vi.fn(),
    clearQueue: vi.fn(),
  };

  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(usePlayer).mockReturnValue(mockPlayer);
  });

  it('should render audio player', () => {
    render(<AudioPlayer />);

    expect(screen.getByTestId('audio-player')).toBeInTheDocument();
  });

  it('should render audio element', () => {
    render(<AudioPlayer />);

    const audioElement = screen.getByTestId('audio-element');
    expect(audioElement).toBeInTheDocument();
    expect(audioElement.tagName).toBe('AUDIO');
  });

  it('should pass audio ref to usePlayer hook', () => {
    render(<AudioPlayer />);

    // usePlayer should be called with a ref object
    expect(usePlayer).toHaveBeenCalled();
    const callArgs = vi.mocked(usePlayer).mock.calls[0][0];
    expect(callArgs).toBeDefined();
    expect(callArgs?.current).toBeInstanceOf(HTMLAudioElement);
  });

  it('should display no track message when no track is selected', () => {
    render(<AudioPlayer />);

    expect(screen.getByText('Aucune piste sélectionnée')).toBeInTheDocument();
  });

  it('should display current track when track is playing', () => {
    const track = {
      id: 1,
      title: 'Test Track',
      duration: 180,
      url: 'https://example.com/track.mp3',
    };

    vi.mocked(usePlayer).mockReturnValue({
      ...mockPlayer,
      currentTrack: track,
    });

    render(<AudioPlayer />);

    expect(screen.getByText('Test Track')).toBeInTheDocument();
    expect(screen.getByText('En cours de lecture')).toBeInTheDocument();
  });

  it('should display artist when available', () => {
    const track = {
      id: 1,
      title: 'Test Track',
      artist: 'Test Artist',
      duration: 180,
      url: 'https://example.com/track.mp3',
    };

    vi.mocked(usePlayer).mockReturnValue({
      ...mockPlayer,
      currentTrack: track,
    });

    render(<AudioPlayer />);

    expect(screen.getByText('Test Artist')).toBeInTheDocument();
  });

  it('should apply custom className', () => {
    const { container } = render(<AudioPlayer className="custom-class" />);

    expect(container.firstChild).toHaveClass('custom-class');
  });

  it('should set preload attribute on audio element', () => {
    render(<AudioPlayer preload="auto" />);

    const audioElement = screen.getByTestId(
      'audio-element',
    ) as HTMLAudioElement;
    expect(audioElement.preload).toBe('auto');
  });

  it('should set autoplay attribute on audio element', () => {
    render(<AudioPlayer autoPlay={true} />);

    const audioElement = screen.getByTestId(
      'audio-element',
    ) as HTMLAudioElement;
    expect(audioElement.autoplay).toBe(true);
  });

  it('should have default preload value', () => {
    render(<AudioPlayer />);

    const audioElement = screen.getByTestId(
      'audio-element',
    ) as HTMLAudioElement;
    expect(audioElement.preload).toBe('metadata');
  });

  it('should have default autoplay value', () => {
    render(<AudioPlayer />);

    const audioElement = screen.getByTestId(
      'audio-element',
    ) as HTMLAudioElement;
    expect(audioElement.autoplay).toBe(false);
  });

  it('should hide audio element visually', () => {
    render(<AudioPlayer />);

    const audioElement = screen.getByTestId('audio-element');
    expect(audioElement).toHaveStyle({ display: 'none' });
  });
});
