import { z } from 'zod';

// 打印机相关Schema
export const PrinterSchema = z.object({
  printerSn: z.string().min(1, '打印机序列号不能为空').max(50, '打印机序列号过长'),
  printerKey: z.string().min(1, '打印机密钥不能为空').max(100, '打印机密钥过长'),
  printerName: z.string().max(100, '打印机名称过长').optional(),
  printerType: z.enum(['58mm', '80mm']).default('58mm'),
  isDefault: z.boolean().default(false)
});

export const UpdatePrinterSchema = z.object({
  printerName: z.string().max(100, '打印机名称过长').optional(),
  printerType: z.enum(['58mm', '80mm']).optional(),
  printerStatus: z.enum(['ACTIVE', 'INACTIVE', 'ERROR']).optional(),
  isDefault: z.boolean().optional()
});

// 打印任务相关Schema
export const CreatePrintTaskSchema = z.object({
  orderId: z.number().int().positive().optional(),
  printerSn: z.string().min(1, '打印机序列号不能为空').max(50, '打印机序列号过长'),
  content: z.string().min(1, '打印内容不能为空'),
  printType: z.enum(['RECEIPT', 'SHIPPING', 'ORDER']),
  delaySeconds: z.number().int().min(0).max(300).default(0) // 最大延迟5分钟
});

export const CancelPrintTaskSchema = z.object({
  reason: z.enum(['REFUND', 'MANUAL', 'TIMEOUT']).default('MANUAL')
});

// 配置相关Schema
export const ConfigSchema = z.object({
  configKey: z.string().min(1, '配置键不能为空').max(50, '配置键过长'),
  configValue: z.string().optional(),
  configType: z.enum(['STRING', 'JSON', 'BOOLEAN', 'NUMBER']).default('STRING'),
  description: z.string().max(200, '描述过长').optional()
});

export const UpdateConfigSchema = z.object({
  configValue: z.string().optional(),
  configType: z.enum(['STRING', 'JSON', 'BOOLEAN', 'NUMBER']).optional(),
  description: z.string().max(200, '描述过长').optional()
});

// 查询参数Schema
export const QueryPrintTasksSchema = z.object({
  orderId: z.string().transform(val => parseInt(val, 10)).optional(),
  printerSn: z.string().optional(),
  printType: z.enum(['RECEIPT', 'SHIPPING', 'ORDER']).optional(),
  printStatus: z.enum(['PENDING', 'DELAYED', 'PRINTING', 'SUCCESS', 'FAILED', 'CANCELLED']).optional(),
  startDate: z.string().datetime().optional(),
  endDate: z.string().datetime().optional(),
  page: z.string().transform(val => parseInt(val, 10)).default(() => 1),
  limit: z.string().transform(val => parseInt(val, 10)).default(() => 20)
});

// 类型推断
export type PrinterInput = z.infer<typeof PrinterSchema>;
export type UpdatePrinterInput = z.infer<typeof UpdatePrinterSchema>;
export type CreatePrintTaskInput = z.infer<typeof CreatePrintTaskSchema>;
export type CancelPrintTaskInput = z.infer<typeof CancelPrintTaskSchema>;
export type ConfigInput = z.infer<typeof ConfigSchema>;
export type UpdateConfigInput = z.infer<typeof UpdateConfigSchema>;
export type QueryPrintTasksInput = z.infer<typeof QueryPrintTasksSchema>;