import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document } from 'mongoose'

export type PaymentOrderDocument = PaymentOrder & Document

@Schema({ timestamps: true })
export class PaymentOrder {
  @Prop({ required: true, index: true })
  outTradeNo: string

  @Prop({ required: true, index: true })
  userId: string

  @Prop({ default: '' })
  userPhone?: string

  @Prop({ required: true })
  amount: number          // 分

  @Prop({ required: true })
  title: string

  @Prop({ default: '' })
  description?: string

  // pending | success | failed | refunded | partial_refund
  @Prop({ default: 'pending' })
  status: string

  @Prop({ default: 'membership' })
  type: string            // membership | interview | optimize | download

  @Prop({ default: 'growth' })
  plan: string            // growth | sprint

  @Prop({ default: 'native' })
  channel: string         // native | jsapi

  @Prop({ type: Object })
  metadata?: Record<string, any>

  @Prop()
  paidAt?: Date

  @Prop()
  wxTransactionId?: string

  // 退款
  @Prop({ default: 0 })
  refundAmount?: number

  @Prop()
  refundedAt?: Date

  @Prop()
  refundReason?: string

  @Prop()
  refundId?: string           // 微信退款单号
}

export const PaymentOrderSchema = SchemaFactory.createForClass(PaymentOrder)
PaymentOrderSchema.index({ createdAt: -1 })
PaymentOrderSchema.index({ status: 1, createdAt: -1 })
