import { z } from '@hono/zod-openapi';

// 退款状态枚举
export const RefundStatus = {
  PENDING: 0, // 未退款
  PROCESSING: 1, // 退款中
  SUCCESS: 2, // 退款成功
  FAILED: 3, // 退款失败
} as const;

// 订单退款基础Schema
export const OrderRefundSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '退款记录ID',
    example: 1
  }),
  tenantId: z.number().int().positive().openapi({
    description: '租户ID',
    example: 1
  }),
  orderNo: z.string().max(32, '订单号最多32个字符').nullable().optional().openapi({
    description: '订单号',
    example: 'ORD20240101123456'
  }),
  refundOrderNo: z.string().max(32, '退款订单号最多32个字符').nullable().optional().openapi({
    description: '退款订单号',
    example: 'REF20240101123456'
  }),
  refundAmount: z.coerce.number().min(0, '退款金额不能小于0').max(999999.99, '退款金额不能超过999999.99').nullable().optional().openapi({
    description: '退款金额',
    example: 99.99
  }),
  state: z.coerce.number().int().min(0, '状态最小为0').max(3, '状态最大为3').default(0).openapi({
    description: '0未退款1退款中2退款成功3退款失败',
    example: 0
  }),
  remark: z.string().max(255, '备注最多255个字符').nullable().optional().openapi({
    description: '备注',
    example: '用户申请退款'
  }),
  createdBy: z.number().int().positive().nullable().optional().openapi({
    description: '创建人ID',
    example: 1
  }),
  updatedBy: z.number().int().positive().nullable().optional().openapi({
    description: '更新人ID',
    example: 1
  }),
  createdAt: z.coerce.date().openapi({
    description: '创建时间',
    example: '2024-01-01T12:00:00Z'
  }),
  updatedAt: z.coerce.date().openapi({
    description: '更新时间',
    example: '2024-01-01T12:00:00Z'
  }),
  // 关联实体
  order: z.object({
    id: z.number().int().positive().openapi({ description: '订单ID' }),
    orderNo: z.string().openapi({ description: '订单号', example: 'ORD20240101123456' }),
    userId: z.number().int().positive().openapi({ description: '用户ID', example: 1 }),
    amount: z.coerce.number().openapi({ description: '订单金额', example: 99.99 })
  }).nullable().optional().openapi({
    description: '订单信息'
  })
});

// 创建订单退款DTO
export const CreateOrderRefundDto = z.object({
  tenantId: z.number().int().positive('租户ID必须是正整数').openapi({
    description: '租户ID',
    example: 1
  }),
  orderNo: z.string().max(32, '订单号最多32个字符').nullable().optional().openapi({
    description: '订单号',
    example: 'ORD20240101123456'
  }),
  refundOrderNo: z.string().max(32, '退款订单号最多32个字符').nullable().optional().openapi({
    description: '退款订单号',
    example: 'REF20240101123456'
  }),
  refundAmount: z.coerce.number().min(0, '退款金额不能小于0').max(999999.99, '退款金额不能超过999999.99').nullable().optional().openapi({
    description: '退款金额',
    example: 99.99
  }),
  state: z.coerce.number().int().min(0, '状态最小为0').max(3, '状态最大为3').default(0).optional().openapi({
    description: '0未退款1退款中2退款成功3退款失败',
    example: 0
  }),
  remark: z.string().max(255, '备注最多255个字符').nullable().optional().openapi({
    description: '备注',
    example: '用户申请退款'
  })
});

// 更新订单退款DTO
export const UpdateOrderRefundDto = z.object({
  tenantId: z.number().int().positive('租户ID必须是正整数').optional().openapi({
    description: '租户ID',
    example: 1
  }),
  orderNo: z.string().max(32, '订单号最多32个字符').nullable().optional().openapi({
    description: '订单号',
    example: 'ORD20240101123456'
  }),
  refundOrderNo: z.string().max(32, '退款订单号最多32个字符').nullable().optional().openapi({
    description: '退款订单号',
    example: 'REF20240101123456'
  }),
  refundAmount: z.coerce.number().min(0, '退款金额不能小于0').max(999999.99, '退款金额不能超过999999.99').nullable().optional().openapi({
    description: '退款金额',
    example: 99.99
  }),
  state: z.coerce.number().int().min(0, '状态最小为0').max(3, '状态最大为3').optional().openapi({
    description: '0未退款1退款中2退款成功3退款失败',
    example: 2
  }),
  remark: z.string().max(255, '备注最多255个字符').nullable().optional().openapi({
    description: '备注',
    example: '退款已完成'
  })
});

// 订单退款列表响应
export const OrderRefundListResponse = z.object({
  data: z.array(OrderRefundSchema),
  pagination: z.object({
    total: z.number().openapi({
      example: 100,
      description: '总记录数'
    }),
    current: z.number().openapi({
      example: 1,
      description: '当前页码'
    }),
    pageSize: z.number().openapi({
      example: 10,
      description: '每页数量'
    })
  })
});