"use client";

import { useQuery } from "@tanstack/react-query";
import { getDropdownChoicesRequest } from "@/features/auth/services/authApi";
import {
  healthConcernOptions,
  nationalityResidencyOptions,
  occupationOptions,
} from "@/data/Constants";
import { EMPLOYEE_OCCUPATIONS } from "./volunteerAccountConstants";
import { toChoiceOptions, toChoiceTagOptions } from "@/lib/choices";

/**
 * Every dropdown on the volunteer account form.
 *
 * The occupation list is the interesting one: it is **grouped**, with the
 * employee roles collected under one heading and "other" pinned last, because
 * the flat master-choice list put a dozen job titles ahead of the handful of
 * non-employee answers most volunteers actually pick.
 */
export function useVolunteerAccountOptions(selectedLanguage: string) {
  const { data: tagsData, isLoading: tagsLoading } = useQuery({
    queryKey: ["user-interest-choices", selectedLanguage],
    queryFn: () => getDropdownChoicesRequest("user_interest"),
    enabled: !!selectedLanguage,
  });

  const { data: genderData, isLoading: genderLoading } = useQuery({
    queryKey: ["gender-choices", selectedLanguage],
    queryFn: () => getDropdownChoicesRequest("gender"),
    enabled: !!selectedLanguage,
  });

  const { data: relationshipData, isLoading: relationshipLoading } = useQuery({
    queryKey: ["emergency-contact-relationship-choices", selectedLanguage],
    queryFn: () => getDropdownChoicesRequest("emergency_contact_relationship"),
    enabled: !!selectedLanguage,
  });

  const tagOptions =
    toChoiceTagOptions(tagsData?.data, selectedLanguage, (item) => item.id);

  const genderOptions =
    toChoiceOptions(genderData?.data, selectedLanguage, (item) => item.id);

  const relationshipOptions =
    toChoiceOptions(relationshipData?.data, selectedLanguage, (item) => item.id);

  const nationalityOpts = nationalityResidencyOptions.map((item) => ({
    label: selectedLanguage === "ar" ? item.name_ar : item.name_en,
    value: item.value,
  }));

  const healthConcernOpts = healthConcernOptions.map((item) => ({
    label: selectedLanguage === "ar" ? item.name_ar : item.name_en,
    value: item.value,
  }));

  const localizedOccupation = (item: (typeof occupationOptions)[number]) => ({
    label: selectedLanguage === "ar" ? item.name_ar : item.name_en,
    value: item.value,
  });

  const occupationOpts = [
    // Non-employee options
    ...occupationOptions
      .filter(
        (item) =>
          !item.isHeading &&
          item.value !== "other" &&
          item.value !== "unemployed" &&
          !EMPLOYEE_OCCUPATIONS.includes(item.value)
      )
      .map(localizedOccupation),
    // Employee group
    {
      label: selectedLanguage === "ar" ? "موظف" : "Employee",
      options: occupationOptions
        .filter(
          (item) => !item.isHeading && EMPLOYEE_OCCUPATIONS.includes(item.value)
        )
        .map(localizedOccupation),
    },
    // Other option
    ...occupationOptions
      .filter((item) => item.value === "other")
      .map(localizedOccupation),
  ];

  // Use the centralized helper function for default profile image

  return {
    tagsData,
    tagsLoading,
    genderData,
    genderLoading,
    relationshipData,
    relationshipLoading,
    tagOptions,
    genderOptions,
    relationshipOptions,
    nationalityOpts,
    healthConcernOpts,
    occupationOpts,
  };
}
