import { z } from '@hono/zod-openapi';
import { UserSchema } from '../users/user.schema';
import { AgentSchema } from '../agent/agent.schema';

// 基础用户卡Schema
const UserCardSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '用户卡ID',
    example: 1
  }),
  userId: z.number().int().positive().openapi({
    description: '用户ID',
    example: 1001
  }),
  agentId: z.coerce.number().int().positive().nullable().openapi({
    description: '代理商ID',
    example: 5
  }),
  cardNo: z.string().min(1, '卡号不能为空').max(20, '卡号最多20个字符').openapi({
    description: '卡号',
    example: '12345678901234567890'
  }),
  sjtCardNo: z.string().max(20, '盛京通卡卡号最多20个字符').nullable().openapi({
    description: '盛京通卡卡号',
    example: 'SJT1234567890'
  }),
  password: z.string().min(1, '密码不能为空').max(255, '密码最多255个字符').openapi({
    description: '密码',
    example: 'encrypted_password'
  }),
  authCode: z.string().max(16, '付款码最多16个字符').nullable().openapi({
    description: '付款码70_75开头16位随机数',
    example: '7012345678901234'
  }),
  state: z.coerce.number().int().min(1).max(2).default(1).openapi({
    description: '状态 1绑定 2解绑 通用联名电子卡不可解绑',
    example: 1
  }),
  balance: z.coerce.number().multipleOf(0.01).min(0).max(999999.99).default(0).openapi({
    description: '余额',
    example: 100.50
  }),
  isDefault: z.coerce.number().int().min(1).max(2).default(2).openapi({
    description: '默认 1是 2否',
    example: 1
  }),
  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(),
  agent: AgentSchema.nullable().optional()
});

// 创建用户卡DTO
export const CreateUserCardDto = UserCardSchema.pick({
  userId: true,
  agentId: true,
  cardNo: true,
  sjtCardNo: true,
  password: true,
  authCode: true,
  state: true,
  balance: true,
  isDefault: true
}).extend({
  userId: z.coerce.number<number>().int().positive('用户ID必须是正整数'),
  agentId: z.coerce.number<number>().int().positive('代理商ID必须是正整数').nullable().optional(),
  cardNo: z.string().min(1, '卡号不能为空').max(20, '卡号最多20个字符'),
  password: z.string().min(1, '密码不能为空').max(255, '密码最多255个字符'),
  sjtCardNo: z.string().max(20, '盛京通卡卡号最多20个字符').nullable().optional(),
  authCode: z.string().max(16, '付款码最多16个字符').nullable().optional(),
  state: z.coerce.number<number>().int().min(1).max(2).default(1),
  balance: z.coerce.number<number>().multipleOf(0.01).min(0).max(999999.99).default(0),
  isDefault: z.coerce.number<number>().int().min(1).max(2).default(2)
});

// 更新用户卡DTO
export const UpdateUserCardDto = UserCardSchema.pick({
  userId: true,
  agentId: true,
  cardNo: true,
  sjtCardNo: true,
  password: true,
  authCode: true,
  state: true,
  balance: true,
  isDefault: true
}).partial().extend({
  userId: z.coerce.number<number>().int().positive('用户ID必须是正整数').optional(),
  agentId: z.coerce.number<number>().int().positive('代理商ID必须是正整数').nullable().optional(),
  cardNo: z.string().min(1, '卡号不能为空').max(20, '卡号最多20个字符').optional(),
  password: z.string().min(1, '密码不能为空').max(255, '密码最多255个字符').optional(),
  sjtCardNo: z.string().max(20, '盛京通卡卡号最多20个字符').nullable().optional(),
  authCode: z.string().max(16, '付款码最多16个字符').nullable().optional(),
  state: z.coerce.number<number>().int().min(1).max(2).optional(),
  balance: z.coerce.number<number>().multipleOf(0.01).min(0).max(999999.99).optional(),
  isDefault: z.coerce.number<number>().int().min(1).max(2).optional()
});

// 响应Schema（包含关联数据）
export const UserCardResponseSchema = UserCardSchema.extend({
  user: UserSchema.pick({
    id: true,
    username: true,
    name: true,
    phone: true
  }).optional(),
  agent: AgentSchema.pick({
    id: true,
    name: true
  }).nullable().optional()
});

export type UserCard = z.infer<typeof UserCardSchema>;
export type CreateUserCardDto = z.infer<typeof CreateUserCardDto>;
export type UpdateUserCardDto = z.infer<typeof UpdateUserCardDto>;
export type UserCardResponse = z.infer<typeof UserCardResponseSchema>;

export { UserCardSchema };