export const PaymentProviderType = {
  STRIPE: 'stripe' as const,
  ALIPAY: 'alipay' as const,
  WECHAT_PAY: 'wechat_pay' as const,
  CRYPTO: 'crypto' as const,
};
export type PaymentProviderType = typeof PaymentProviderType[keyof typeof PaymentProviderType];

export interface CreatePaymentParams {
  amount: number;           // in cents/fen
  currency: string;         // 'USD' | 'CNY'
  description: string;
  tenantId: string;
  invoiceId: string;
  returnUrl: string;        // for redirect-based flows
  cancelUrl?: string;
  metadata?: Record<string, any>;
}

// Alias for backwards compatibility
export type PaymentSessionRequest = CreatePaymentParams;

export interface PaymentSession {
  providerPaymentId: string;
  redirectUrl?: string;     // Alipay, WeChat, Crypto
  qrCodeUrl?: string;       // WeChat Native Pay
  clientSecret?: string;    // Stripe PaymentIntent
  hostedUrl?: string;       // Coinbase Commerce
  expiresAt?: Date;
}

export interface PaymentResult {
  providerPaymentId: string;
  status: 'succeeded' | 'failed' | 'pending';
  amount: number;
  currency: string;
  paidAt?: Date;
}

export interface WebhookResult {
  type: 'payment_succeeded' | 'payment_failed' | 'refunded' | 'unknown';
  providerPaymentId?: string;
  amount?: number;
  currency?: string;
  metadata?: Record<string, any>;
}

// Alias for backwards compatibility
export type WebhookEvent = WebhookResult;

export interface PaymentProviderPort {
  readonly providerType: PaymentProviderType;
  createPaymentSession(params: CreatePaymentParams): Promise<PaymentSession>;
  confirmPayment(providerPaymentId: string): Promise<PaymentResult>;
  handleWebhook(payload: Buffer, headers: Record<string, string>): Promise<WebhookResult>;
}
