import { useState, useEffect } from "react";
import { usePositions } from "@/user-management/hooks/usePosition";
import { TeamMemberDto } from "@/user-management/dto/teamMember/teamMember";

interface UseEmployeesData {
  employees: TeamMemberDto[];
  isLoading: boolean;
  setEmployees: (employees: TeamMemberDto[]) => void;
  refetch?: () => Promise<void>;
}

export const useEmployeesData = (positionId: string): UseEmployeesData => {
  const [employees, setEmployees] = useState<TeamMemberDto[]>([]);

  const { useEmployeesUnderPosition } = usePositions();
  const { data, isLoading, refetch } = useEmployeesUnderPosition(positionId);

  useEffect(() => {
    if (data) {
      setEmployees(data.data as TeamMemberDto[]);
    }
  }, [data]);

  const refetchEmployees = async () => {
    if (refetch) {
      await refetch();
    }
  };

  return { employees, isLoading, setEmployees, refetch: refetchEmployees };
};
