import { z } from '@hono/zod-openapi';
import { OrderStatus, WorkStatus } from '@d8d/allin-enums';
import { DisabledPersonSchema } from '@d8d/allin-disability-module/schemas';

// 资产类型枚举 - 从实体移到schema，供前端使用
export enum AssetType {
  TAX = 'tax',
  SALARY = 'salary',
  JOB_RESULT = 'job_result',
  CONTRACT_SIGN = 'contract_sign',
  DISABILITY_CERT = 'disability_cert',
  OTHER = 'other',
}

// 资产文件类型枚举 - 从实体移到schema，供前端使用
export enum AssetFileType {
  IMAGE = 'image',
  VIDEO = 'video',
}

// 用工订单实体Schema
export const EmploymentOrderSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '订单ID',
    example: 1
  }),
  orderName: z.string().max(50).nullable().optional().openapi({
    description: '订单名称',
    example: '2024年Q1用工订单'
  }),
  platformId: z.number().int().positive().openapi({
    description: '用人平台ID',
    example: 1
  }),
  companyId: z.number().int().positive().openapi({
    description: '用人单位ID',
    example: 1
  }),
  channelId: z.number().int().positive().nullable().optional().openapi({
    description: '渠道ID',
    example: 1
  }),
  expectedStartDate: z.coerce.date().nullable().optional().openapi({
    description: '预计开始日期',
    example: '2024-01-01T00:00:00Z'
  }),
  actualStartDate: z.coerce.date().nullable().optional().openapi({
    description: '实际开始日期',
    example: '2024-01-01T00:00:00Z'
  }),
  actualEndDate: z.coerce.date().nullable().optional().openapi({
    description: '实际结束日期',
    example: '2024-12-31T00:00:00Z'
  }),
  orderStatus: z.nativeEnum(OrderStatus).default(OrderStatus.DRAFT).openapi({
    description: '订单状态：draft-草稿, confirmed-已确认, in_progress-进行中, completed-已完成, cancelled-已取消',
    example: OrderStatus.DRAFT
  }),
  workStatus: z.nativeEnum(WorkStatus).default(WorkStatus.NOT_WORKING).openapi({
    description: '工作状态：not_working-未就业, pre_working-待就业, working-已就业, resigned-已离职',
    example: WorkStatus.NOT_WORKING
  }),
  createTime: z.coerce.date().openapi({
    description: '创建时间',
    example: '2024-01-01T00:00:00Z'
  }),
  updateTime: z.coerce.date().openapi({
    description: '更新时间',
    example: '2024-01-01T00:00:00Z'
  }),
  orderPersons: z.array(z.lazy(() => OrderPersonSchema)).optional().openapi({
    description: '订单关联人员列表',
    example: []
  })
});

// 创建用工订单DTO
export const CreateEmploymentOrderSchema = z.object({
  orderName: z.string().max(50).optional().openapi({
    description: '订单名称',
    example: '2024年Q1用工订单'
  }),
  platformId: z.coerce.number().int().positive().openapi({
    description: '用人平台ID',
    example: 1
  }),
  companyId: z.coerce.number().int().positive().openapi({
    description: '用人单位ID',
    example: 1
  }),
  channelId: z.coerce.number().int().positive().optional().openapi({
    description: '渠道ID',
    example: 1
  }),
  expectedStartDate: z.coerce.date().optional().openapi({
    description: '预计开始日期',
    example: '2024-01-01T00:00:00Z'
  }),
  actualStartDate: z.coerce.date().optional().openapi({
    description: '实际开始日期',
    example: '2024-01-01T00:00:00Z'
  }),
  actualEndDate: z.coerce.date().optional().openapi({
    description: '实际结束日期',
    example: '2024-12-31T00:00:00Z'
  }),
  orderStatus: z.nativeEnum(OrderStatus).default(OrderStatus.DRAFT).optional().openapi({
    description: '订单状态：draft-草稿, confirmed-已确认, in_progress-进行中, completed-已完成, cancelled-已取消',
    example: OrderStatus.DRAFT
  }),
  workStatus: z.nativeEnum(WorkStatus).default(WorkStatus.NOT_WORKING).optional().openapi({
    description: '工作状态：not_working-未就业, pre_working-待就业, working-已就业, resigned-已离职',
    example: WorkStatus.NOT_WORKING
  })
});

// 更新用工订单DTO
export const UpdateEmploymentOrderSchema = z.object({
  id: z.coerce.number().int().positive().openapi({
    description: '订单ID',
    example: 1
  }),
  orderName: z.string().max(50).optional().openapi({
    description: '订单名称',
    example: '2024年Q1用工订单'
  }),
  platformId: z.coerce.number().int().positive().optional().openapi({
    description: '用人平台ID',
    example: 1
  }),
  companyId: z.coerce.number().int().positive().optional().openapi({
    description: '用人单位ID',
    example: 1
  }),
  channelId: z.coerce.number().int().positive().optional().openapi({
    description: '渠道ID',
    example: 1
  }),
  expectedStartDate: z.coerce.date().optional().openapi({
    description: '预计开始日期',
    example: '2024-01-01T00:00:00Z'
  }),
  actualStartDate: z.coerce.date().optional().openapi({
    description: '实际开始日期',
    example: '2024-01-01T00:00:00Z'
  }),
  actualEndDate: z.coerce.date().optional().openapi({
    description: '实际结束日期',
    example: '2024-12-31T00:00:00Z'
  }),
  orderStatus: z.nativeEnum(OrderStatus).optional().openapi({
    description: '订单状态：draft-草稿, confirmed-已确认, in_progress-进行中, completed-已完成, cancelled-已取消',
    example: OrderStatus.DRAFT
  }),
  workStatus: z.nativeEnum(WorkStatus).optional().openapi({
    description: '工作状态：not_working-未就业, pre_working-待就业, working-已就业, resigned-已离职',
    example: WorkStatus.NOT_WORKING
  })
});

// 订单人员关联实体Schema
export const OrderPersonSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '关联ID',
    example: 1
  }),
  orderId: z.number().int().positive().openapi({
    description: '订单ID',
    example: 1
  }),
  personId: z.number().int().positive().openapi({
    description: '残疾人ID',
    example: 1
  }),
  joinDate: z.coerce.date().openapi({
    description: '入职日期',
    example: '2024-01-01T00:00:00Z'
  }),
  actualStartDate: z.coerce.date().nullable().optional().openapi({
    description: '实际入职日期',
    example: '2024-01-15T00:00:00Z'
  }),
  leaveDate: z.coerce.date().nullable().optional().openapi({
    description: '离职日期',
    example: '2024-12-31T00:00:00Z'
  }),
  workStatus: z.nativeEnum(WorkStatus).default(WorkStatus.NOT_WORKING).openapi({
    description: '工作状态：not_working-未就业, pre_working-待就业, working-已就业, resigned-已离职',
    example: WorkStatus.NOT_WORKING
  }),
  salaryDetail: z.coerce.number().positive().openapi({
    description: '个人薪资',
    example: 5000.00
  }),
  // 残疾人员的详细信息
  person: DisabledPersonSchema.pick({
    id: true,
    name: true,
    gender: true,
    disabilityType: true,
    phone: true,
    disabilityId: true
  }).partial().nullable().optional().openapi({
    description: '残疾人员详细信息'
  })
});

// 创建订单人员关联DTO
export const CreateOrderPersonSchema = z.object({
  orderId: z.coerce.number().int().positive().openapi({
    description: '订单ID',
    example: 1
  }),
  personId: z.coerce.number().int().positive().openapi({
    description: '残疾人ID',
    example: 1
  }),
  joinDate: z.coerce.date().openapi({
    description: '入职日期',
    example: '2024-01-01T00:00:00Z'
  }),
  leaveDate: z.coerce.date().optional().openapi({
    description: '离职日期',
    example: '2024-12-31T00:00:00Z'
  }),
  workStatus: z.nativeEnum(WorkStatus).default(WorkStatus.NOT_WORKING).optional().openapi({
    description: '工作状态：not_working-未就业, pre_working-待就业, working-已就业, resigned-已离职',
    example: WorkStatus.NOT_WORKING
  }),
  salaryDetail: z.coerce.number().positive().openapi({
    description: '个人薪资',
    example: 5000.00
  })
});

// 批量添加人员DTO（不需要orderId，从URL参数获取）
export const BatchAddPersonItemSchema = z.object({
  personId: z.coerce.number().int().positive().openapi({
    description: '残疾人ID',
    example: 1
  }),
  joinDate: z.coerce.date().openapi({
    description: '入职日期',
    example: '2024-01-01T00:00:00Z'
  }),
  leaveDate: z.coerce.date().optional().openapi({
    description: '离职日期',
    example: '2024-12-31T00:00:00Z'
  }),
  workStatus: z.nativeEnum(WorkStatus).default(WorkStatus.NOT_WORKING).optional().openapi({
    description: '工作状态：not_working-未就业, pre_working-待就业, working-已就业, resigned-已离职',
    example: WorkStatus.NOT_WORKING
  }),
  salaryDetail: z.coerce.number().positive().openapi({
    description: '个人薪资',
    example: 5000.00
  })
});

// 订单人员资产实体Schema
export const OrderPersonAssetSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '关联ID',
    example: 1
  }),
  orderId: z.number().int().positive().openapi({
    description: '订单ID',
    example: 1
  }),
  personId: z.number().int().positive().openapi({
    description: '残疾人ID',
    example: 1
  }),
  assetType: z.nativeEnum(AssetType).openapi({
    description: '资产类型：tax-税务, salary-薪资, job_result-工作成果, contract_sign-合同签署, disability_cert-残疾证明, other-其他',
    example: AssetType.SALARY
  }),
  assetFileType: z.nativeEnum(AssetFileType).openapi({
    description: '资产文件类型：image-图片, video-视频',
    example: AssetFileType.IMAGE
  }),
  fileId: z.number().int().positive().openapi({
    description: '文件ID，引用files表',
    example: 1
  }),
  relatedTime: 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 CreateOrderPersonAssetSchema = z.object({
  orderId: z.coerce.number().int().positive().openapi({
    description: '订单ID',
    example: 1
  }),
  personId: z.coerce.number().int().positive().openapi({
    description: '残疾人ID',
    example: 1
  }),
  assetType: z.nativeEnum(AssetType).openapi({
    description: '资产类型：tax-税务, salary-薪资, job_result-工作成果, contract_sign-合同签署, disability_cert-残疾证明, other-其他',
    example: AssetType.SALARY
  }),
  assetFileType: z.nativeEnum(AssetFileType).openapi({
    description: '资产文件类型：image-图片, video-视频',
    example: AssetFileType.IMAGE
  }),
  fileId: z.coerce.number().int().positive().openapi({
    description: '文件ID，引用files表',
    example: 1
  }),
  relatedTime: z.coerce.date().optional().openapi({
    description: '关联时间',
    example: '2024-01-01T00:00:00Z'
  })
});

// 分页查询参数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
  })
});

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

// 删除订单人员DTO
export const DeleteOrderPersonSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '关联ID',
    example: 1
  })
});

// 删除订单人员资产DTO
export const DeleteOrderPersonAssetSchema = z.object({
  id: z.number().int().positive().openapi({
    description: '关联ID',
    example: 1
  })
});

// 类型定义
export type EmploymentOrder = z.infer<typeof EmploymentOrderSchema>;
export type CreateEmploymentOrderDto = z.infer<typeof CreateEmploymentOrderSchema>;
export type UpdateEmploymentOrderDto = z.infer<typeof UpdateEmploymentOrderSchema>;
export type OrderPerson = z.infer<typeof OrderPersonSchema>;
export type CreateOrderPersonDto = z.infer<typeof CreateOrderPersonSchema>;
export type OrderPersonAsset = z.infer<typeof OrderPersonAssetSchema>;
export type CreateOrderPersonAssetDto = z.infer<typeof CreateOrderPersonAssetSchema>;
export type PaginationQuery = z.infer<typeof PaginationQuerySchema>;
export type DeleteOrderDto = z.infer<typeof DeleteOrderSchema>;
export type DeleteOrderPersonDto = z.infer<typeof DeleteOrderPersonSchema>;
export type DeleteOrderPersonAssetDto = z.infer<typeof DeleteOrderPersonAssetSchema>;
export type UpdatePersonWorkStatusDto = z.infer<typeof UpdatePersonWorkStatusSchema>;

// 查询订单参数Schema
export const QueryOrderSchema = z.object({
  orderName: z.string().optional().openapi({
    description: '订单名称',
    example: '2024年Q1用工订单'
  }),
  platformId: z.coerce.number().int().positive().optional().openapi({
    description: '用人平台ID',
    example: 1
  }),
  companyId: z.coerce.number().int().positive().optional().openapi({
    description: '用人单位ID',
    example: 1
  }),
  channelId: z.coerce.number().int().positive().optional().openapi({
    description: '渠道ID',
    example: 1
  }),
  orderStatus: z.nativeEnum(OrderStatus).optional().openapi({
    description: '订单状态',
    example: OrderStatus.DRAFT
  }),
  startDate: z.string().optional().openapi({
    description: '开始日期（YYYY-MM-DD格式）',
    example: '2024-01-01'
  }),
  endDate: z.string().optional().openapi({
    description: '结束日期（YYYY-MM-DD格式）',
    example: '2024-12-31'
  }),
  page: z.coerce.number().int().min(1).default(1).optional().openapi({
    description: '页码',
    example: 1
  }),
  limit: z.coerce.number().int().min(1).max(100).default(10).optional().openapi({
    description: '每页数量',
    example: 10
  })
});

// 查询订单人员资产参数Schema
export const QueryOrderPersonAssetSchema = z.object({
  orderId: z.coerce.number().int().positive().optional().openapi({
    description: '订单ID',
    example: 1
  }),
  personId: z.coerce.number().int().positive().optional().openapi({
    description: '人员ID',
    example: 1
  }),
  assetType: z.nativeEnum(AssetType).optional().openapi({
    description: '资产类型',
    example: AssetType.SALARY
  }),
  assetFileType: z.nativeEnum(AssetFileType).optional().openapi({
    description: '资产文件类型',
    example: AssetFileType.IMAGE
  }),
  page: z.coerce.number().int().min(1).default(1).optional().openapi({
    description: '页码',
    example: 1
  }),
  limit: z.coerce.number().int().min(1).max(100).default(10).optional().openapi({
    description: '每页数量',
    example: 10
  })
});

// 为兼容性添加别名导出
export const CreateOrderSchema = CreateEmploymentOrderSchema;
export const UpdateOrderSchema = UpdateEmploymentOrderSchema;
export const BatchAddPersonsSchema = z.object({
  persons: z.array(BatchAddPersonItemSchema).openapi({
    description: '人员列表',
    example: [{ personId: 1, joinDate: '2024-01-01T00:00:00Z', salaryDetail: 5000 }]
  })
});

// 更新订单人员工作状态DTO
export const UpdatePersonWorkStatusSchema = z.object({
  orderId: z.coerce.number().int().positive().openapi({
    description: '订单ID',
    example: 1
  }),
  personId: z.coerce.number().int().positive().openapi({
    description: '人员ID',
    example: 1
  }),
  workStatus: z.nativeEnum(WorkStatus).openapi({
    description: '工作状态：not_working-未就业, pre_working-待就业, working-已就业, resigned-已离职',
    example: WorkStatus.WORKING
  })
});