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

// 平台实体Schema
export const PlatformSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '平台ID',
    example: 1
  }),
  platformName: z.string().max(100).openapi({
    description: '平台名称',
    example: '微信平台'
  }),
  contactPerson: z.string().max(50).openapi({
    description: '联系人',
    example: '张三'
  }),
  contactPhone: z.string().max(20).openapi({
    description: '联系电话',
    example: '13800138000'
  }),
  contactEmail: z.string().email().max(100).nullable().optional().openapi({
    description: '联系邮箱',
    example: 'zhangsan@example.com'
  }),
  status: z.number().int().min(0).max(1).default(1).openapi({
    description: '状态：1-正常，0-禁用',
    example: 1
  }),
  createTime: z.coerce.date().openapi({
    description: '创建时间',
    example: '2024-01-01T00:00:00Z'
  }),
  updateTime: z.coerce.date().openapi({
    description: '更新时间',
    example: '2024-01-01T00:00:00Z'
  })
});

// 创建平台DTO
export const CreatePlatformSchema = z.object({
  platformName: z.string({
    error: '请输入平台名称'
  }).min(1, {
    message: '平台名称不能为空'
  }).max(100, {
    message: '平台名称不能超过100个字符'
  }).openapi({
    description: '平台名称',
    example: '微信平台'
  }),
  contactPerson: z.string().max(50).optional().openapi({
    description: '联系人',
    example: '张三'
  }),
  contactPhone: z.string().max(20).optional().openapi({
    description: '联系电话',
    example: '13800138000'
  }),
  contactEmail: z.string({
    error: '请输入联系邮箱'
  }).email({
    message: '请输入有效的邮箱地址'
  }).max(100, {
    message: '邮箱地址不能超过100个字符'
  }).optional()
    .or(z.literal(''))  // 允许空字符串
    .transform(val => val === '' ? undefined : val)  // 将空字符串转为undefined
    .openapi({
      description: '联系邮箱',
      example: 'zhangsan@example.com'
    })
});

// 更新平台DTO
export const UpdatePlatformSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '平台ID',
    example: 1
  }),
  platformName: z.string().min(1).max(100).optional().openapi({
    description: '平台名称',
    example: '微信平台'
  }),
  contactPerson: z.string().max(50).optional().openapi({
    description: '联系人',
    example: '张三'
  }),
  contactPhone: z.string().max(20).optional().openapi({
    description: '联系电话',
    example: '13800138000'
  }),
  contactEmail: z.string({
    error: '请输入联系邮箱'
  }).email({
    message: '请输入有效的邮箱地址'
  }).max(100, {
    message: '邮箱地址不能超过100个字符'
  }).optional()
    .or(z.literal(''))  // 允许空字符串
    .transform(val => val === '' ? undefined : val)  // 将空字符串转为undefined
    .openapi({
      description: '联系邮箱',
      example: 'zhangsan@example.com'
    })
});

// 删除平台DTO
export const DeletePlatformSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '平台ID',
    example: 1
  })
});

// 分页查询参数Schema
export const PaginationQuerySchema = z.object({
  skip: z.coerce.number().int().min(0).default(0).optional().openapi({
    description: '跳过记录数',
    example: 0
  }),
  take: z.coerce.number().int().min(1).max(100).default(10).optional().openapi({
    description: '获取记录数',
    example: 10
  })
});

// 搜索平台参数Schema
export const SearchPlatformQuerySchema = PaginationQuerySchema.extend({
  name: z.string().min(1).openapi({
    description: '搜索关键词',
    example: '微信'
  })
});

// 类型定义
export type Platform = z.infer<typeof PlatformSchema>;
export type CreatePlatformDto = z.infer<typeof CreatePlatformSchema>;
export type UpdatePlatformDto = z.infer<typeof UpdatePlatformSchema>;
export type DeletePlatformDto = z.infer<typeof DeletePlatformSchema>;
export type PaginationQuery = z.infer<typeof PaginationQuerySchema>;
export type SearchPlatformQuery = z.infer<typeof SearchPlatformQuerySchema>;