"use client";

import { FormikProps } from "formik";
import { useTranslation } from "react-i18next";
import PhoneInput from "@/components/ui/PhoneInput";
import SelectInput from "@/components/ui/SelectInput";
import CountryCodeSelect from "./LazyCountryCodeSelect";
import { type SponsorshipFormValues } from "./SponsorshipForm";

/**
 * The sponsorship form's phone, sponsor-type and type-of-support fields.
 *
 * The option lists come from the parent's three dropdown queries, and the
 * support list depends on which sponsor type is chosen, so both arrive as
 * props rather than being fetched again here.
 */
export default function SponsorshipSupportFields({
  formikProps,
  sponsorTypeOptions,
  sponsorTypeLoading,
  filteredSupportTypeOptions,
  supportTypeLoading,
}: {
  formikProps: FormikProps<SponsorshipFormValues>;
  sponsorTypeOptions: { label: string; value: string }[];
  sponsorTypeLoading: boolean;
  filteredSupportTypeOptions: { label: string; value: string }[];
  supportTypeLoading: boolean;
}) {
  const { t } = useTranslation();
  const { setFieldValue, values } = formikProps;

  return (
    <div className="flex gap-6 mobilescreen:gap-1 laptopitm1:flex-col laptopitm1:gap-0 lg:flex-row mobilescreen:flex-col lg:gap-6 md:flex-col md:gap-0">
      <div className="lg:w-1/2 md:w-full mobilescreen:w-full laptopitm1:w-full">
        <div className="flex gap-2">
          <div>
            <CountryCodeSelect
              name="country_code"
              className="w-full"
              onChange={(value: string) =>
                setFieldValue("country_code", value)
              }
            />
          </div>
          <div className="w-full">
            <PhoneInput
              name="phone_number"
              label={t("COMMON.PHONEPLACEHOLDER")}
              className="w-full"
              countryCode={values.country_code}
            />
          </div>
        </div>
      </div>
      <div className="w-1/2 xss:flex-col flex gap-1 mobilescreen:w-full md:w-full lg:w-1/2 laptopitm1:w-full">
        <div className="w-1/2 xss:w-full">
          <div className="max-w-[95%] xss:max-w-full">
            <SelectInput
              name="sponsor_type"
              label={t("COMMON.SPONSORSHIP_TYPE")}
              options={sponsorTypeOptions}
              onChange={(selectedOption) => {
                setFieldValue("sponsor_type", selectedOption?.value || "");
              }}
              disabled={sponsorTypeLoading}
            />
          </div>
        </div>
        <div className="w-1/2 xss:w-full">
          <div className="max-w-[100%]">
            <SelectInput
              name="type_of_support"
              label={t("COMMON.TYPE_OF_SUPPORT_PROVIDED")}
              options={filteredSupportTypeOptions}
              onChange={(selectedOption) =>
                setFieldValue(
                  "type_of_support",
                  selectedOption?.value || ""
                )
              }
              disabled={
                supportTypeLoading ||
                !values.sponsor_type ||
                filteredSupportTypeOptions.length === 0
              }
            />
          </div>
        </div>
      </div>
    </div>
  );
}
