import { afterEach, describe, expect, test, vi } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/svelte';
import { emptyLikesMock } from '../../test-utils/mocks/likes';
import { emptyPlaylistsMock } from '../../test-utils/mocks/playlists';
import type { TrackRef } from '$lib/api/types';
import { makeTracks } from '$test-utils/fixtures/track';

vi.mock('$app/navigation', () => ({ goto: vi.fn() }));

vi.mock('$lib/api/likes', () => emptyLikesMock());

vi.mock('$lib/api/artists', () => ({
  listArtistTracks: vi.fn().mockResolvedValue([])
}));

vi.mock('$lib/api/playlists', () => emptyPlaylistsMock());

vi.mock('$lib/player/store.svelte', () => ({
  enqueueTracks: vi.fn()
}));

import ArtistMenu from './ArtistMenu.svelte';
import { enqueueTracks } from '$lib/player/store.svelte';
import { listArtistTracks } from '$lib/api/artists';
import { likeEntity, unlikeEntity } from '$lib/api/likes';

const TRACKS: TrackRef[] = makeTracks(2, {
  album_id: 'al1', artist_id: 'a1', artist_name: 'Test'
});

afterEach(() => {
  vi.clearAllMocks();
});

describe('ArtistMenu', () => {
  test('kebab opens menu with action items', async () => {
    render(ArtistMenu, { props: { artistId: 'a1', artistName: 'Test', tracks: TRACKS } });
    const kebab = screen.getByRole('button', { name: /artist actions for test/i });
    await fireEvent.click(kebab);
    expect(await screen.findByText(/Like artist/i)).toBeInTheDocument();
    expect(screen.getByText(/Add all to queue/i)).toBeInTheDocument();
    expect(screen.getByText(/Add all to playlist/i)).toBeInTheDocument();
  });

  test('Add all to queue uses cached tracks when provided', async () => {
    render(ArtistMenu, { props: { artistId: 'a1', artistName: 'Test', tracks: TRACKS } });
    await fireEvent.click(screen.getByRole('button', { name: /artist actions for test/i }));
    await fireEvent.click(screen.getByText(/Add all to queue/i));
    expect(enqueueTracks).toHaveBeenCalledWith(TRACKS);
    expect(listArtistTracks).not.toHaveBeenCalled();
  });

  test('Add all to queue lazily fetches when no cached tracks', async () => {
    (listArtistTracks as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce(TRACKS);
    render(ArtistMenu, { props: { artistId: 'a1', artistName: 'Test' } });
    await fireEvent.click(screen.getByRole('button', { name: /artist actions for test/i }));
    await fireEvent.click(screen.getByText(/Add all to queue/i));
    await waitFor(() => expect(enqueueTracks).toHaveBeenCalledWith(TRACKS));
    expect(listArtistTracks).toHaveBeenCalledWith('a1');
  });

  test('Like item calls likeEntity', async () => {
    render(ArtistMenu, { props: { artistId: 'a1', artistName: 'Test', tracks: TRACKS } });
    await fireEvent.click(screen.getByRole('button', { name: /artist actions for test/i }));
    await fireEvent.click(screen.getByText(/Like artist/i));
    expect(likeEntity).toHaveBeenCalled();
  });

  test('Escape key closes the menu', async () => {
    render(ArtistMenu, { props: { artistId: 'a1', artistName: 'Test', tracks: TRACKS } });
    await fireEvent.click(screen.getByRole('button', { name: /artist actions for test/i }));
    expect(screen.getByRole('menu')).toBeInTheDocument();
    await fireEvent.keyDown(window, { key: 'Escape' });
    expect(screen.queryByRole('menu')).not.toBeInTheDocument();
  });
});
