"use client";

import { useQuery } from "@tanstack/react-query";
import { useAuthStore } from "@/store/authStore";
import { getVolunteerProfile, getOrganizerProfile } from "@/features/profile/services/profileApi";
import AddBanner from "@/features/community/components/AddBanner";

/** The home banner celebrates the signed-in account's own contribution. */
export default function HomeContributionBanner() {
  const user = useAuthStore(state => state.user);
  const { data, isError } = useQuery({
    queryKey: ["home-contribution", user?.id, user?.user_type],
    enabled: Boolean(user?.auth_token),
    queryFn: async () => {
      if (user?.user_type === "organization") {
        const profile = (await getOrganizerProfile()).data;
        return { hours: Number(profile?.organization_hours ?? 0), name: profile?.company_name || profile?.nickname };
      }
      const profile = (await getVolunteerProfile()).data;
      return { hours: Number(profile?.total_volunteer_hours ?? 0), name: profile?.nickname };
    },
  });
  if (!user || isError || !data || !Number.isFinite(data.hours) || data.hours <= 0) return null;
  return <AddBanner individual={{ total_hours: data.hours, name: data.name || [user.first_name, user.last_name].filter(Boolean).join(" ") }} />;
}
