import i18n from "@/lib/i18n/config";
/**
 * The sponsorship form's initial values and rules.
 *
 * A function, not a constant, because every message is produced by `t` and must
 * be re-read when the language changes. Lifted out of `SponsorshipForm` to bring
 * that file back under the 200-line limit — the schema was the largest block
 * that depends on nothing but `t`.
 */

import * as Yup from "yup";
import {
  YupEmail,
  YupRequiredString,
  YupStringMaxLength,
  createPhoneNumberSchema,
} from "@/features/shared/schemas";
import {
  whenValue,
  type YupWhenValue,
} from "@/features/shared/formRules";
import type { SponsorshipFormValues } from "./SponsorshipForm";
import { DEFAULT_DIAL_CODE } from "@/data/defaultCountry";
import {
  SPONSOR_DOCUMENTS_MAX,
  SPONSOR_DOCUMENT_EXTENSIONS,
  SPONSOR_FILE_MAX_MB,
  SPONSOR_LOGO_EXTENSIONS,
  hasAllowedExtension,
  isWithinSponsorFileSize,
} from "./sponsorshipFiles";

export const sponsorshipInitialValues: SponsorshipFormValues = {
  org_name: "",
  org_type: "",
  person_name: "",
  email: "",
  phone_number: "",
  /* Kuwait by default, declared here rather than seeded by
     `CountryCodeSelect` from a mount effect. See `DEFAULT_DIAL_CODE`. */
  country_code: DEFAULT_DIAL_CODE,
  sponsor_type: "",
  type_of_support: "",
  sponsorship_details: "",
  why_interested: "",
  resources_expected: "",
  new_sponsor_documents: [],
  sponsor_logo: null,
};

export function buildSponsorshipValidation(t: (key: string) => string) {
  return Yup.object({
  org_name: YupStringMaxLength(100).concat(YupRequiredString),
  org_type: Yup.string(),
  person_name: YupStringMaxLength(100).concat(YupRequiredString),
  email: YupEmail,
  phone_number: Yup.string().when(
    "country_code",
    (country_code: YupWhenValue) =>
      createPhoneNumberSchema(whenValue(country_code)),
  ),
  country_code: Yup.string(),
  sponsor_type: Yup.string(),
  type_of_support: Yup.string(),
  // `SponsorController::store` caps the details at 5000 characters.
  sponsorship_details: Yup.string().concat(YupRequiredString).max(5000, () => `${i18n.t("COMMON.MUST.BE.ATMOST")}5000${i18n.t("COMMON.CHARACTERS")}`),
  // Optional on PDF p.31; the API caps each at 5000 characters.
  why_interested: Yup.string().max(5000, () => `${i18n.t("COMMON.MUST.BE.ATMOST")}5000${i18n.t("COMMON.CHARACTERS")}`),
  resources_expected: Yup.string().max(5000, () => `${i18n.t("COMMON.MUST.BE.ATMOST")}5000${i18n.t("COMMON.CHARACTERS")}`),
  // The API's rules (see `sponsorshipFiles.ts`): 5 files, each 10 MB.
  new_sponsor_documents: Yup.array()
    .max(SPONSOR_DOCUMENTS_MAX, () => t("COMMON.SPONSOR_DOCUMENTS_MAX"))
    .test("document-type", () => t("COMMON.SPONSOR_DOCUMENT_TYPE"), (files) =>
      (files ?? []).every((file) => file instanceof File && hasAllowedExtension(file, SPONSOR_DOCUMENT_EXTENSIONS)))
    .test("document-size", () => i18n.t("COMMON.FILE_MAX_SIZE_MB", { size: SPONSOR_FILE_MAX_MB }), (files) =>
      (files ?? []).every((file) => file instanceof File && isWithinSponsorFileSize(file))),
  sponsor_logo: Yup.mixed().nullable()
    .test("logo-type", () => t("COMMON.SPONSOR_LOGO_TYPE"), (value) =>
      value == null || (value instanceof File && hasAllowedExtension(value, SPONSOR_LOGO_EXTENSIONS)))
    .test("logo-size", () => i18n.t("COMMON.FILE_MAX_SIZE_MB", { size: SPONSOR_FILE_MAX_MB }), (value) =>
      value == null || (value instanceof File && isWithinSponsorFileSize(value))),
});
}
