import type { SalesforceProduct2WithPricebookEntries } from "#internal/services/providers/salesforce";
import type { SalesforceOrderItemRecord, SalesforceOrderRecord } from "./raw.types.js";

type SalesforceOrderRecordKey = Extract<keyof SalesforceOrderRecord, string>;
type SalesforceOrderItemRecordKey = Extract<keyof SalesforceOrderItemRecord, string>;
type SalesforceProductKey = Extract<keyof SalesforceProduct2WithPricebookEntries, string>;

export interface SalesforceOrderFieldMap {
  order: {
    type: SalesforceOrderRecordKey;
    activationType: SalesforceOrderRecordKey;
    activationScheduledAt: SalesforceOrderRecordKey;
    activationStatus: SalesforceOrderRecordKey;
    activationErrorCode: SalesforceOrderRecordKey;
    activationErrorMessage: SalesforceOrderRecordKey;
    activationLastAttemptAt: SalesforceOrderRecordKey;
    internetPlanTier: SalesforceOrderRecordKey;
    installationType: SalesforceOrderRecordKey;
    weekendInstall: SalesforceOrderRecordKey;
    accessMode: SalesforceOrderRecordKey;
    hikariDenwa: SalesforceOrderRecordKey;
    vpnRegion: SalesforceOrderRecordKey;
    simType: SalesforceOrderRecordKey;
    simVoiceMail: SalesforceOrderRecordKey;
    simCallWaiting: SalesforceOrderRecordKey;
    eid: SalesforceOrderRecordKey;
    whmcsOrderId: SalesforceOrderRecordKey;
    addressChanged: SalesforceOrderRecordKey;
    billingStreet: SalesforceOrderRecordKey;
    billingCity: SalesforceOrderRecordKey;
    billingState: SalesforceOrderRecordKey;
    billingPostalCode: SalesforceOrderRecordKey;
    billingCountry: SalesforceOrderRecordKey;
    mnpApplication: SalesforceOrderRecordKey;
    mnpReservation: SalesforceOrderRecordKey;
    mnpExpiry: SalesforceOrderRecordKey;
    mnpPhone: SalesforceOrderRecordKey;
    mvnoAccountNumber: SalesforceOrderRecordKey;
    portingDateOfBirth: SalesforceOrderRecordKey;
    portingFirstName: SalesforceOrderRecordKey;
    portingLastName: SalesforceOrderRecordKey;
    portingFirstNameKatakana: SalesforceOrderRecordKey;
    portingLastNameKatakana: SalesforceOrderRecordKey;
    portingGender: SalesforceOrderRecordKey;
  };
  orderItem: {
    billingCycle: SalesforceOrderItemRecordKey;
    whmcsServiceId: SalesforceOrderItemRecordKey;
  };
  product: {
    sku: SalesforceProductKey;
    itemClass: SalesforceProductKey;
    billingCycle: SalesforceProductKey;
    whmcsProductId: SalesforceProductKey;
    internetOfferingType: SalesforceProductKey;
    internetPlanTier: SalesforceProductKey;
    vpnRegion: SalesforceProductKey;
    bundledAddon: SalesforceProductKey;
    isBundledAddon: SalesforceProductKey;
  };
}

export type PartialSalesforceOrderFieldMap = {
  [Section in keyof SalesforceOrderFieldMap]?: Partial<SalesforceOrderFieldMap[Section]>;
};

export const defaultSalesforceOrderFieldMap: SalesforceOrderFieldMap = {
  order: {
    type: "Type",
    activationType: "Activation_Type__c",
    activationScheduledAt: "Activation_Scheduled_At__c",
    activationStatus: "Activation_Status__c",
    activationErrorCode: "Activation_Error_Code__c",
    activationErrorMessage: "Activation_Error_Message__c",
    activationLastAttemptAt: "Activation_Last_Attempt_At__c",
    internetPlanTier: "Internet_Plan_Tier__c",
    installationType: "Installment_Plan__c",
    weekendInstall: "Weekend_Install__c",
    accessMode: "Access_Mode__c",
    hikariDenwa: "Hikari_Denwa__c",
    vpnRegion: "VPN_Region__c",
    simType: "SIM_Type__c",
    simVoiceMail: "SIM_Voice_Mail__c",
    simCallWaiting: "SIM_Call_Waiting__c",
    eid: "EID__c",
    whmcsOrderId: "WHMCS_Order_ID__c",
    addressChanged: "Address_Changed__c",
    billingStreet: "BillingStreet",
    billingCity: "BillingCity",
    billingState: "BillingState",
    billingPostalCode: "BillingPostalCode",
    billingCountry: "BillingCountry",
    mnpApplication: "MNP_Application__c",
    mnpReservation: "MNP_Reservation_Number__c",
    mnpExpiry: "MNP_Expiry_Date__c",
    mnpPhone: "MNP_Phone_Number__c",
    mvnoAccountNumber: "MVNO_Account_Number__c",
    portingDateOfBirth: "Porting_Date_Of_Birth__c",
    portingFirstName: "Porting_First_Name__c",
    portingLastName: "Porting_Last_Name__c",
    portingFirstNameKatakana: "Porting_First_Name_Katakana__c",
    portingLastNameKatakana: "Porting_Last_Name_Katakana__c",
    portingGender: "Porting_Gender__c",
  },
  orderItem: {
    billingCycle: "Billing_Cycle__c",
    whmcsServiceId: "WHMCS_Service_ID__c",
  },
  product: {
    sku: "StockKeepingUnit",
    itemClass: "Item_Class__c",
    billingCycle: "Billing_Cycle__c",
    whmcsProductId: "WH_Product_ID__c",
    internetOfferingType: "Internet_Offering_Type__c",
    internetPlanTier: "Internet_Plan_Tier__c",
    vpnRegion: "VPN_Region__c",
    bundledAddon: "Bundled_Addon__c",
    isBundledAddon: "Is_Bundled_Addon__c",
  },
};

export function createSalesforceOrderFieldMap(
  overrides: PartialSalesforceOrderFieldMap = {}
): SalesforceOrderFieldMap {
  return {
    order: { ...defaultSalesforceOrderFieldMap.order, ...overrides.order },
    orderItem: { ...defaultSalesforceOrderFieldMap.orderItem, ...overrides.orderItem },
    product: { ...defaultSalesforceOrderFieldMap.product, ...overrides.product },
  };
}
