import { z } from '@hono/zod-openapi';
import { CreditBalanceChangeType } from '../entities';
import { ErrorSchema } from '@d8d/shared-utils';

// 信用额度实体Schema
export const CreditBalanceSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '额度记录ID',
    example: 1
  }),
  tenantId: z.number().int().positive().openapi({
    description: '租户ID',
    example: 1
  }),
  userId: z.number().int().positive().openapi({
    description: '用户ID',
    example: 1001
  }),
  totalLimit: z.coerce.number().openapi({
    description: '总额度',
    example: 10000.00
  }),
  usedAmount: z.coerce.number().openapi({
    description: '已用额度',
    example: 500.00
  }),
  availableAmount: z.coerce.number().openapi({
    description: '可用额度',
    example: 9500.00
  }),
  isEnabled: z.number().int().min(0).max(1).openapi({
    description: '是否启用(0:禁用,1:启用)',
    example: 1
  }),
  createdAt: z.coerce.date().openapi({
    description: '创建时间',
    example: '2024-01-01T00:00:00Z'
  }),
  updatedAt: z.coerce.date().openapi({
    description: '更新时间',
    example: '2024-01-01T00:00:00Z'
  })
});

// 额度变更记录实体Schema
export const CreditBalanceLogSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '变更记录ID',
    example: 1
  }),
  tenantId: z.number().int().positive().openapi({
    description: '租户ID',
    example: 1
  }),
  userId: z.number().int().positive().openapi({
    description: '用户ID',
    example: 1001
  }),
  changeType: z.nativeEnum(CreditBalanceChangeType).openapi({
    description: '变更类型',
    example: CreditBalanceChangeType.PAYMENT
  }),
  changeAmount: z.coerce.number().openapi({
    description: '变更金额（正数表示增加额度，负数表示减少额度）',
    example: -500.00
  }),
  beforeTotal: z.coerce.number().nullable().openapi({
    description: '变更前总额度',
    example: 10000.00
  }),
  afterTotal: z.coerce.number().nullable().openapi({
    description: '变更后总额度',
    example: 10000.00
  }),
  beforeUsed: z.coerce.number().nullable().openapi({
    description: '变更前已用额度',
    example: 0.00
  }),
  afterUsed: z.coerce.number().nullable().openapi({
    description: '变更后已用额度',
    example: 500.00
  }),
  referenceId: z.string().nullable().openapi({
    description: '关联ID(订单号等)',
    example: 'ORD202412010001'
  }),
  remark: z.string().nullable().openapi({
    description: '备注',
    example: '订单支付扣减额度'
  }),
  operatorId: z.number().int().positive().nullable().openapi({
    description: '操作人ID',
    example: 1
  }),
  createdAt: z.coerce.date().openapi({
    description: '创建时间',
    example: '2024-01-01T00:00:00Z'
  })
});

// 设置额度DTO
export const SetLimitDto = z.object({
  totalLimit: z.coerce.number().refine((val) => !isNaN(val), {
    message: '总额度必须是有效的数字'
  }).min(0, {
    message: '总额度必须大于等于0'
  }).openapi({
    description: '总额度',
    example: 10000.00
  }),
  operatorId: z.number().int().positive().optional().openapi({
    description: '操作人ID',
    example: 1
  }),
  remark: z.string().max(500, {
    message: '备注不能超过500个字符'
  }).optional().openapi({
    description: '备注',
    example: '初始化用户信用额度'
  })
});

// 调整额度DTO
export const AdjustLimitDto = z.object({
  adjustAmount: z.coerce.number().openapi({
    description: '调整金额（正数表示增加额度，负数表示减少额度）',
    example: 1000.00
  }),
  operatorId: z.number().int().positive().optional().openapi({
    description: '操作人ID',
    example: 1
  }),
  remark: z.string().max(500, {
    message: '备注不能超过500个字符'
  }).optional().openapi({
    description: '备注',
    example: '根据用户等级调整额度'
  })
});

// 额度支付DTO（用户操作）
export const PaymentDto = z.object({
  referenceId: z.string().max(100, {
    message: '订单ID不能超过100个字符'
  }).openapi({
    description: '订单ID',
    example: '123'
  }),
  operatorId: z.number().int().positive().optional().openapi({
    description: '操作人ID',
    example: 1
  }),
  remark: z.string().max(500, {
    message: '备注不能超过500个字符'
  }).optional().openapi({
    description: '备注',
    example: '订单支付扣减额度'
  })
});

// 结账恢复额度DTO（管理员操作）
export const CheckoutDto = z.object({
  userId: z.number().int().positive({
    message: '用户ID必须是正整数'
  }).openapi({
    description: '用户ID',
    example: 1001
  }),
  amount: z.number().positive({
    message: '恢复金额必须大于0'
  }).openapi({
    description: '恢复金额',
    example: 500.00
  }),
  referenceId: z.string().max(100, {
    message: '关联ID不能超过100个字符'
  }).optional().openapi({
    description: '关联ID(订单号等)',
    example: 'ORD202412010001'
  }),
  operatorId: z.number().int().positive().optional().openapi({
    description: '操作人ID',
    example: 1
  }),
  remark: z.string().max(500, {
    message: '备注不能超过500个字符'
  }).optional().openapi({
    description: '备注',
    example: '结账恢复额度'
  })
});

// 查询额度变更记录DTO
export const QueryBalanceLogsDto = z.object({
  page: z.coerce.number().int().positive().default(1).optional().openapi({
    description: '页码',
    example: 1
  }),
  pageSize: z.coerce.number().int().positive().max(100).default(20).optional().openapi({
    description: '每页数量',
    example: 20
  })
});