// Midtrans Core API response types (subset, aligned with docs)
// This file defines common transaction status response fields and method-specific extras,
// plus a normalizer to our app-friendly PaymentStatusResponse.

export type MidtransTransactionStatus =
  | 'pending'
  | 'settlement'
  | 'capture' // credit_card specific
  | 'deny'
  | 'cancel'
  | 'expire'
  | 'refund'
  | 'chargeback'

export interface MidtransBaseStatusResponse {
  status_code: string
  status_message: string
  transaction_id: string
  order_id: string
  merchant_id?: string
  gross_amount: string
  currency?: string
  payment_type: string
  transaction_time: string
  transaction_status: MidtransTransactionStatus
  fraud_status?: 'accept' | 'challenge' | 'deny'
}

// Bank transfer extras
export interface MidtransBankTransferExtras {
  va_numbers?: Array<{ bank: string; va_number: string }>
  permata_va_number?: string
  bill_key?: string // echannel (Mandiri)
  biller_code?: string // echannel (Mandiri)
}

// C-store extras
export interface MidtransCStoreExtras {
  store?: string // e.g., 'alfamart' | 'indomaret'
  payment_code?: string
}

// E-money (GoPay) extras
export interface MidtransEMoneyExtras {
  actions?: Array<{ name: string; method?: string; url: string }>
  expiry_time?: string
  image_url?: string
  qr_string?: string
  qr_url?: string
}

// Card extras
export interface MidtransCardExtras {
  masked_card?: string
  card_type?: string
  bank?: string
  redirect_url?: string
}

export type MidtransStatusResponse = MidtransBaseStatusResponse &
  Partial<MidtransBankTransferExtras & MidtransCStoreExtras & MidtransEMoneyExtras & MidtransCardExtras>

// Our app-level normalized payment status response type
export interface PaymentStatusResponse {
  orderId: string
  status: MidtransTransactionStatus
  method?: string
  // Common
  transactionTime?: string
  grossAmount?: string
  // Bank transfer
  vaNumber?: string
  bank?: string
  billKey?: string
  billerCode?: string
  // C-store
  store?: string
  paymentCode?: string
  // E-money
  actions?: Array<{ name: string; method?: string; url: string }>
  imageUrl?: string
  qrString?: string
  // Card
  maskedCard?: string
}

export function normalizeMidtransStatus(res: MidtransStatusResponse): PaymentStatusResponse {
  const method = res.payment_type
  const status = res.transaction_status

  // Bank transfer resolution
  let vaNumber: string | undefined
  let bank: string | undefined
  if (res.va_numbers && res.va_numbers.length > 0) {
    vaNumber = res.va_numbers[0].va_number
    bank = res.va_numbers[0].bank
  } else if (res.permata_va_number) {
    vaNumber = res.permata_va_number
    bank = 'permata'
  }

  return {
    orderId: res.order_id,
    status,
    method,
    transactionTime: res.transaction_time,
    grossAmount: res.gross_amount,
    vaNumber,
    bank,
    billKey: res.bill_key,
    billerCode: res.biller_code,
    store: res.store,
    paymentCode: res.payment_code,
    actions: res.actions,
    imageUrl: res.image_url,
    qrString: res.qr_string,
    maskedCard: res.masked_card,
  }
}