import { TicketNote as CwTicketNote } from "../../generated/prisma/client";
import { ServiceTicketNote as ApiServiceTicketNote } from "../../../api/generated/prisma/client";
import { Translation, skipRow } from "./types";
import { TranslationContext } from "./context";

export const serviceTicketNoteTranslation: Translation<
  CwTicketNote,
  ApiServiceTicketNote,
  TranslationContext
> = {
  values: [
    { from: "ticketNoteRecId", to: "id" },
    {
      from: "srServiceRecId",
      to: "serviceTicketId",
      process: (value: number | null, context: TranslationContext) => {
        if (!value) {
          skipRow("ServiceTicketNote missing srServiceRecId");
        }

        if (!context.serviceTicketIds.has(value)) {
          skipRow(
            `ServiceTicketNote parent ticket missing: ${value}`
          );
        }

        return value;
      },
    },
    {
      from: "notesMarkdown",
      to: "notesMd",
      process: (value: string | null) => value || "",
    },
    {
      from: "notes",
      to: "notes",
      process: (value: string | null) => value || "",
    },
    {
      from: "memberRecId",
      to: "authorId",
      process: (value: number | null, context: TranslationContext) => {
        if (!value) {
          skipRow("ServiceTicketNote missing memberRecId");
        }
        const userId = context.usersByMemberRecId.get(value);
        if (!userId) {
          skipRow(
            `Cannot find user mapping for memberRecId: ${value}`
          );
        }
        return userId;
      },
    },
    { from: "problemFlag", to: "problemFlag" },
    { from: "resolutionFlag", to: "resolutionFlag" },
    { from: "internalAnalysisFlag", to: "internalAnalysisFlag" },
    { from: "internalMemberFlag", to: "internalMemberFlag" },
    { from: "mergedFlag", to: "mergedFlag" },
    { from: "bundledFlag", to: "bundledFlag" },
    { from: "createdByParentFlag", to: "createdByParentFlag" },
    {
      from: "dateCreatedUtc",
      to: "createdAt",
      process: (value: Date | null) => value || null,
    },
    {
      from: "lastUpdatedUtc",
      to: "updatedAt",
      process: (value: Date | null) => value || null,
    },
  ],
};
