"use client";

import Input from "@/components/ui/Input";
import VolunteerAvatarField from "./VolunteerAvatarField";
import PhoneInput from "@/components/ui/PhoneInput";
import CountryCodeSelect from "./CountryCodeSelectLazy";
import { useTranslation } from "react-i18next";
import { nationalityNeedsPassport } from "@/data/Constants";
import type { NicknameAvailability } from "@/features/shared/nicknameAvailability";
import type { FormikProps } from "formik";
import { type CombinedFormValues } from "./volunteerAccountTypes";
import type { ApiResponse } from "@/lib/api/types";
import type { AccountRecord } from "./volunteerAccountRecords";


interface SectionProps {
  values: CombinedFormValues;
  setFieldValue: FormikProps<CombinedFormValues>["setFieldValue"];
  profilePic: string | null;
  uploadError: string | null;
  handleFileChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
  getDefaultImage: () => string;
  nicknameAvailability: NicknameAvailability;
  checkNicknameAvailability: (
    nickname: string,
    currentNickname: string
  ) => void;
  accountData: ApiResponse<AccountRecord> | undefined;
}

/**
 * The account half of the form: avatar, name, nickname, contact and civil id.
 *
 * Saved through `updateAccountInfo`, where everything below the divider goes to
 * `updateVolunteerProfile` — the split here mirrors the two endpoints.
 */
export default function VolunteerAccountIdentitySection(props: SectionProps) {
  const { t } = useTranslation();
  const {
    values,
    setFieldValue,
    profilePic,
    uploadError,
    handleFileChange,
    getDefaultImage,
    accountData,
  } = props;

  return (
    <>
                    {/* Account Information Section */}
                    <div>
                      <VolunteerAvatarField
                        values={values}
                        profilePic={profilePic}
                        uploadError={uploadError}
                        handleFileChange={handleFileChange}
                        getDefaultImage={getDefaultImage}
                      />

                      <div className="flex mobilescreen:gap-2 mobilescreen:flex-col gap-4">
                        <div className="relative flex-1">
                          <Input
                            name="first_name"
                            type="text"
                            label={t("COMMON.FIRSTNAMEPLACEHOLDER")}
                            className="pr-10"
                          />
                        </div>
                        <div className="relative flex-1">
                          <Input
                            name="last_name"
                            type="text"
                            label={t("COMMON.LASTNAMEPLACEHOLDER")}
                            className="pr-10"
                          />
                        </div>
                      </div>

                      <div className="flex mobilescreen:gap-2 mobilescreen:flex-col gap-4">
                        <div className="relative flex-1">
                          <Input
                            name="email"
                            type="text"
                            label={t("COMMON.EMAIL")}
                            value={accountData?.data?.email || ""}
                            disabled
                            className="pr-10"
                          />
                        </div>
                        <div className="relative flex-1">
                          <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.PHONE_NUMBER")}
                                className="pr-10"
                                countryCode={values.country_code}
                              />
                            </div>
                          </div>
                        </div>
                      </div>

                      <div className="flex mobilescreen:gap-0 mobilescreen:flex-col gap-4">
                        <div className="relative flex-1">
                          {nationalityNeedsPassport(values.nationality) ? (
                            <Input
                              name="passport_number"
                              type="text"
                              label={t("COMMON.PASSPORT_NUMBER")}
                              maxLength={20}
                            />
                          ) : (
                            <Input
                              name="civil_id"
                              type="text"
                              label={t("COMMON.CIVIL_ID")}
                              maxLength={12}
                              digitsOnly
                            />
                          )}
                        </div>
                      </div>
                    </div>
    </>
  );
}
