/**
 * Salesforce Field Maps
 *
 * Centralized mapping of logical field names to Salesforce API field names.
 * This provides a single source of truth for all Salesforce custom fields
 * used by the Customer Portal.
 *
 * Usage:
 * ```typescript
 * import { ACCOUNT_FIELDS } from "@bff/integrations/salesforce/constants";
 *
 * const eligibilityValue = account[ACCOUNT_FIELDS.eligibility.value];
 * ```
 */

// =============================================================================
// Account Fields
// =============================================================================

export const ACCOUNT_FIELDS = {
  // Standard fields
  id: "Id",
  name: "Name",
  personEmail: "PersonEmail",
  phone: "Phone",

  // Portal identification
  customerNumber: "SF_Account_No__c",
  portalStatus: "Portal_Status__c",
  registrationSource: "Portal_Registration_Source__c",
  lastSignIn: "Portal_Last_SignIn__c",

  // WHMCS integration
  whmcsAccountId: "WH_Account__c",

  // Internet eligibility
  eligibility: {
    value: "Internet_Eligibility__c",
    status: "Internet_Eligibility_Status__c",
    requestedAt: "Internet_Eligibility_Request_Date_Time__c",
    checkedAt: "Internet_Eligibility_Checked_Date_Time__c",
  },

  // ID verification
  verification: {
    status: "Id_Verification_Status__c",
    submittedAt: "Id_Verification_Submitted_Date_Time__c",
    verifiedAt: "Id_Verification_Verified_Date_Time__c",
    note: "Id_Verification_Note__c",
    rejectionMessage: "Id_Verification_Rejection_Message__c",
  },

  // Address fields
  address: {
    street: "BillingStreet",
    city: "BillingCity",
    state: "BillingState",
    postalCode: "BillingPostalCode",
    country: "BillingCountry",
  },
} as const;

export type AccountFieldKey = keyof typeof ACCOUNT_FIELDS;

// =============================================================================
// Order Fields
// =============================================================================

export const ORDER_FIELDS = {
  // Standard fields
  id: "Id",
  orderNumber: "OrderNumber",
  accountId: "AccountId",
  opportunityId: "OpportunityId",
  status: "Status",
  effectiveDate: "EffectiveDate",
  totalAmount: "TotalAmount",

  // Activation tracking
  activationStatus: "Activation_Status__c",
  activationErrorCode: "Activation_Error_Code__c",
  activationErrorMessage: "Activation_Error_Message__c",
  activationLastAttemptAt: "Activation_Last_Attempt_At__c",

  // WHMCS integration
  whmcsOrderId: "WHMCS_Order_ID__c",

  // Address fields
  billing: {
    street: "BillingStreet",
    city: "BillingCity",
    state: "BillingState",
    postalCode: "BillingPostalCode",
    country: "BillingCountry",
  },
} as const;

export type OrderFieldKey = keyof typeof ORDER_FIELDS;

// =============================================================================
// Opportunity Fields
// =============================================================================

export const OPPORTUNITY_FIELDS = {
  // Standard fields
  id: "Id",
  name: "Name",
  accountId: "AccountId",
  accountName: "Account.Name",
  stage: "StageName",
  closeDate: "CloseDate",
  isClosed: "IsClosed",
  isWon: "IsWon",
  description: "Description",
  createdDate: "CreatedDate",
  lastModifiedDate: "LastModifiedDate",

  // Application stage
  applicationStage: "Application_Stage__c",

  // Internet cancellation fields
  scheduledCancellationDate: "ScheduledCancellationDateAndTime__c",
  cancellationNotice: "CancellationNotice__c",
  lineReturnStatus: "LineReturn__c",

  // SIM cancellation fields
  simCancellationNotice: "SIMCancellationNotice__c",
  simScheduledCancellationDate: "SIMScheduledCancellationDateAndTime__c",

  // Product type
  commodityType: "CommodityType__c",

  // Portal integration fields
  source: "Opportunity_Source__c",
  whmcsServiceId: "WHMCS_Service_ID__c",
  whmcsRegistrationUrl: "WH_Registeration__c",
} as const;

export type OpportunityFieldKey = keyof typeof OPPORTUNITY_FIELDS;

/**
 * Complete Opportunity field map for portal operations.
 */
export const OPPORTUNITY_FIELD_MAP = OPPORTUNITY_FIELDS;

export type OpportunityFieldMap = typeof OPPORTUNITY_FIELD_MAP;

// =============================================================================
// Opportunity Query Field Sets
// =============================================================================

/**
 * Fields to select when querying Opportunities for matching.
 */
export const OPPORTUNITY_MATCH_QUERY_FIELDS = [
  OPPORTUNITY_FIELD_MAP.id,
  OPPORTUNITY_FIELD_MAP.name,
  OPPORTUNITY_FIELD_MAP.accountId,
  OPPORTUNITY_FIELD_MAP.stage,
  OPPORTUNITY_FIELD_MAP.closeDate,
  OPPORTUNITY_FIELD_MAP.isClosed,
  OPPORTUNITY_FIELD_MAP.applicationStage,
  OPPORTUNITY_FIELD_MAP.commodityType,
  OPPORTUNITY_FIELD_MAP.source,
  OPPORTUNITY_FIELD_MAP.createdDate,
] as const;

/**
 * Fields to select when querying full Opportunity details.
 */
export const OPPORTUNITY_DETAIL_QUERY_FIELDS = [
  ...OPPORTUNITY_MATCH_QUERY_FIELDS,
  OPPORTUNITY_FIELD_MAP.whmcsServiceId,
  // Internet cancellation fields
  OPPORTUNITY_FIELD_MAP.scheduledCancellationDate,
  OPPORTUNITY_FIELD_MAP.cancellationNotice,
  OPPORTUNITY_FIELD_MAP.lineReturnStatus,
  // SIM cancellation fields
  OPPORTUNITY_FIELD_MAP.simCancellationNotice,
  OPPORTUNITY_FIELD_MAP.simScheduledCancellationDate,
  OPPORTUNITY_FIELD_MAP.lastModifiedDate,
] as const;

/**
 * Fields to select for Internet cancellation status display.
 */
export const OPPORTUNITY_INTERNET_CANCELLATION_QUERY_FIELDS = [
  OPPORTUNITY_FIELD_MAP.id,
  OPPORTUNITY_FIELD_MAP.stage,
  OPPORTUNITY_FIELD_MAP.commodityType,
  OPPORTUNITY_FIELD_MAP.scheduledCancellationDate,
  OPPORTUNITY_FIELD_MAP.cancellationNotice,
  OPPORTUNITY_FIELD_MAP.lineReturnStatus,
  OPPORTUNITY_FIELD_MAP.whmcsServiceId,
] as const;

/**
 * Fields to select for SIM cancellation status display.
 */
export const OPPORTUNITY_SIM_CANCELLATION_QUERY_FIELDS = [
  OPPORTUNITY_FIELD_MAP.id,
  OPPORTUNITY_FIELD_MAP.stage,
  OPPORTUNITY_FIELD_MAP.commodityType,
  OPPORTUNITY_FIELD_MAP.simCancellationNotice,
  OPPORTUNITY_FIELD_MAP.simScheduledCancellationDate,
  OPPORTUNITY_FIELD_MAP.whmcsServiceId,
] as const;

// =============================================================================
// Opportunity Picklist References
// =============================================================================

/**
 * Opportunity stage picklist values (already exist in Salesforce).
 * These stages track the complete service lifecycle.
 */
export const OPPORTUNITY_STAGE_REFERENCE = {
  INTRODUCTION: {
    value: "Introduction",
    probability: 30,
    forecastCategory: "Pipeline",
    isClosed: false,
    description: "Initial customer interest / eligibility pending",
  },
  WIKI: {
    value: "WIKI",
    probability: 10,
    forecastCategory: "Omitted",
    isClosed: false,
    description: "Low priority / informational only",
  },
  READY: {
    value: "Ready",
    probability: 60,
    forecastCategory: "Pipeline",
    isClosed: false,
    description: "Eligible and ready to order",
  },
  POST_PROCESSING: {
    value: "Post Processing",
    probability: 75,
    forecastCategory: "Pipeline",
    isClosed: false,
    description: "Order placed, processing",
  },
  ACTIVE: {
    value: "Active",
    probability: 90,
    forecastCategory: "Pipeline",
    isClosed: false,
    description: "Service is active",
  },
  CANCELLING: {
    value: "△Cancelling",
    probability: 100,
    forecastCategory: "Pipeline",
    isClosed: false,
    description: "Cancellation requested, pending processing",
  },
  CANCELLED: {
    value: "〇Cancelled",
    probability: 100,
    forecastCategory: "Closed",
    isClosed: true,
    isWon: true,
    description: "Successfully cancelled",
  },
  COMPLETED: {
    value: "Completed",
    probability: 100,
    forecastCategory: "Closed",
    isClosed: true,
    isWon: true,
    description: "Service completed normally",
  },
  VOID: {
    value: "Void",
    probability: 0,
    forecastCategory: "Omitted",
    isClosed: true,
    isWon: false,
    description: "Lost / not eligible",
  },
  PENDING: {
    value: "Pending",
    probability: 0,
    forecastCategory: "Omitted",
    isClosed: true,
    isWon: false,
    description: "On hold / abandoned",
  },
} as const;

/**
 * Application stage picklist values (already exist in Salesforce).
 * Portal uses INTRO-1 for new opportunities.
 */
export const APPLICATION_STAGE_REFERENCE = {
  INTRO_1: { value: "INTRO-1", description: "Portal introduction (default)" },
  NA: { value: "N/A", description: "Not applicable" },
} as const;

/**
 * Cancellation notice picklist values (already exist in Salesforce).
 */
export const CANCELLATION_NOTICE_REFERENCE = {
  RECEIVED: { value: "有", label: "Received", description: "Cancellation form received" },
  NOT_YET: { value: "未", label: "Not Yet", description: "Not yet received (default)" },
  NOT_REQUIRED: { value: "不要", label: "Not Required", description: "Not required" },
  TRANSFER: { value: "移転", label: "Transfer", description: "Customer moving/transferring" },
} as const;

/**
 * Line return status picklist values (already exist in Salesforce).
 */
export const LINE_RETURN_STATUS_REFERENCE = {
  NOT_YET: { value: "NotYet", label: "Not Yet", description: "Return kit not sent" },
  SENT_KIT: { value: "SentKit", label: "Kit Sent", description: "Return kit sent to customer" },
  PICKUP_SCHEDULED: {
    value: "AS/Pickup予定",
    label: "Pickup Scheduled",
    description: "Pickup scheduled",
  },
  RETURNED_1: { value: "Returned1", label: "Returned", description: "Equipment returned" },
  RETURNED: { value: "Returned2", label: "Returned", description: "Equipment returned" },
  NTT_DISPATCH: { value: "NTT派遣", label: "NTT Dispatch", description: "NTT handling return" },
  COMPENSATED: {
    value: "Compensated",
    label: "Compensated",
    description: "Compensation fee charged",
  },
  NA: { value: "N/A", label: "N/A", description: "No rental equipment" },
} as const;

/**
 * CommodityType picklist values (already exist in Salesforce).
 * Maps to simplified product types for portal logic.
 */
export const COMMODITY_TYPE_REFERENCE = {
  PERSONAL_HOME_INTERNET: {
    value: "Personal SonixNet Home Internet",
    portalProductType: "Internet",
    description: "Personal home internet service",
  },
  CORPORATE_HOME_INTERNET: {
    value: "Corporate SonixNet Home Internet",
    portalProductType: "Internet",
    description: "Corporate home internet service",
  },
  SIM: {
    value: "SIM",
    portalProductType: "SIM",
    description: "SIM / mobile service",
  },
  VPN: {
    value: "VPN",
    portalProductType: "VPN",
    description: "VPN service",
  },
  TECH_SUPPORT: {
    value: "Onsite Support",
    portalProductType: null,
    description: "Tech support (not used by portal)",
  },
} as const;

/**
 * Source picklist values for Opportunity_Source__c field (existing in Salesforce).
 */
export const OPPORTUNITY_SOURCE_PICKLIST = [
  { value: "Portal - Internet Eligibility Request", label: "Portal - Internet Eligibility" },
  { value: "Portal - SIM Checkout Registration", label: "Portal - SIM Checkout" },
  { value: "Portal - Order Placement", label: "Portal - Order Placement" },
  { value: "Agent Created", label: "Agent Created" },
] as const;

// =============================================================================
// Case Fields
// =============================================================================

export const CASE_FIELDS = {
  // Standard fields
  id: "Id",
  caseNumber: "CaseNumber",
  accountId: "AccountId",
  contactId: "ContactId",
  opportunityId: "Opportunity__c",
  subject: "Subject",
  description: "Description",
  status: "Status",
  priority: "Priority",
  origin: "Origin",
  type: "Type",

  // Portal fields
  createdAt: "CreatedDate",
  closedAt: "ClosedDate",

  // Web-to-Case fields
  suppliedEmail: "SuppliedEmail",
  suppliedName: "SuppliedName",
  suppliedPhone: "SuppliedPhone",

  // Eligibility address key for duplicate detection (postcode:streetAddress)
  eligibilityAddressKey: "Eligibility_Address_Key__c",
} as const;

export type CaseFieldKey = keyof typeof CASE_FIELDS;

// =============================================================================
// Combined Export
// =============================================================================

export const SALESFORCE_FIELDS = {
  account: ACCOUNT_FIELDS,
  order: ORDER_FIELDS,
  opportunity: OPPORTUNITY_FIELDS,
  case: CASE_FIELDS,
} as const;
