/**
 * Payment related type definitions
 */

import type { UUID, Timestamps } from './common.types.js';
import type { ImmigrationCategory } from './immigration.types.js';

export enum PaymentMethod {
  ALIPAY = 'ALIPAY',
  WECHAT = 'WECHAT',
  CREDIT_CARD = 'CREDIT_CARD',
}

export enum PaymentStatus {
  PENDING = 'PENDING',
  PROCESSING = 'PROCESSING',
  COMPLETED = 'COMPLETED',
  FAILED = 'FAILED',
  REFUNDED = 'REFUNDED',
  CANCELLED = 'CANCELLED',
}

export enum OrderStatus {
  CREATED = 'CREATED',
  PENDING_PAYMENT = 'PENDING_PAYMENT',
  PAID = 'PAID',
  PROCESSING = 'PROCESSING',
  COMPLETED = 'COMPLETED',
  CANCELLED = 'CANCELLED',
  REFUNDED = 'REFUNDED',
}

export enum ServiceType {
  ASSESSMENT = 'ASSESSMENT',
  CONSULTATION = 'CONSULTATION',
  DOCUMENT_REVIEW = 'DOCUMENT_REVIEW',
}

export interface Order extends Timestamps {
  id: UUID;
  userId: UUID;
  conversationId?: UUID;
  serviceType: ServiceType;
  serviceCategory?: ImmigrationCategory;
  amount: number;
  currency: string;
  status: OrderStatus;
  paymentMethod?: PaymentMethod;
  paymentId?: UUID;
  paidAt?: Date;
  completedAt?: Date;
  metadata?: OrderMetadata;
}

export interface OrderMetadata {
  assessmentResult?: Record<string, unknown>;
  consultationNotes?: string;
  refundReason?: string;
}

export interface Payment extends Timestamps {
  id: UUID;
  orderId: UUID;
  method: PaymentMethod;
  amount: number;
  currency: string;
  status: PaymentStatus;
  transactionId?: string;
  qrCodeUrl?: string;
  paymentUrl?: string;
  expiresAt: Date;
  paidAt?: Date;
  failedReason?: string;
  callbackPayload?: Record<string, unknown>;
}

export interface CreatePaymentDto {
  orderId: UUID;
  method: PaymentMethod;
}

export interface CreateOrderDto {
  serviceType: ServiceType;
  serviceCategory?: ImmigrationCategory;
  conversationId?: UUID;
}

export interface PaymentResult {
  paymentId: UUID;
  orderId: UUID;
  qrCodeUrl?: string;
  paymentUrl?: string;
  expiresAt: Date;
  method: PaymentMethod;
  amount: number;
}

export interface PaymentCallbackData {
  transactionId: string;
  orderId: string;
  amount: number;
  status: 'success' | 'failed';
  timestamp: string;
  signature: string;
  rawPayload: Record<string, unknown>;
}

// Pricing configuration
export interface ServicePricing {
  serviceType: ServiceType;
  category?: ImmigrationCategory;
  price: number;
  currency: string;
  description: string;
  isActive: boolean;
}
