"use client";

import { useTranslation } from "react-i18next";
import UploadInput from "@/components/ui/UploadInput";
import TagsCheckbox from "@/components/ui/TagsCheckbox";
import type { OrganizerSectionProps } from "./organizerSectionProps";


/**
 * The supporting documents.
 *
 * Optional for تعليمي، حكومي and مجتمع and **required for every other type**
 * (`isLicenseUploadRequired`), and saved through their own multipart endpoint
 * alongside the ids of the documents kept.
 */
export default function OrganizerLicenseSection(props: OrganizerSectionProps) {
  const { t } = useTranslation();
  const {
    values,
    errors,
    touched,
    setFieldValue,
    setFieldTouched,
    organizerProfile,
    tagOptions,
    tagsLoading,
    keptDocIds,
    setKeptDocIds,
  } = props;

  return (
    <>
                      {/* Upload License */}
                      <div className="mb-4">
                        <UploadInput
                          name="documents"
                          label={t("COMMON.UPLOAD_CERTIFICATE")}
                          existingFiles={(
                            organizerProfile?.data?.documents ?? []
                          )
                            .filter((doc) => keptDocIds.includes(doc.id))
                            .map((doc, index) => ({
                                id: doc.id,
                                name: `${t("COMMON.DOCUMENT")} ${index + 1}`,
                                url: doc.document,
                              })
                            )}
                          onRemoveExisting={(id: string | number) =>
                            setKeptDocIds((prev: number[]) =>
                              prev.filter((kid: string | number) => kid !== id)
                            )
                          }
                          onChange={(event) => {
                            const files = event.currentTarget.files
                              ? Array.from(event.currentTarget.files)
                              : [];
                            setFieldValue("documents", files);
                          }}
                        />
                      </div>

                      <div className="mb-4">
                        <UploadInput
                          name="internal_license_files"
                          label={t("COMMON.INTERNAL_LICENSE_DOCUMENT")}
                          accept=".pdf,.jpg,.jpeg,.png,.webp"
                          multiple={false}
                          existingFiles={organizerProfile?.data?.internal_license_document &&
                            !values.internal_license_document_removed && !values.internal_license_files?.length
                            ? [{ id: "internal-license", name: t("COMMON.INTERNAL_LICENSE_DOCUMENT"),
                                url: organizerProfile.data.internal_license_document }]
                            : []}
                          onRemoveExisting={() => setFieldValue("internal_license_document_removed", true)}
                        />
                      </div>

                      <div className="flex mobilescreen:flex-col gap-4 mobilescreen:gap-0">
                        <TagsCheckbox
                          name="_interests"
                          label={t("COMMON.ENTER_TAGS")}
                          setFieldValue={setFieldValue}
                          setFieldTouched={setFieldTouched}
                          values={values._interests}
                          errors={errors._interests as string | undefined}
                          touched={touched._interests as boolean | undefined}
                          options={tagOptions}
                          disabled={tagsLoading}
                        />
                      </div>
    </>
  );
}
