"use client";

import { useTranslation } from "react-i18next";
import Input from "@/components/ui/Input";
import InlineSpinner from "@/components/ui/InlineSpinner";
import SelectInput from "@/components/ui/SelectInput";
import type { OrganizerSectionProps } from "./organizerSectionProps";
import { useAuthStore } from "@/store/authStore";


/**
 * Description, interests and the entity type.
 *
 * The entity type is what decides whether a licence is required at all — see\n * `OrganizerLicenseSection`.
 */
export default function OrganizerDetailsSection(props: OrganizerSectionProps) {
  const { t } = useTranslation();
  const {
    values,
    errors,
    setFieldValue,
    nicknameAvailability,
    checkNicknameAvailability,
    initialNickname,
    orgTypeOptions,
    orgTypeLoading,
  } = props;
  /* A team has no organization type to pick (PDF p.9): the list leaves
     «Volunteer Team» out, so the select rendered empty and the server ignores
     a team's `organizer_type` anyway. */
  const isTeam = useAuthStore((s) => s.user?.organizer_type_display?.value_en === "Volunteer Team");

  return (
    <>
                      {/* Organizer Personal Details Section */}
                      <div className="flex mobilescreen:flex-col gap-4 mobilescreen:gap-0">
                        <div className="w-full relative">
                          <Input
                            name="nickname"
                            endAdornment={
                              nicknameAvailability.checking ? (
                                <InlineSpinner label={t("COMMON.CHECKING_AVAILABILITY")} />
                              ) : null
                            }
                            type="text"
                            label={t("COMMON.NICKNAME")}
                            onChange={(e) => {
                              setFieldValue("nickname", e.target.value);
                              checkNicknameAvailability(
                                e.target.value,
                                initialNickname || ""
                              );
                            }}
                          />
                          {values.nickname &&
                            values.nickname !== initialNickname &&
                            !errors.nickname && (
                              <div className="text-sm -mt-3 mb-4">
                                {!nicknameAvailability.checking &&
                                  nicknameAvailability.available === true && (
                                    <span className="text-green-600">
                                      ✓ {nicknameAvailability.message}
                                    </span>
                                  )}
                                {!nicknameAvailability.checking &&
                                  nicknameAvailability.available === false && (
                                    <span className="text-red-600">
                                      ✗ {nicknameAvailability.message}
                                    </span>
                                  )}
                              </div>
                            )}
                        </div>
                        {/*
                          Sector was dropped from the UI at the client's
                          request. The field is still on the API (8 resources
                          read it) and will be removed there in a separate pass,
                          so the value carried on the profile is preserved and
                          resubmitted untouched — only the input is gone.
                        */}
                      </div>

                      {/* Entity Type */}
                      {!isTeam && <div className="selectfiled">
                        <SelectInput
                          name="organizer_type"
                          label={t("COMMON.ORGANIZER.TYPE")}
                          options={orgTypeOptions}
                          onChange={(selectedOption) =>
                            setFieldValue(
                              "organizer_type",
                              String(selectedOption?.value ?? "")
                            )
                          }
                          disabled={orgTypeLoading}
                        />
                      </div>}
    </>
  );
}
