import { z } from '@hono/zod-openapi';
import { UserSchema } from '../users/user.schema';
import { UserCardSchema } from '../user-cards/user-card.schema';

// 基础用户卡余额记录Schema
const UserCardBalanceRecordSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '记录ID',
    example: 1
  }),
  userId: z.number().int().positive().openapi({
    description: '用户ID',
    example: 1001
  }),
  cardNo: z.string().min(1, '卡号不能为空').max(20, '卡号最多20个字符').openapi({
    description: '卡号',
    example: '12345678901234567890'
  }),
  amount: z.coerce.number<number>().multipleOf(0.01).min(0).max(999999.99).openapi({
    description: '变动金额',
    example: 50.50
  }),
  amountBefore: z.coerce.number<number>().multipleOf(0.01).min(0).max(999999.99).openapi({
    description: '变动前金额',
    example: 100.00
  }),
  amountAfter: z.coerce.number<number>().multipleOf(0.01).min(0).max(999999.99).openapi({
    description: '变动后金额',
    example: 150.50
  }),
  orderNo: z.string().min(1, '订单号不能为空').max(32, '订单号最多32个字符').openapi({
    description: '订单号',
    example: 'ORD20240101120000'
  }),
  type: z.coerce.number<number>().int().min(1).max(2).openapi({
    description: '类型 1消费 2退款',
    example: 1
  }),
  remark: z.string().max(32, '备注最多32个字符').nullable().openapi({
    description: '备注',
    example: '消费充值'
  }),
  createdBy: z.number().int().positive().nullable().openapi({
    description: '创建人ID',
    example: 1
  }),
  updatedBy: z.number().int().positive().nullable().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'
  }),
  user: UserSchema.optional(),
  userCard: UserCardSchema.optional()
});

// 创建用户卡余额记录DTO
export const CreateUserCardBalanceRecordDto = UserCardBalanceRecordSchema.pick({
  userId: true,
  cardNo: true,
  amount: true,
  amountBefore: true,
  amountAfter: true,
  orderNo: true,
  type: true,
  remark: true
}).extend({
  userId: z.coerce.number<number>().int().positive('用户ID必须是正整数'),
  cardNo: z.string().min(1, '卡号不能为空').max(20, '卡号最多20个字符'),
  amount: z.coerce.number<number>().multipleOf(0.01).min(0).max(999999.99),
  amountBefore: z.coerce.number<number>().multipleOf(0.01).min(0).max(999999.99),
  amountAfter: z.coerce.number<number>().multipleOf(0.01).min(0).max(999999.99),
  orderNo: z.string().min(1, '订单号不能为空').max(32, '订单号最多32个字符'),
  type: z.coerce.number<number>().int().min(1).max(2),
  remark: z.string().max(32, '备注最多32个字符').nullable()
});

// 更新用户卡余额记录DTO
export const UpdateUserCardBalanceRecordDto = UserCardBalanceRecordSchema.pick({
  userId: true,
  cardNo: true,
  amount: true,
  amountBefore: true,
  amountAfter: true,
  orderNo: true,
  type: true,
  remark: true
}).partial().extend({
  userId: z.coerce.number<number>().int().positive('用户ID必须是正整数').optional(),
  cardNo: z.string().min(1, '卡号不能为空').max(20, '卡号最多20个字符').optional(),
  amount: z.coerce.number<number>().multipleOf(0.01).min(0).max(999999.99).optional(),
  amountBefore: z.coerce.number<number>().multipleOf(0.01).min(0).max(999999.99).optional(),
  amountAfter: z.coerce.number<number>().multipleOf(0.01).min(0).max(999999.99).optional(),
  orderNo: z.string().min(1, '订单号不能为空').max(32, '订单号最多32个字符').optional(),
  type: z.coerce.number<number>().int().min(1).max(2).optional(),
  remark: z.string().max(32, '备注最多32个字符').nullable().optional()
});

// 响应Schema（包含关联数据）
export const UserCardBalanceRecordResponseSchema = UserCardBalanceRecordSchema.extend({
  user: UserSchema.pick({
    id: true,
    username: true,
    name: true,
    phone: true
  }).optional(),
  userCard: UserCardSchema.pick({
    id: true,
    cardNo: true,
    balance: true
  }).optional()
});

export type UserCardBalanceRecord = z.infer<typeof UserCardBalanceRecordSchema>;
export type CreateUserCardBalanceRecordDto = z.infer<typeof CreateUserCardBalanceRecordDto>;
export type UpdateUserCardBalanceRecordDto = z.infer<typeof UpdateUserCardBalanceRecordDto>;
export type UserCardBalanceRecordResponse = z.infer<typeof UserCardBalanceRecordResponseSchema>;

export { UserCardBalanceRecordSchema };