/**
 * WaafiPay (Hosted Payment Page) request/response types.
 *
 * WaafiPay multiplexes every operation through a single `POST /asm` endpoint, discriminated by
 * `serviceName`. We use the HPP family (`HPP_PURCHASE`, `HPP_GETTRANINFO`) which returns a hosted
 * redirect URL and supports webhooks — see docs/waffi/intro.md.
 */

/** Terminal/intermediate transaction states reported by Waafi (sync `state` / `HPP_GETTRANINFO`). */
export type WaafiState =
  | 'APPROVED'
  | 'DECLINED'
  | 'FAILED'
  | 'CANCELED'
  | 'EXPIRED'
  | 'TIMEOUT'
  | string;

/** Common request envelope shared by every `/asm` call. */
export interface WaafiRequestEnvelope<TServiceParams> {
  schemaVersion: '1.0';
  requestId: string;
  timestamp: string;
  channelName: 'WEB';
  serviceName: string;
  serviceParams: TServiceParams;
}

/** Common response envelope. `responseCode === '2001'` means the request was processed (not paid). */
export interface WaafiResponseEnvelope<TParams> {
  schemaVersion: string;
  timestamp: string;
  responseId: string;
  responseCode: string;
  errorCode: string;
  responseMsg: string;
  params?: TParams;
}

// --- HPP_PURCHASE -----------------------------------------------------------------------------

export interface WaafiHppPurchaseServiceParams {
  merchantUid: string;
  storeId: string;
  hppKey: string;
  paymentMethod: string;
  hppSuccessCallbackUrl: string;
  hppFailureCallbackUrl: string;
  /** Callback data format: 1 = POST, 2 = GET, 4 = Result Token. */
  hppRespDataFormat: number;
  /** Required for MWALLET_ACCOUNT — pre-fills (and locks) the payer's phone on the hosted page. */
  payerInfo?: {
    subscriptionId: string;
  };
  transactionInfo: {
    referenceId: string;
    amount: number;
    currency: string;
    description?: string;
  };
}

export type WaafiHppPurchaseRequest = WaafiRequestEnvelope<WaafiHppPurchaseServiceParams>;

export interface WaafiHppPurchaseParams {
  hppUrl: string;
  directPaymentLink?: string;
  orderId: string;
  referenceId: string;
}

export type WaafiHppPurchaseResponse = WaafiResponseEnvelope<WaafiHppPurchaseParams>;

// --- HPP_GETTRANINFO --------------------------------------------------------------------------

export interface WaafiGetTranInfoServiceParams {
  merchantUid: string;
  storeId: string;
  hppKey: string;
  /** Either the merchant referenceId or the Waafi transactionId may be supplied. */
  referenceId?: string;
  transactionId?: string;
}

export type WaafiGetTranInfoRequest = WaafiRequestEnvelope<WaafiGetTranInfoServiceParams>;

export interface WaafiGetTranInfoParams {
  tranStatusDesc?: string;
  amount?: string;
  payerId?: string;
  paymentMethod?: string;
  description?: string;
  tranDate?: string;
  currency?: string;
  invoiceId?: string;
  referenceId?: string;
  tranAmount?: string;
  transactionId?: string;
  tranStatusId?: string;
  status?: WaafiState;
}

export type WaafiGetTranInfoResponse = WaafiResponseEnvelope<WaafiGetTranInfoParams>;
