/**
 * Orders Domain - Salesforce Provider Raw Types
 * 
 * Raw Salesforce API response types for Order and OrderItem sobjects.
 * Product and Pricebook types belong in the catalog domain.
 */

import { z } from "zod";

// ============================================================================
// Salesforce OrderItem Record (Raw API Response)
// ============================================================================

export const salesforceOrderItemRecordSchema = z.object({
  Id: z.string(),
  OrderId: z.string().nullable().optional(),
  Quantity: z.number().nullable().optional(),
  UnitPrice: z.number().nullable().optional(),
  TotalPrice: z.number().nullable().optional(),
  PricebookEntryId: z.string().nullable().optional(),
  // Note: PricebookEntry nested object comes from catalog domain
  PricebookEntry: z.unknown().nullable().optional(),
  Billing_Cycle__c: z.string().nullable().optional(),
  WHMCS_Service_ID__c: z.string().nullable().optional(),
  CreatedDate: z.string().optional(),
  LastModifiedDate: z.string().optional(),
});

export type SalesforceOrderItemRecord = z.infer<typeof salesforceOrderItemRecordSchema>;

// ============================================================================
// Salesforce Order Record (Raw API Response)
// ============================================================================

export const salesforceOrderRecordSchema = z.object({
  Id: z.string(),
  OrderNumber: z.string().optional(),
  Status: z.string().optional(),
  Type: z.string().optional(),
  EffectiveDate: z.string().nullable().optional(),
  TotalAmount: z.number().nullable().optional(),
  AccountId: z.string().nullable().optional(),
  // Note: Account nested object comes from customer domain
  Account: z.object({ Name: z.string().nullable().optional() }).nullable().optional(),
  Pricebook2Id: z.string().nullable().optional(),
  
  // Activation fields
  Activation_Type__c: z.string().nullable().optional(),
  Activation_Status__c: z.string().nullable().optional(),
  Activation_Scheduled_At__c: z.string().nullable().optional(),
  Activation_Error_Code__c: z.string().nullable().optional(),
  Activation_Error_Message__c: z.string().nullable().optional(),
  Activation_Last_Attempt_At__c: z.string().nullable().optional(),
  ActivatedDate: z.string().nullable().optional(),
  
  // Internet fields
  Internet_Plan_Tier__c: z.string().nullable().optional(),
  Installment_Plan__c: z.string().nullable().optional(),
  Access_Mode__c: z.string().nullable().optional(),
  Weekend_Install__c: z.boolean().nullable().optional(),
  Hikari_Denwa__c: z.boolean().nullable().optional(),
  
  // VPN fields
  VPN_Region__c: z.string().nullable().optional(),
  
  // SIM fields
  SIM_Type__c: z.string().nullable().optional(),
  SIM_Voice_Mail__c: z.boolean().nullable().optional(),
  SIM_Call_Waiting__c: z.boolean().nullable().optional(),
  EID__c: z.string().nullable().optional(),
  
  // MNP (Mobile Number Portability) fields
  MNP_Application__c: z.string().nullable().optional(),
  MNP_Reservation_Number__c: z.string().nullable().optional(),
  MNP_Expiry_Date__c: z.string().nullable().optional(),
  MNP_Phone_Number__c: z.string().nullable().optional(),
  MVNO_Account_Number__c: z.string().nullable().optional(),
  Porting_Date_Of_Birth__c: z.string().nullable().optional(),
  Porting_First_Name__c: z.string().nullable().optional(),
  Porting_Last_Name__c: z.string().nullable().optional(),
  Porting_First_Name_Katakana__c: z.string().nullable().optional(),
  Porting_Last_Name_Katakana__c: z.string().nullable().optional(),
  Porting_Gender__c: z.string().nullable().optional(),
  
  // Billing address snapshot fields (standard Salesforce Order columns)
  BillingStreet: z.string().nullable().optional(),
  BillingCity: z.string().nullable().optional(),
  BillingState: z.string().nullable().optional(),
  BillingPostalCode: z.string().nullable().optional(),
  BillingCountry: z.string().nullable().optional(),
  
  // Other fields
  Address_Changed__c: z.boolean().nullable().optional(),
  WHMCS_Order_ID__c: z.string().nullable().optional(),
  
  // Audit fields
  CreatedDate: z.string().optional(),
  LastModifiedDate: z.string().optional(),
});

export type SalesforceOrderRecord = z.infer<typeof salesforceOrderRecordSchema>;

// ============================================================================
// Salesforce Platform Events (for Order Provisioning)
// ============================================================================

/**
 * Platform Event payload for Order Fulfillment
 */
export const salesforceOrderProvisionEventPayloadSchema = z.object({
  OrderId__c: z.string().optional(),
  OrderId: z.string().optional(),
}).passthrough();

export type SalesforceOrderProvisionEventPayload = z.infer<typeof salesforceOrderProvisionEventPayloadSchema>;

/**
 * Platform Event structure
 */
export const salesforceOrderProvisionEventSchema = z.object({
  payload: salesforceOrderProvisionEventPayloadSchema,
  replayId: z.number().optional(),
}).passthrough();

export type SalesforceOrderProvisionEvent = z.infer<typeof salesforceOrderProvisionEventSchema>;

// ============================================================================
// Salesforce Pub/Sub Infrastructure Types
// ============================================================================

/**
 * Pub/Sub subscription configuration
 */
export const salesforcePubSubSubscriptionSchema = z.object({
  topicName: z.string(),
});

export type SalesforcePubSubSubscription = z.infer<typeof salesforcePubSubSubscriptionSchema>;

/**
 * Pub/Sub error metadata
 */
export const salesforcePubSubErrorMetadataSchema = z.object({
  "error-code": z.array(z.string()).optional(),
}).passthrough();

export type SalesforcePubSubErrorMetadata = z.infer<typeof salesforcePubSubErrorMetadataSchema>;

/**
 * Pub/Sub error structure
 */
export const salesforcePubSubErrorSchema = z.object({
  details: z.string().optional(),
  metadata: salesforcePubSubErrorMetadataSchema.optional(),
}).passthrough();

export type SalesforcePubSubError = z.infer<typeof salesforcePubSubErrorSchema>;

/**
 * Pub/Sub callback type
 */
export const salesforcePubSubCallbackTypeSchema = z.enum([
  "data",
  "event",
  "grpcstatus",
  "end",
  "error",
]);

export type SalesforcePubSubCallbackType = z.infer<typeof salesforcePubSubCallbackTypeSchema>;

/**
 * Generic event data (used when event type is unknown)
 */
export type SalesforcePubSubUnknownData = Record<string, unknown> | null | undefined;

/**
 * Pub/Sub event (can be order event, error, or unknown data)
 */
export type SalesforcePubSubEventData =
  | SalesforceOrderProvisionEvent
  | SalesforcePubSubError
  | SalesforcePubSubUnknownData;

/**
 * Complete Pub/Sub callback structure
 */
export const salesforcePubSubCallbackSchema = z.object({
  subscription: salesforcePubSubSubscriptionSchema,
  callbackType: salesforcePubSubCallbackTypeSchema,
  data: z.union([
    salesforceOrderProvisionEventSchema,
    salesforcePubSubErrorSchema,
    z.record(z.string(), z.unknown()),
    z.null(),
  ]),
});

export type SalesforcePubSubCallback = z.infer<typeof salesforcePubSubCallbackSchema>;
