import { SoActivity as CwSoActivity } from "../../generated/prisma/client";
import { Translation, skipRow } from "./types";
import { TranslationContext } from "./context";

type ApiActivityRecord = {
    id: number;
    subject: string;
    startTime: Date;
    endTime: Date;
    assignToId: string;
    assignedById: string;
    enteredBy: string;
    automated: boolean;
    closedFlag: boolean;
    notifyCompleteFlag: boolean;
    notificationSentFlat: boolean;
    opportunityId: string | null;
    serviceTicketId: string | null;
    contactId: number | null;
    companyId: number | null;
    activityTypeId: number | null;
    activityStatusId: number | null;
    createdById: string | null;
    updatedById: string | null;
    closedById: string | null;
    closedAt: Date | null;
    createdAt: Date;
    updatedAt: Date;
};

export const activityTranslation: Translation<
    CwSoActivity,
    ApiActivityRecord,
    TranslationContext
> = {
    values: [
        { from: "soActivityRecId", to: "id" },
        {
            from: "subject",
            to: "subject",
            process: (value) => (value ? value : ""),
        },
        {
            from: "dateTimeStart",
            to: "startTime",
            process: (value) => {
                if (!value) skipRow("Missing dateTimeStart");
                return value as Date;
            },
        },
        {
            from: "dateTimeEnd",
            to: "endTime",
            process: (value) => {
                if (!value) skipRow("Missing dateTimeEnd");
                return value as Date;
            },
        },
        {
            from: "assignTo",
            to: "assignToId",
        },
        {
            from: "assignedBy",
            to: "assignedById",
        },
        {
            from: "enteredBy",
            to: "enteredBy",
        },
        {
            from: "automated",
            to: "automated",
            process: (value) => Boolean(value),
        },
        {
            from: "closeFlag",
            to: "closedFlag",
            process: (value) => Boolean(value),
        },
        {
            from: "notifyCompleteFlag",
            to: "notifyCompleteFlag",
            process: (value) => Boolean(value),
        },
        {
            from: "notificationSentFlag",
            to: "notificationSentFlat",
            process: (value) => Boolean(value),
        },
        {
            // opportunityId is a String? field with no @relation — store as null
            // until a uid-mapping approach is established
            from: "opportunityRecId",
            to: "opportunityId",
            process: () => null,
        },
        {
            // serviceTicketId is a String? field with no @relation — store as null
            // until a uid-mapping approach is established
            from: "srServiceRecId",
            to: "serviceTicketId",
            process: () => null,
        },
        {
            from: "contactRecId",
            to: "contactId",
            process: (value: number | null, context: TranslationContext) => {
                if (!value) return null;
                if (!context.contactIds.has(value)) return null;
                return value;
            },
        },
        {
            from: "companyRecId",
            to: "companyId",
            process: (value: number | null, context: TranslationContext) => {
                if (!value) return null;
                if (!context.companyIds.has(value)) return null;
                return value;
            },
        },
        {
            from: "soActivityTypeRecId",
            to: "activityTypeId",
            process: (value: number | null, context: TranslationContext) => {
                if (!value) return null;
                if (!context.activityTypeIds.has(value)) return null;
                return value;
            },
        },
        {
            from: "soActStatusRecId",
            to: "activityStatusId",
            process: (value: number, context: TranslationContext) => {
                if (!context.activityStatusIds.has(value)) return null;
                return value;
            },
        },
        { from: "enteredBy", to: "createdById" },
        { from: "updatedBy", to: "updatedById" },
        { from: "closedBy", to: "closedById" },
        { from: "dateClosedUtc", to: "closedAt" },
        { from: "dateEnteredUtc", to: "createdAt" },
        { from: "lastUpdatedUTC", to: "updatedAt" },
    ],
};
