import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { projectsApi, Project, ProjectStatus } from '@/services/api/projects.api';
import { PaginationParams } from '@/services/api/client';
import { useToast } from '@/hooks/use-toast';

export function useProjects(
  params?: PaginationParams & { brandId?: string; status?: ProjectStatus; search?: string }
) {
  return useQuery({
    queryKey: ['projects', params],
    queryFn: () => projectsApi.getAll(params).then((res) => res.data),
  });
}

export function useProject(id: string) {
  return useQuery({
    queryKey: ['projects', id],
    queryFn: () => projectsApi.getById(id).then((res) => res.data),
    enabled: !!id,
  });
}

export function useProjectsByBrand(brandId: string) {
  return useQuery({
    queryKey: ['projects', 'brand', brandId],
    queryFn: () => projectsApi.getByBrand(brandId).then((res) => res.data),
    enabled: !!brandId,
  });
}

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

  return useMutation({
    mutationFn: (data: Partial<Project>) => projectsApi.create(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['projects'] });
      toast({ title: 'Proyecto creado exitosamente' });
    },
    onError: (error: any) => {
      toast({
        title: 'Error al crear proyecto',
        description: error.response?.data?.message || 'Error desconocido',
        variant: 'destructive',
      });
    },
  });
}

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

  return useMutation({
    mutationFn: ({ id, data }: { id: string; data: Partial<Project> }) =>
      projectsApi.update(id, data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['projects'] });
      toast({ title: 'Proyecto actualizado exitosamente' });
    },
    onError: (error: any) => {
      toast({
        title: 'Error al actualizar proyecto',
        description: error.response?.data?.message || 'Error desconocido',
        variant: 'destructive',
      });
    },
  });
}

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

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

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

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