import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import {
  getPlaylists,
  getPlaylist,
  createPlaylist,
  updatePlaylist,
  deletePlaylist,
  addTrackToPlaylist,
  removeTrackFromPlaylist,
  reorderPlaylistTracks,
} from '../services/playlistService';
import { 
  Playlist, 
  CreatePlaylistRequest, 
  UpdatePlaylistRequest 
} from '../types';

export const PLAYLISTS_KEYS = {
  all: ['playlists'] as const,
  lists: () => [...PLAYLISTS_KEYS.all, 'list'] as const,
  list: (userId?: number) => [...PLAYLISTS_KEYS.lists(), { userId }] as const,
  details: () => [...PLAYLISTS_KEYS.all, 'detail'] as const,
  detail: (id: number) => [...PLAYLISTS_KEYS.details(), id] as const,
};

export function usePlaylists(userId?: number, page = 1, limit = 20) {
  return useQuery({
    queryKey: [...PLAYLISTS_KEYS.list(userId), { page, limit }],
    queryFn: () => getPlaylists(userId, page, limit),
    keepPreviousData: true,
  });
}

export function usePlaylist(id: number) {
  return useQuery({
    queryKey: PLAYLISTS_KEYS.detail(id),
    queryFn: () => getPlaylist(id),
    enabled: !!id,
  });
}

export function useCreatePlaylist() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (data: CreatePlaylistRequest) => createPlaylist(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: PLAYLISTS_KEYS.lists() });
    },
  });
}

export function useUpdatePlaylist() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, data }: { id: number; data: UpdatePlaylistRequest }) =>
      updatePlaylist(id, data),
    onSuccess: (data) => {
      queryClient.invalidateQueries({ queryKey: PLAYLISTS_KEYS.detail(data.id) });
      queryClient.invalidateQueries({ queryKey: PLAYLISTS_KEYS.lists() });
    },
  });
}

export function useDeletePlaylist() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => deletePlaylist(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: PLAYLISTS_KEYS.lists() });
    },
  });
}

export function useAddTrackToPlaylist() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({
      playlistId,
      trackId,
      position,
    }: {
      playlistId: number;
      trackId: number;
      position?: number;
    }) => addTrackToPlaylist(playlistId, trackId, position),
    onSuccess: (_, { playlistId }) => {
      queryClient.invalidateQueries({ queryKey: PLAYLISTS_KEYS.detail(playlistId) });
    },
  });
}

export function useRemoveTrackFromPlaylist() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ playlistId, trackId }: { playlistId: number; trackId: number }) =>
      removeTrackFromPlaylist(playlistId, trackId),
    onSuccess: (_, { playlistId }) => {
      queryClient.invalidateQueries({ queryKey: PLAYLISTS_KEYS.detail(playlistId) });
    },
  });
}

export function useReorderPlaylistTracks() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({
      playlistId,
      trackPositions,
    }: {
      playlistId: number;
      trackPositions: Record<number, number>;
    }) => reorderPlaylistTracks(playlistId, trackPositions),
    onSuccess: (_, { playlistId }) => {
      queryClient.invalidateQueries({ queryKey: PLAYLISTS_KEYS.detail(playlistId) });
    },
  });
}

