import { useQuery } from "@tanstack/react-query";
import { apiRequest } from "../lib/api-client";
import { useAuthStore } from "../stores/auth.store";

export type SettlementMember = {
  userId: string;
  name: string;
  paid: number;
  owes: number;
};

export type Settlement = {
  month: string;
  totalExpenses: number;
  memberCount: number;
  perMember: number;
  members: SettlementMember[];
};

export function useSettlement(month: string) {
  const activeHouseholdId = useAuthStore((s) => s.activeHouseholdId);
  return useQuery({
    queryKey: ["settlement", activeHouseholdId, month],
    queryFn: () =>
      apiRequest<Settlement>(`/api/households/settlement?month=${month}`),
    enabled: !!activeHouseholdId,
  });
}
