import * as yup from "yup";
import { normalizeDatabaseDate } from "./dateUtils";

export const getValidationSchema = (isEditMode) =>
  yup.object({
    national_id: yup
      .string()
      .required("کد ملی الزامی است")
      .matches(/^[0-9]{10}$/, "کد ملی باید 10 رقم باشد"),
    mobile: isEditMode
      ? yup
          .string()
          .nullable()
          .test(
            "mobile-format",
            "شماره تلفن باید 11 رقم باشد",
            (value) => !value || /^[0-9]{11}$/.test(value)
          )
      : yup
          .string()
          .required("شماره تلفن الزامی است")
          .matches(/^[0-9]{11}$/, "شماره تلفن باید 11 رقم باشد"),
    first_name: yup.string(),
    last_name: yup.string(),
    guild_name: yup.string(),
    guild_category: yup.string(),
    state: yup.string(),
    city: yup.string(),
    address: yup.string(),
    license_expire_date: yup.string(),
    license_status: yup.string(),
    union_name: yup.string(),
    postal_code: yup.string(),
    guild_national_id: yup.string(),
    is_foreigner: yup.string(),
    national_code: yup.string(),
    has_steward: yup.string(),
    has_partner: yup.string(),
    license_number: yup.string(),
    isAccepted: yup
      .boolean()
      .test("req", "باید تعهد نامه را بپذیرید!", (val) => {
        return val === true;
      })
      .required("این فیلد اجباری است!"),
  });

export const getInitialValues = (guild) => ({
  first_name: guild?.user?.firstName || "",
  last_name: guild?.user?.lastName || "",
  corporation_name: guild?.companyName || "",
  national_id: guild?.user?.nationalId || "",
  national_code: guild?.user?.nationalCode || "",
  birth_date: normalizeDatabaseDate(guild?.user?.birthday || ""),
  father_name: guild?.user?.fatherName || "",
  gender: guild?.user?.gender || "",
  person_city: guild?.user?.city || "",
  is_alive: guild?.user?.isAlive || "",
  guild_name: guild?.guildsName || "",
  area_activity: guild?.areaActivity || "",
  state: guild?.address?.province?.name || "",
  city: guild?.address?.city?.name || "",
  address: guild?.address?.address || "",
  license_expire_date: normalizeDatabaseDate(guild?.licenseExpireDate || ""),
  license_status: guild?.licenseStatus || "",
  license_type: guild?.licenseType || "",
  union_name: guild?.unionName || "",
  postal_code: guild?.address?.postalCode || "",
  phone_number: guild?.phoneNumber || "",
  mobile: guild?.user?.mobile || "",
  is_foreigner: guild?.is_foreign_national || "",
  has_steward: guild?.hasSteward || "",
  has_partner: guild?.hasPartner || "",
  license_number: guild?.licenseNumber || "",
  isAccepted: guild?.provinceAcceptState === "accepted" || false,
  steward:
    typeof guild?.steward === "boolean"
      ? guild.steward
      : typeof guild?.isSteward === "boolean"
      ? guild.isSteward
      : false,
  guild:
    typeof guild?.guild === "boolean"
      ? guild.guild
      : typeof guild?.isGuild === "boolean"
      ? guild.isGuild
      : false,
  verify_mobile: guild?.verifyMobile || false,
  guild_national_id: guild?.nationalId || "",
  license_issue_date: normalizeDatabaseDate(guild?.licenseIssueDate || ""),
  company_name: guild?.companyName || "",
  company_identifier: guild?.companyIdentifier || "",
  type_activity_name: guild?.typeActivityName || "",
  active: guild?.active ?? null,
});
