import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import {
  contentPiecesApi,
  ContentPiece,
  ContentType,
  ContentStatus,
} from '@/services/api/projects.api';
import { PaginationParams } from '@/services/api/client';
import { useToast } from '@/hooks/use-toast';

export function useContentPieces(
  params?: PaginationParams & {
    projectId?: string;
    type?: ContentType;
    status?: ContentStatus;
    search?: string;
  }
) {
  return useQuery({
    queryKey: ['content-pieces', params],
    queryFn: () => contentPiecesApi.getAll(params).then((res) => res.data),
  });
}

export function useContentPiece(id: string) {
  return useQuery({
    queryKey: ['content-pieces', id],
    queryFn: () => contentPiecesApi.getById(id).then((res) => res.data),
    enabled: !!id,
  });
}

export function useContentPiecesByProject(projectId: string) {
  return useQuery({
    queryKey: ['content-pieces', 'project', projectId],
    queryFn: () => contentPiecesApi.getByProject(projectId).then((res) => res.data),
    enabled: !!projectId,
  });
}

export function useCreateContentPiece() {
  const queryClient = useQueryClient();
  const { toast } = useToast();

  return useMutation({
    mutationFn: (data: Partial<ContentPiece>) => contentPiecesApi.create(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['content-pieces'] });
      toast({ title: 'Contenido creado exitosamente' });
    },
    onError: (error: any) => {
      toast({
        title: 'Error al crear contenido',
        description: error.response?.data?.message || 'Error desconocido',
        variant: 'destructive',
      });
    },
  });
}

export function useUpdateContentPiece() {
  const queryClient = useQueryClient();
  const { toast } = useToast();

  return useMutation({
    mutationFn: ({ id, data }: { id: string; data: Partial<ContentPiece> }) =>
      contentPiecesApi.update(id, data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['content-pieces'] });
      toast({ title: 'Contenido actualizado exitosamente' });
    },
    onError: (error: any) => {
      toast({
        title: 'Error al actualizar contenido',
        description: error.response?.data?.message || 'Error desconocido',
        variant: 'destructive',
      });
    },
  });
}

export function useUpdateContentPieceStatus() {
  const queryClient = useQueryClient();
  const { toast } = useToast();

  return useMutation({
    mutationFn: ({ id, status }: { id: string; status: ContentStatus }) =>
      contentPiecesApi.updateStatus(id, status),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['content-pieces'] });
      toast({ title: 'Estado actualizado' });
    },
    onError: (error: any) => {
      toast({
        title: 'Error al actualizar estado',
        description: error.response?.data?.message || 'Error desconocido',
        variant: 'destructive',
      });
    },
  });
}

export function useDuplicateContentPiece() {
  const queryClient = useQueryClient();
  const { toast } = useToast();

  return useMutation({
    mutationFn: (id: string) => contentPiecesApi.duplicate(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['content-pieces'] });
      toast({ title: 'Contenido duplicado exitosamente' });
    },
    onError: (error: any) => {
      toast({
        title: 'Error al duplicar contenido',
        description: error.response?.data?.message || 'Error desconocido',
        variant: 'destructive',
      });
    },
  });
}

export function useDeleteContentPiece() {
  const queryClient = useQueryClient();
  const { toast } = useToast();

  return useMutation({
    mutationFn: (id: string) => contentPiecesApi.delete(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['content-pieces'] });
      toast({ title: 'Contenido eliminado exitosamente' });
    },
    onError: (error: any) => {
      toast({
        title: 'Error al eliminar contenido',
        description: error.response?.data?.message || 'Error desconocido',
        variant: 'destructive',
      });
    },
  });
}
