import { Contact as CwContact } from "../../generated/prisma/client";
import {
  Contact as ApiContact,
  GenderType,
  PhoneType,
} from "../../../api/generated/prisma/client";
import { Translation } from "./types";

const toGender = (value: string | null): GenderType | null => {
  if (!value) return null;
  const normalized = value.trim().toUpperCase();
  if (normalized === "M") return GenderType.MALE;
  if (normalized === "F") return GenderType.FEMALE;
  return null;
};

const toPhoneType = (value: string | null): PhoneType | null => {
  if (!value) return null;
  const normalized = value.trim().toUpperCase();
  if (normalized === "DIRECT") return PhoneType.DIRECT;
  if (normalized === "MOBILE") return PhoneType.MOBILE;
  if (normalized === "HOME") return PhoneType.HOME;
  if (normalized === "COMPANY") return PhoneType.COMPANY;
  if (normalized === "SITE") return PhoneType.SITE;
  return null;
};

export const contactTranslation: Translation<CwContact, ApiContact> = {
  values: [
    { from: "contactRecId", to: "id" },
    {
      from: "inactiveFlag",
      to: "active",
      process: (value) => !value,
    },
    {
      from: "defaultFlag",
      to: "default",
      process: (value) => Boolean(value),
    },
    {
      from: "firstName",
      to: "firstName",
      process: (value) => (value ? value : "Unknown"),
    },
    {
      from: "lastName",
      to: "lastName",
      process: (value) => (value ? value : ""),
    },
    { from: "nickName", to: "nickname" },
    { from: "title", to: "title" },
    { from: "gender", to: "gender", process: toGender },
    { from: "dateBirth", to: "birthday" },
    { from: "defaultPhoneNbr", to: "phone" },
    { from: "defaultPhoneExtension", to: "phoneExtension" },
    { from: "defaultPhoneType", to: "phoneType", process: toPhoneType },
    { from: "companyAddressRecId", to: "companyAddressId" },
    { from: "companyRecId", to: "companyId" },
    { from: "dateEnteredUtc", to: "createdAt" },
    { from: "lastUpdatedUtc", to: "updatedAt" },
  ],
};
