import { z } from 'zod';
import { DisabledStatus } from '../../share/types';
import { ActivityType } from './activity.entity';
import { locationInfoSchema } from '../locations/location.schema';

// 活动创建Schema
export const createActivitySchema = z.object({
  name: z.string().min(1, '活动名称不能为空').max(255, '活动名称不能超过255个字符'),
  description: z.string().max(1000, '活动描述不能超过1000个字符').optional().nullable(),
  type: z.nativeEnum(ActivityType, {
    message: '活动类型必须是departure(去程)或return(返程)'
  }),
  startDate: z.coerce.date('开始日期格式不正确'),
  endDate: z.coerce.date('结束日期格式不正确'),
  venueLocationId: z.number().int().positive('举办地点ID必须为正整数'),
}).refine((data) => new Date(data.startDate) < new Date(data.endDate), {
  message: '开始日期必须早于结束日期',
  path: ['endDate'],
});

// 活动更新Schema
export const updateActivitySchema = z.object({
  name: z.string().min(1, '活动名称不能为空').max(255, '活动名称不能超过255个字符').optional(),
  description: z.string().max(1000, '活动描述不能超过1000个字符').optional().nullable(),
  type: z.nativeEnum(ActivityType, {
    message: '活动类型必须是departure(去程)或return(返程)'
  }).optional(),
  startDate: z.coerce.date('开始日期格式不正确').optional(),
  endDate: z.coerce.date('结束日期格式不正确').optional(),
  venueLocationId: z.number().int().positive('举办地点ID必须为正整数').optional(),
  isDisabled: z.nativeEnum(DisabledStatus).optional(),
}).refine((data) => {
  if (data.startDate && data.endDate) {
    return new Date(data.startDate) < new Date(data.endDate);
  }
  return true;
}, {
  message: '开始日期必须早于结束日期',
  path: ['endDate'],
});

// 活动获取Schema
export const getActivitySchema = z.object({
  id: z.number().int().positive('ID必须为正整数'),
  name: z.string().min(1, '活动名称不能为空').max(255, '活动名称不能超过255个字符'),
  description: z.string().max(1000, '活动描述不能超过1000个字符').optional().nullable(),
  type: z.nativeEnum(ActivityType, {
    message: '活动类型必须是departure(去程)或return(返程)'
  }),
  startDate: z.coerce.date(),
  endDate: z.coerce.date(),
  venueLocationId: z.number().int().positive('举办地点ID必须为正整数'),
  venueLocation: locationInfoSchema.optional().nullable(),
  isDisabled: z.nativeEnum(DisabledStatus),
  createdAt: z.coerce.date(),
  updatedAt: z.coerce.date(),
  createdBy: z.number().int().nullable(),
  updatedBy: z.number().int().nullable(),
});

// 活动列表查询Schema
export const listActivitiesSchema = z.object({
  keyword: z.string().optional(),
  type: z.nativeEnum(ActivityType).optional(),
  isDisabled: z.coerce.number().int().optional(),
  page: z.coerce.number().int().min(1).default(1),
  pageSize: z.coerce.number().int().min(1).max(100).default(20),
  sortBy: z.enum(['name', 'startDate', 'endDate', 'createdAt']).default('createdAt'),
  sortOrder: z.enum(['ASC', 'DESC']).default('DESC'),
});

// 活动列表返回Schema
export const activityListResponseSchema = z.object({
  id: z.number().int().positive('ID必须为正整数'),
  name: z.string().min(1, '活动名称不能为空').max(255, '活动名称不能超过255个字符'),
  description: z.string().max(1000, '活动描述不能超过1000个字符').optional().nullable(),
  type: z.nativeEnum(ActivityType, {
    message: '活动类型必须是departure(去程)或return(返程)'
  }),
  startDate: z.coerce.date(),
  endDate: z.coerce.date(),
  venueLocationId: z.number().int().positive('举办地点ID必须为正整数'),
  venueLocation: locationInfoSchema.optional().nullable(),
  isDisabled: z.nativeEnum(DisabledStatus),
  createdAt: z.coerce.date(),
  updatedAt: z.coerce.date(),
  createdBy: z.number().int().nullable(),
  updatedBy: z.number().int().nullable(),
});

// 活动删除Schema
export const deleteActivitySchema = z.object({
  id: z.number().int().positive('ID必须为正整数'),
});

// 活动启用/禁用Schema
export const toggleActivityStatusSchema = z.object({
  id: z.number().int().positive('ID必须为正整数'),
  isDisabled: z.nativeEnum(DisabledStatus),
});

// 导出类型
export type CreateActivityInput = z.infer<typeof createActivitySchema>;
export type UpdateActivityInput = z.infer<typeof updateActivitySchema>;
export type GetActivityInput = z.infer<typeof getActivitySchema>;
export type ListActivitiesInput = z.infer<typeof listActivitiesSchema>;
export type DeleteActivityInput = z.infer<typeof deleteActivitySchema>;
export type ToggleActivityStatusInput = z.infer<typeof toggleActivityStatusSchema>;