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

// 薪资实体Schema
export const SalaryLevelSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '薪资ID',
    example: 1
  }),
  provinceId: z.number().int().positive().openapi({
    description: '省份ID',
    example: 110000
  }),
  cityId: z.number().int().positive().openapi({
    description: '城市ID',
    example: 110100
  }),
  districtId: z.number().int().positive().nullable().optional().openapi({
    description: '区县ID',
    example: 110101
  }),
  basicSalary: z.coerce.number<number>().positive().openapi({
    description: '基本工资',
    example: 5000.00
  }),
  allowance: z.coerce.number<number>().min(0).default(0).openapi({
    description: '津贴补贴',
    example: 1000.00
  }),
  insurance: z.coerce.number<number>().min(0).default(0).openapi({
    description: '保险费用',
    example: 500.00
  }),
  housingFund: z.coerce.number<number>().min(0).default(0).openapi({
    description: '住房公积金',
    example: 800.00
  }),
  totalSalary: z.coerce.number<number>().positive().openapi({
    description: '总薪资',
    example: 7300.00
  }),
  updateTime: z.coerce.date().openapi({
    description: '更新时间',
    example: '2024-01-01T00:00:00Z'
  }),
  // 区域关联信息
  province: z.object({
    id: z.number().int().positive(),
    name: z.string()
  }).optional().openapi({
    description: '省份信息'
  }),
  city: z.object({
    id: z.number().int().positive(),
    name: z.string()
  }).optional().openapi({
    description: '城市信息'
  }),
  district: z.object({
    id: z.number().int().positive(),
    name: z.string()
  }).nullable().optional().openapi({
    description: '区县信息'
  })
});

// 创建薪资DTO
export const CreateSalarySchema = z.object({
  provinceId: z.coerce.number<number>({
    error: '请选择省份'
  }).int().positive({
    message: '请选择省份'
  }).openapi({
    description: '省份ID',
    example: 110000
  }),
  cityId: z.coerce.number<number>({
    error: '请选择城市'
  }).int().positive({
    message: '请选择城市'
  }).openapi({
    description: '城市ID',
    example: 110100
  }),
  districtId: z.coerce.number<number>().int().positive().optional().openapi({
    description: '区县ID',
    example: 110101
  }),
  basicSalary: z.coerce.number<number>({
    error: '基本工资必须大于0'
  }).positive({
    message: '基本工资必须大于0'
  }).openapi({
    description: '基本工资',
    example: 5000.00
  }),
  allowance: z.coerce.number<number>({
    error: '津贴补贴不能为负数'
  }).min(0, {
    message: '津贴补贴不能为负数'
  }).optional().openapi({
    description: '津贴补贴',
    example: 1000.00
  }),
  insurance: z.coerce.number<number>({
    error: '保险费用不能为负数'
  }).min(0, {
    message: '保险费用不能为负数'
  }).optional().openapi({
    description: '保险费用',
    example: 500.00
  }),
  housingFund: z.coerce.number<number>({
    error: '住房公积金不能为负数'
  }).min(0, {
    message: '住房公积金不能为负数'
  }).optional().openapi({
    description: '住房公积金',
    example: 800.00
  })
});

// 更新薪资DTO
export const UpdateSalarySchema = z.object({
  id: z.coerce.number<number>().int().positive().optional().openapi({
    description: '薪资ID',
    example: 1
  }),
  provinceId: z.coerce.number<number>({
    error: '请选择省份'
  }).int().positive({
    message: '请选择省份'
  }).optional().openapi({
    description: '省份ID',
    example: 110000
  }),
  cityId: z.coerce.number<number>({
    error: '请选择城市'
  }).int().positive({
    message: '请选择城市'
  }).optional().openapi({
    description: '城市ID',
    example: 110100
  }),
  districtId: z.coerce.number<number>().int().positive().optional().openapi({
    description: '区县ID',
    example: 110101
  }),
  basicSalary: z.coerce.number<number>({
    error: '基本工资必须大于0'
  }).positive({
    message: '基本工资必须大于0'
  }).optional().openapi({
    description: '基本工资',
    example: 5000.00
  }),
  allowance: z.coerce.number<number>({
    error: '津贴补贴不能为负数'
  }).min(0, {
    message: '津贴补贴不能为负数'
  }).optional().openapi({
    description: '津贴补贴',
    example: 1000.00
  }),
  insurance: z.coerce.number<number>({
    error: '保险费用不能为负数'
  }).min(0, {
    message: '保险费用不能为负数'
  }).optional().openapi({
    description: '保险费用',
    example: 500.00
  }),
  housingFund: z.coerce.number<number>({
    error: '住房公积金不能为负数'
  }).min(0, {
    message: '住房公积金不能为负数'
  }).optional().openapi({
    description: '住房公积金',
    example: 800.00
  })
});

// 查询薪资DTO
export const QuerySalarySchema = z.object({
  provinceId: z.coerce.number<number>().int().positive().optional().openapi({
    description: '省份ID',
    example: 110000
  }),
  cityId: z.coerce.number<number>().int().positive().optional().openapi({
    description: '城市ID',
    example: 110100
  }),
  districtId: z.coerce.number<number>().int().positive().optional().openapi({
    description: '区县ID',
    example: 110101
  }),
  skip: z.coerce.number<number>().int().min(0).default(0).optional().openapi({
    description: '跳过记录数',
    example: 0
  }),
  take: z.coerce.number<number>().int().min(1).max(100).default(10).optional().openapi({
    description: '获取记录数',
    example: 10
  })
});

// 按省份城市查询DTO
export const GetSalaryByProvinceCitySchema = z.object({
  provinceId: z.coerce.number<number>().int().positive().openapi({
    description: '省份ID',
    example: 110000
  }),
  cityId: z.coerce.number<number>().int().positive().openapi({
    description: '城市ID',
    example: 110100
  })
});

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

// 类型定义
export type SalaryLevel = z.infer<typeof SalaryLevelSchema>;
export type CreateSalaryDto = z.infer<typeof CreateSalarySchema>;
export type UpdateSalaryDto = z.infer<typeof UpdateSalarySchema>;
export type QuerySalaryDto = z.infer<typeof QuerySalarySchema>;
export type GetSalaryByProvinceCityDto = z.infer<typeof GetSalaryByProvinceCitySchema>;
export type DeleteSalaryDto = z.infer<typeof DeleteSalarySchema>;