import { z } from 'zod';
import { OrderStatus, PaymentStatus } from '../../share/order.types';



// 路线快照Schema
export const RouteSnapshotSchema = z.object({
  id: z.number(),
  name: z.string(),
  pickupPoint: z.string(),
  dropoffPoint: z.string(),
  departureTime: z.coerce.date(),
  price: z.coerce.number(),
  vehicleType: z.string(),
  travelMode: z.string()
});

// 创建订单Schema
export const OrderCreateSchema = z.object({
  routeId: z.number().int().positive(),
  passengerCount: z.number().int().min(1),
  totalAmount: z.number().positive(),
  passengerSnapshots: z.array(z.any()),
  routeSnapshot: RouteSnapshotSchema,
  createdBy: z.number().int().positive().optional()
});

// 更新订单Schema
export const OrderUpdateSchema = z.object({
  status: z.nativeEnum(OrderStatus).optional(),
  paymentStatus: z.nativeEnum(PaymentStatus).optional(),
  updatedBy: z.number().int().positive().optional()
});

// 获取订单Schema
export const OrderGetSchema = z.object({
  id: z.number().int().positive()
});

// 订单列表查询Schema
export const OrderListSchema = z.object({
  page: z.coerce.number().int().positive().default(1),
  pageSize: z.coerce.number().int().positive().max(100).default(20),
  status: z.nativeEnum(OrderStatus).optional(),
  paymentStatus: z.nativeEnum(PaymentStatus).optional(),
  search: z.string().optional()
});

// 订单响应Schema
export const OrderResponseSchema = z.object({
  id: z.number(),
  userId: z.number(),
  routeId: z.number(),
  passengerCount: z.number(),
  totalAmount: z.coerce.number(),
  status: z.nativeEnum(OrderStatus),
  paymentStatus: z.nativeEnum(PaymentStatus),
  passengerSnapshots: z.array(z.any()),
  routeSnapshot: RouteSnapshotSchema,
  createdBy: z.number().nullable().optional(),
  updatedBy: z.number().nullable().optional(),
  createdAt: z.date(),
  updatedAt: z.date(),
  user: z.object({
    id: z.number(),
    username: z.string(),
    phone: z.string().nullable().optional()
  }).optional(),
  route: z.object({
    id: z.number(),
    name: z.string(),
    description: z.string().nullable().optional()
  }).optional()
});

// 订单统计Schema
export const OrderStatsSchema = z.object({
  total: z.number(),
  pendingPayment: z.number(),
  waitingDeparture: z.number(),
  inProgress: z.number(),
  completed: z.number(),
  cancelled: z.number()
});

export type OrderCreateInput = z.infer<typeof OrderCreateSchema>;
export type OrderUpdateInput = z.infer<typeof OrderUpdateSchema>;
export type OrderGetInput = z.infer<typeof OrderGetSchema>;
export type OrderListInput = z.infer<typeof OrderListSchema>;
export type OrderResponse = z.infer<typeof OrderResponseSchema>;
export type OrderStats = z.infer<typeof OrderStatsSchema>;