import { z } from '@hono/zod-openapi';
import { FileSchema } from '@d8d/file-module/schemas';
import { BankNameSchema } from '@d8d/bank-names-module/schemas';

// 基础字段定义
const BaseDisabledPersonSchema = z.object({
  name: z.string().min(1, '姓名不能为空').max(50).openapi({
    description: '姓名',
    example: '张三'
  }),
  gender: z.string().length(1, '性别不能为空').openapi({
    description: '性别：男/女',
    example: '男'
  }),
  idCard: z.string().min(1, '身份证号不能为空').max(20).openapi({
    description: '身份证号',
    example: '110101199001011234'
  }),
  disabilityId: z.string().min(1, '残疾证号不能为空').max(50).openapi({
    description: '残疾证号',
    example: 'CJZ20240001'
  }),
  disabilityType: z.string().min(1, '残疾类型不能为空').max(50).openapi({
    description: '残疾类型',
    example: '视力残疾'
  }),
  disabilityLevel: z.string().min(1, '残疾等级不能为空').max(20).openapi({
    description: '残疾等级',
    example: '一级'
  }),
  idAddress: z.string().min(1, '身份证地址不能为空').max(200).openapi({
    description: '身份证地址',
    example: '北京市东城区'
  }),
  phone: z.string().min(1, '联系电话不能为空').max(20).openapi({
    description: '联系方式',
    example: '13800138000'
  }),
  province: z.string().min(1, '省份不能为空').max(50).openapi({
    description: '省级',
    example: '北京市'
  }),
  city: z.string().min(1, '城市不能为空').max(50).openapi({
    description: '市级',
    example: '北京市'
  }),
  specificDisability: z.string().max(500, '具体残疾部位和情况最多500字符').optional().openapi({
    description: '具体残疾部位和情况',
    example: '左眼视力0.1，右眼视力0.2，需要助听器'
  })
});

// 残疾人实体Schema
export const DisabledPersonSchema = BaseDisabledPersonSchema.extend({
  id: z.number().int().positive().openapi({
    description: '残疾人ID',
    example: 1
  }),
  idValidDate: z.coerce.date<Date>().nullable().optional().openapi({
    description: '身份证有效期',
    example: '2030-12-31T00:00:00Z'
  }),
  disabilityValidDate: z.coerce.date<Date>().nullable().optional().openapi({
    description: '残疾证有效期',
    example: '2030-12-31T00:00:00Z'
  }),
  canDirectContact: z.number().int().min(0).max(1).default(0).openapi({
    description: '是否可直接联系：1-是，0-否',
    example: 1
  }),
  isMarried: z.number().int().min(0).max(1).nullable().optional().openapi({
    description: '是否已婚：1-是，0-否，null-未知',
    example: 1
  }),
  nation: z.string().max(20).nullable().optional().openapi({
    description: '民族',
    example: '汉族'
  }),
  district: z.string().max(50).nullable().optional().openapi({
    description: '区县级',
    example: '东城区'
  }),
  detailedAddress: z.string().max(200).nullable().optional().openapi({
    description: '详细地址',
    example: '东城区某街道某号'
  }),
  isInBlackList: z.number().int().min(0).max(1).default(0).openapi({
    description: '是否在黑名单中：1-是，0-否',
    example: 0
  }),
  jobStatus: z.number().int().min(0).max(1).default(0).openapi({
    description: '在职状态：0-未在职，1-已在职',
    example: 1
  }),
  specificDisability: z.string().max(500).nullable().optional().openapi({
    description: '具体残疾部位和情况',
    example: '左眼视力0.1，右眼视力0.2，需要助听器'
  }),
  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 CreateDisabledPersonSchema = BaseDisabledPersonSchema.extend({
  idValidDate: z.coerce.date<Date>().optional().openapi({
    description: '身份证有效期',
    example: '2030-12-31T00:00:00Z'
  }),
  disabilityValidDate: z.coerce.date<Date>().optional().openapi({
    description: '残疾证有效期',
    example: '2030-12-31T00:00:00Z'
  }),
  canDirectContact: z.number().int().min(0).max(1).default(0).optional().openapi({
    description: '是否可直接联系：1-是，0-否',
    example: 1
  }),
  isMarried: z.number().int().min(0).max(1).nullable().optional().openapi({
    description: '是否已婚：1-是，0-否，null-未知',
    example: 1
  }),
  nation: z.string().max(20).optional().openapi({
    description: '民族',
    example: '汉族'
  }),
  district: z.string().max(50).optional().openapi({
    description: '区县级',
    example: '东城区'
  }),
  detailedAddress: z.string().max(200).optional().openapi({
    description: '详细地址',
    example: '东城区某街道某号'
  }),
  isInBlackList: z.number().int().min(0).max(1).default(0).optional().openapi({
    description: '是否在黑名单中：1-是，0-否',
    example: 0
  }),
  jobStatus: z.number().int().min(0).max(1).default(0).optional().openapi({
    description: '在职状态：0-未在职，1-已在职',
    example: 1
  }),
  specificDisability: z.string().max(500).optional().openapi({
    description: '具体残疾部位和情况',
    example: '左眼视力0.1，右眼视力0.2，需要助听器'
  })
});

// 更新残疾人DTO
export const UpdateDisabledPersonSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '残疾人ID',
    example: 1
  }),
  name: z.string().min(1).max(50).optional().openapi({
    description: '姓名',
    example: '张三'
  }),
  gender: z.string().length(1).optional().openapi({
    description: '性别：男/女',
    example: '男'
  }),
  idCard: z.string().min(1).max(20).optional().openapi({
    description: '身份证号',
    example: '110101199001011234'
  }),
  disabilityId: z.string().min(1).max(50).optional().openapi({
    description: '残疾证号',
    example: 'CJZ20240001'
  }),
  disabilityType: z.string().min(1).max(50).optional().openapi({
    description: '残疾类型',
    example: '视力残疾'
  }),
  disabilityLevel: z.string().min(1).max(20).optional().openapi({
    description: '残疾等级',
    example: '一级'
  }),
  idAddress: z.string().min(1).max(200).optional().openapi({
    description: '身份证地址',
    example: '北京市东城区'
  }),
  idValidDate: z.coerce.date<Date>().optional().openapi({
    description: '身份证有效期',
    example: '2030-12-31T00:00:00Z'
  }),
  disabilityValidDate: z.coerce.date<Date>().optional().openapi({
    description: '残疾证有效期',
    example: '2030-12-31T00:00:00Z'
  }),
  phone: z.string().min(1).max(20).optional().openapi({
    description: '联系方式',
    example: '13800138000'
  }),
  canDirectContact: z.number().int().min(0).max(1).optional().openapi({
    description: '是否可直接联系：1-是，0-否',
    example: 1
  }),
  isMarried: z.number().int().min(0).max(1).optional().openapi({
    description: '是否已婚：1-是，0-否',
    example: 1
  }),
  nation: z.string().max(20).optional().openapi({
    description: '民族',
    example: '汉族'
  }),
  province: z.string().min(1).max(50).optional().openapi({
    description: '省级',
    example: '北京市'
  }),
  city: z.string().min(1).max(50).optional().openapi({
    description: '市级',
    example: '北京市'
  }),
  district: z.string().max(50).optional().openapi({
    description: '区县级',
    example: '东城区'
  }),
  detailedAddress: z.string().max(200).optional().openapi({
    description: '详细地址',
    example: '东城区某街道某号'
  }),
  isInBlackList: z.number().int().min(0).max(1).optional().openapi({
    description: '是否在黑名单中：1-是，0-否',
    example: 0
  }),
  jobStatus: z.number().int().min(0).max(1).optional().openapi({
    description: '在职状态：0-未在职，1-已在职',
    example: 1
  }),
  specificDisability: z.string().max(500).optional().openapi({
    description: '具体残疾部位和情况',
    example: '左眼视力0.1，右眼视力0.2，需要助听器'
  })
});

// 删除残疾人DTO
export const DeleteDisabledPersonSchema = 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 SearchDisabledPersonQuerySchema = PaginationQuerySchema.extend({
  keyword: z.string().optional().openapi({
    description: '搜索关键词（姓名或身份证号）',
    example: '张三'
  }),
  bankNameId: z.coerce.number().int().positive().optional().openapi({
    description: '银行名称ID筛选',
    example: 1
  }),
  cardType: z.string().optional().openapi({
    description: '银行卡类型筛选',
    example: '一类卡'
  }),
  disabilityType: z.string().optional().openapi({
    description: '残疾类型筛选',
    example: '视力残疾'
  }),
  disabilityLevel: z.string().optional().openapi({
    description: '残疾等级筛选',
    example: '一级'
  }),
  province: z.string().optional().openapi({
    description: '居住省份筛选',
    example: '北京市'
  })
});

// 银行卡实体Schema
export const DisabledBankCardSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '银行卡ID',
    example: 1
  }),
  personId: z.number().int().positive().openapi({
    description: '残疾人ID',
    example: 1
  }),
  subBankName: z.string().max(100).openapi({
    description: '发卡支行',
    example: '中国工商银行北京分行'
  }),
  bankNameId: z.number().int().positive().openapi({
    description: '银行名称ID',
    example: 1
  }),
  bankName: BankNameSchema.openapi({
    description: '银行名称实体信息',
    example: {
      id: 1,
      name: '中国工商银行',
      code: 'ICBC',
      remark: '大型国有商业银行',
      status: 1,
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-01T00:00:00Z',
      createdBy: 1,
      updatedBy: 1
    }
  }),
  cardNumber: z.string().max(50).openapi({
    description: '卡号',
    example: '6222021234567890123'
  }),
  cardholderName: z.string().max(50).openapi({
    description: '持卡人姓名',
    example: '张三'
  }),
  cardType: z.string().max(20).nullable().openapi({
    description: '银行卡类型：一类卡/二类卡',
    example: '一类卡'
  }),
  fileId: z.number().int().positive().openapi({
    description: '银行卡照片文件ID',
    example: 1
  }),
  file: FileSchema.nullable().optional().openapi({
    description: '银行卡照片文件实体信息',
    example: {
      id: 2,
      name: '银行卡照片.jpg',
      type: 'image/jpeg',
      size: 102400,
      path: '/uploads/2024/01/bank-card.jpg',
      fullUrl: 'https://minio.example.com/d8dai/uploads/2024/01/bank-card.jpg',
      description: '银行卡正面照片',
      uploadUserId: 1,
      uploadTime: '2024-01-01T10:30:00Z',
      lastUpdated: null,
      createdAt: '2024-01-01T10:30:00Z',
      updatedAt: '2024-01-01T10:30:00Z'
    }
  }),
  isDefault: z.number().int().min(0).max(1).default(0).openapi({
    description: '是否默认：1-是，0-否',
    example: 1
  })
});

// 创建银行卡DTO
export const CreateDisabledBankCardSchema = z.object({
  personId: z.number().int().positive().openapi({
    description: '残疾人ID',
    example: 1
  }),
  subBankName: z.string().max(100).openapi({
    description: '发卡支行',
    example: '中国工商银行北京分行'
  }),
  bankNameId: z.number().int().positive().openapi({
    description: '银行名称ID',
    example: 1
  }),
  cardNumber: z.string().max(50).openapi({
    description: '卡号',
    example: '6222021234567890123'
  }),
  cardholderName: z.string().max(50).openapi({
    description: '持卡人姓名',
    example: '张三'
  }),
  cardType: z.string().max(20).nullable().optional().openapi({
    description: '银行卡类型：一类卡/二类卡',
    example: '一类卡'
  }),
  fileId: z.number().int().positive().openapi({
    description: '银行卡照片文件ID',
    example: 1
  }),
  isDefault: z.number().int().min(0).max(1).default(0).optional().openapi({
    description: '是否默认：1-是，0-否',
    example: 1
  })
});

// 更新银行卡DTO
export const UpdateDisabledBankCardSchema = z.object({
  subBankName: z.string().max(100).optional().openapi({
    description: '发卡支行',
    example: '中国工商银行北京分行'
  }),
  bankNameId: z.number().int().positive().optional().openapi({
    description: '银行名称ID',
    example: 1
  }),
  cardNumber: z.string().max(50).optional().openapi({
    description: '卡号',
    example: '6222021234567890123'
  }),
  cardholderName: z.string().max(50).optional().openapi({
    description: '持卡人姓名',
    example: '张三'
  }),
  cardType: z.string().max(20).nullable().optional().openapi({
    description: '银行卡类型：一类卡/二类卡',
    example: '一类卡'
  }),
  fileId: z.number().int().positive().optional().openapi({
    description: '银行卡照片文件ID',
    example: 1
  }),
  isDefault: z.number().int().min(0).max(1).optional().openapi({
    description: '是否默认：1-是，0-否',
    example: 1
  })
});

// 照片实体Schema
export const DisabledPhotoSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '照片ID',
    example: 1
  }),
  personId: z.number().int().positive().openapi({
    description: '残疾人ID',
    example: 1
  }),
  photoType: z.string().max(50).openapi({
    description: '照片类型',
    example: '身份证照片'
  }),
  fileId: z.number().int().positive().openapi({
    description: '照片文件ID',
    example: 1
  }),
  file: FileSchema.nullable().optional().openapi({
    description: '文件实体信息',
    example: {
      id: 1,
      name: '身份证照片.jpg',
      type: 'image/jpeg',
      size: 102400,
      path: '/uploads/2024/01/id-photo.jpg',
      fullUrl: 'https://minio.example.com/d8dai/uploads/2024/01/id-photo.jpg',
      description: '身份证正面照片',
      uploadUserId: 1,
      uploadTime: '2024-01-01T10:30:00Z',
      lastUpdated: null,
      createdAt: '2024-01-01T10:30:00Z',
      updatedAt: '2024-01-01T10:30:00Z'
    }
  }),
  uploadTime: z.coerce.date().openapi({
    description: '上传时间',
    example: '2024-01-01T00:00:00Z'
  }),
  canDownload: z.number().int().min(0).max(1).default(0).openapi({
    description: '是否可下载：1-是，0-否',
    example: 1
  })
});

// 备注实体Schema
export const DisabledRemarkSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '备注ID',
    example: 1
  }),
  personId: z.number().int().positive().openapi({
    description: '残疾人ID',
    example: 1
  }),
  remarkContent: z.string().openapi({
    description: '备注内容',
    example: '该残疾人有特殊需求'
  }),
  isSpecialNeeds: z.number().int().min(0).max(1).default(0).openapi({
    description: '是否特殊需求：1-是，0-否',
    example: 1
  }),
  operatorId: z.number().int().positive().openapi({
    description: '操作人ID',
    example: 1
  }),
  remarkTime: z.coerce.date().openapi({
    description: '备注时间',
    example: '2024-01-01T00:00:00Z'
  })
});

// 回访实体Schema
export const DisabledVisitSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '回访ID',
    example: 1
  }),
  personId: z.number().int().positive().openapi({
    description: '残疾人ID',
    example: 1
  }),
  visitDate: z.coerce.date().openapi({
    description: '回访日期',
    example: '2024-01-01T00:00:00Z'
  }),
  visitType: z.string().max(50).openapi({
    description: '回访类型',
    example: '电话回访'
  }),
  visitContent: z.string().openapi({
    description: '回访内容',
    example: '了解残疾人近期情况'
  }),
  visitResult: z.string().max(50).nullable().optional().openapi({
    description: '回访结果',
    example: '良好'
  }),
  nextVisitDate: z.coerce.date().nullable().optional().openapi({
    description: '下次回访日期',
    example: '2024-02-01T00:00:00Z'
  }),
  visitorId: z.number().int().positive().openapi({
    description: '回访人ID',
    example: 1
  }),
  visitTime: z.coerce.date().openapi({
    description: '记录时间',
    example: '2024-01-01T00:00:00Z'
  })
});

// 创建聚合残疾人信息Schema
export const CreateAggregatedDisabledPersonSchema = z.object({
  personInfo: CreateDisabledPersonSchema.openapi({
    description: '残疾人基本信息'
  }),
  bankCards: z.array(DisabledBankCardSchema.omit({ id: true, personId: true })).optional().openapi({
    description: '银行卡信息列表'
  }),
  photos: z.array(DisabledPhotoSchema.omit({ id: true, personId: true, uploadTime: true })).optional().openapi({
    description: '照片信息列表'
  }),
  remarks: z.array(DisabledRemarkSchema.omit({ id: true, personId: true, remarkTime: true })).optional().openapi({
    description: '备注信息列表'
  }),
  visits: z.array(DisabledVisitSchema.omit({ id: true, personId: true, visitTime: true })).optional().openapi({
    description: '回访记录列表'
  })
});

// 聚合残疾人信息Schema
export const AggregatedDisabledPersonSchema = z.object({
  personInfo: DisabledPersonSchema.openapi({
    description: '残疾人基本信息'
  }),
  bankCards: z.array(DisabledBankCardSchema).openapi({
    description: '银行卡信息列表'
  }),
  photos: z.array(DisabledPhotoSchema).openapi({
    description: '照片信息列表'
  }),
  remarks: z.array(DisabledRemarkSchema).openapi({
    description: '备注信息列表'
  }),
  visits: z.array(DisabledVisitSchema).openapi({
    description: '回访记录列表'
  })
});

// 类型定义
export type DisabledPerson = z.infer<typeof DisabledPersonSchema>;
export type CreateDisabledPersonDto = z.infer<typeof CreateDisabledPersonSchema>;
export type UpdateDisabledPersonDto = z.infer<typeof UpdateDisabledPersonSchema>;
export type DeleteDisabledPersonDto = z.infer<typeof DeleteDisabledPersonSchema>;
export type PaginationQuery = z.infer<typeof PaginationQuerySchema>;
export type SearchDisabledPersonQuery = z.infer<typeof SearchDisabledPersonQuerySchema>;
export type DisabledBankCard = z.infer<typeof DisabledBankCardSchema>;
export type DisabledPhoto = z.infer<typeof DisabledPhotoSchema>;
export type DisabledRemark = z.infer<typeof DisabledRemarkSchema>;
export type DisabledVisit = z.infer<typeof DisabledVisitSchema>;
export type CreateAggregatedDisabledPersonDto = z.infer<typeof CreateAggregatedDisabledPersonSchema>;
export type AggregatedDisabledPerson = z.infer<typeof AggregatedDisabledPersonSchema>;