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/albums', async (orig) => {
  const actual = (await orig()) as Record<string, unknown>;
  return { ...actual, listAlbumTracks: vi.fn().mockResolvedValue([]) };
});

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

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

import AlbumMenu from './AlbumMenu.svelte';
import { enqueueTracks } from '$lib/player/store.svelte';
import { listAlbumTracks } from '$lib/api/albums';
import { likeEntity } from '$lib/api/likes';
import { goto } from '$app/navigation';

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

const PROPS = { albumId: 'al1', albumTitle: 'Disc', artistId: 'a1', artistName: 'Test' };

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

describe('AlbumMenu', () => {
  test('kebab opens menu with action items', async () => {
    render(AlbumMenu, { props: { ...PROPS, tracks: TRACKS } });
    await fireEvent.click(screen.getByRole('button', { name: /album actions for disc/i }));
    expect(await screen.findByText(/Like album/i)).toBeInTheDocument();
    expect(screen.getByText(/Add to queue/i)).toBeInTheDocument();
    expect(screen.getByText(/Add to playlist/i)).toBeInTheDocument();
    expect(screen.getByText(/Go to artist/i)).toBeInTheDocument();
  });

  test('Add to queue uses cached tracks when provided', async () => {
    render(AlbumMenu, { props: { ...PROPS, tracks: TRACKS } });
    await fireEvent.click(screen.getByRole('button', { name: /album actions for disc/i }));
    await fireEvent.click(screen.getByText(/Add to queue/i));
    expect(enqueueTracks).toHaveBeenCalledWith(TRACKS);
    expect(listAlbumTracks).not.toHaveBeenCalled();
  });

  test('Add to queue lazily fetches when no cached tracks', async () => {
    (listAlbumTracks as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce(TRACKS);
    render(AlbumMenu, { props: PROPS });
    await fireEvent.click(screen.getByRole('button', { name: /album actions for disc/i }));
    await fireEvent.click(screen.getByText(/Add to queue/i));
    await waitFor(() => expect(enqueueTracks).toHaveBeenCalledWith(TRACKS));
    expect(listAlbumTracks).toHaveBeenCalledWith('al1');
  });

  test('Like item calls likeEntity', async () => {
    render(AlbumMenu, { props: { ...PROPS, tracks: TRACKS } });
    await fireEvent.click(screen.getByRole('button', { name: /album actions for disc/i }));
    await fireEvent.click(screen.getByText(/Like album/i));
    expect(likeEntity).toHaveBeenCalled();
  });

  test('Go to artist navigates', async () => {
    render(AlbumMenu, { props: { ...PROPS, tracks: TRACKS } });
    await fireEvent.click(screen.getByRole('button', { name: /album actions for disc/i }));
    await fireEvent.click(screen.getByText(/Go to artist/i));
    expect(goto).toHaveBeenCalledWith('/artists/a1');
  });

  test('Escape key closes the menu', async () => {
    render(AlbumMenu, { props: { ...PROPS, tracks: TRACKS } });
    await fireEvent.click(screen.getByRole('button', { name: /album actions for disc/i }));
    expect(await screen.findByText(/Like album/i)).toBeInTheDocument();
    await fireEvent.keyDown(window, { key: 'Escape' });
    expect(screen.queryByText(/Like album/i)).not.toBeInTheDocument();
  });
});
