import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
  createPlanBulkTeamMember,
  CreatePlanBulkTeamMemberDto,
  createPlanTeam,
  CreatePlanTeamDto,
  createPlanTeamMember,
  CreatePlanTeamMemberDto,
  deletePlanTeam,
  deletePlanTeamMember,
  getPlanTeamList,
  getPlanTeamMemberList,
  updatePlanTeam,
  updatePlanTeamMember,
} from "../services/api/planTeamServices";
import { toast } from "sonner";
import { useTranslation } from "react-i18next";
import { useErrorHandler } from "@/shared/hooks/useErrorHandler";
import { getEnvUrl } from "@/shared/config/env";
import { id } from "date-fns/locale";

// Fetch teams
export const usePlanTeamList = (id: string, options?: { enabled?: boolean }) =>
  useQuery({
    queryKey: ["plan-team-list", id],
    queryFn: () => getPlanTeamList(id),
    enabled: options?.enabled !== false && !!id, // Only fetch when enabled and id exists
  });

export const usePlanTeamMemberList = (
  id: string,
  options?: { enabled?: boolean },
) =>
  useQuery({
    queryKey: ["plan-team-member-list", id],
    queryFn: () => getPlanTeamMemberList(id),
    enabled: options?.enabled !== false && !!id, // Only fetch when enabled and id exists
  });

export const usePlanTeamMutation = () => {
  const { t } = useTranslation();
  const { handleError } = useErrorHandler(t);
  const queryClient = useQueryClient();
  const createPlanTeamMutation = useMutation({
    mutationFn: ({ data }: { data: CreatePlanTeamDto }) => createPlanTeam(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["plan-team-list"] });
    },
    onError: (error) => {
      handleError(error);
    },
  });

  const updatePlanTeamMutation = useMutation({
    mutationFn: ({ id, data }: { id: string; data: CreatePlanTeamDto }) =>
      updatePlanTeam(id, data),
    onSuccess: () => {},
    onError: (error) => {
      handleError(error);
    },
  });

  const deletePlanTeamMutation = useMutation({
    mutationFn: ({ id }: { id: string }) => deletePlanTeam(id),
    onSuccess: () => {
      toast.success("Plan team deleted successfully");
    },
    onError: (error) => {
      handleError(error);
    },
  });

  const createPlanTeamMemberMutation = useMutation({
    mutationFn: ({ data }: { data: CreatePlanTeamMemberDto }) =>
      createPlanTeamMember(data),
    onSuccess: () => {
      toast.success("Plan team member created successfully");
    },
    onError: (error) => {
      handleError(error);
    },
  });
  const createPlanBulkTeamMemberMutation = useMutation({
    mutationFn: ({ data }: { data: CreatePlanBulkTeamMemberDto }) =>
      createPlanBulkTeamMember(data),
    onSuccess: () => {
      toast.success("Plan team member created successfully");
    },
    onError: (error) => {
      handleError(error);
    },
  });
  const updatePlanTeamMemberMutation = useMutation({
    mutationFn: ({ id, data }: { id: string; data: CreatePlanTeamMemberDto }) =>
      updatePlanTeamMember(id, data),
    onSuccess: () => {
      toast.success("Plan team member updated successfully");
    },
    onError: (error) => {
      handleError(error);
    },
  });

  const deletePlanTeamMemberMutation = useMutation({
    mutationFn: deletePlanTeamMember,
    onSuccess: () => {
      toast.success("Plan team member deleted successfully");
    },
    onError: (error) => {
      handleError(error);
    },
  });
  return {
    createPlanTeam: createPlanTeamMutation.mutate,
    isLoading: createPlanTeamMutation.isPending,
    isError: createPlanTeamMutation.isError,
    error: createPlanTeamMutation.error,
    updatePlanTeam: updatePlanTeamMutation.mutate,
    deletePlanTeam: deletePlanTeamMutation.mutate,
    createPlanTeamMember: createPlanTeamMemberMutation.mutate,
    updatePlanTeamMember: updatePlanTeamMemberMutation.mutate,
    deletePlanTeamMember: deletePlanTeamMemberMutation.mutate,
    isPlanTeamLoading: updatePlanTeamMutation.isPending,
    isPlanTeamError: updatePlanTeamMutation.isError,
    planTeamError: updatePlanTeamMutation.error,
    isPlanTeamMemberLoading: deletePlanTeamMutation.isPending,
    isPlanTeamMemberError: deletePlanTeamMutation.isError,
    planTeamMemberError: deletePlanTeamMutation.error,
    isPlanTeamMemberCreateLoading: createPlanTeamMemberMutation.isPending,
    isPlanTeamMemberCreateError: createPlanTeamMemberMutation.isError,
    planTeamMemberCreateError: createPlanTeamMemberMutation.error,
    isPlanTeamMemberUpdateLoading: updatePlanTeamMemberMutation.isPending,
    isPlanTeamMemberUpdateError: updatePlanTeamMemberMutation.isError,
    planTeamMemberUpdateError: updatePlanTeamMemberMutation.error,
    isPlanTeamMemberDeleteLoading: deletePlanTeamMemberMutation.isPending,
    isPlanTeamMemberDeleteError: deletePlanTeamMemberMutation.isError,
    planTeamMemberDeleteError: deletePlanTeamMemberMutation.error,
    createPlanBulkTeamMember: createPlanBulkTeamMemberMutation.mutate,
    isPlanBulkTeamMemberCreateLoading:
      createPlanBulkTeamMemberMutation.isPending,
  };
};
// Fetch users for team member selection
export const useUsers = () =>
  useQuery({
    queryKey: ["users"],
    queryFn: async () => {
      const apiBaseUrl = getEnvUrl("VITE_API_URL");
      const response = await fetch(`${apiBaseUrl}/users/active`);
      return response.json();
    },
  });
