import api from './api';
import type { Genre, Track } from '../types';

export interface GetGenreTracksParams {
  limit?: number;
  offset?: number;
}

export const genreService = {
  // GET /api/genres — all genres with a track_count, ordered by popularity.
  async listGenres(): Promise<Genre[]> {
    const res = await api.get<Genre[]>('/genres');
    return res.data;
  },

  // GET /api/genres/:id — a single genre (with track_count), or null if missing.
  async getGenre(id: string): Promise<Genre | null> {
    try {
      const res = await api.get<Genre>(`/genres/${id}`);
      return res.data;
    } catch {
      return null;
    }
  },

  // GET /api/genres/:id/tracks — LIBRARY tracks in this genre, by weight DESC.
  async getGenreTracks(id: string, params?: GetGenreTracksParams): Promise<Track[]> {
    const res = await api.get<Track[]>(`/genres/${id}/tracks`, { params });
    return res.data;
  },
};
