import { z } from 'zod';
import { ShiftStatus, ShiftKind } from '@prisma/client';

export const createShiftSchema = z.object({
  title: z.string().min(1, 'Title is required'),
  description: z.string().optional(),
  date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, 'Date must be YYYY-MM-DD'),
  startTime: z.string().regex(/^\d{2}:\d{2}$/, 'Start time must be HH:MM'),
  endTime: z.string().regex(/^\d{2}:\d{2}$/, 'End time must be HH:MM'),
  location: z.string().optional(),
  maxVolunteers: z.number().int().min(1, 'Must have at least 1 volunteer spot'),
  isPublic: z.boolean().optional().default(false),
  cutId: z.string().optional(),
  kind: z.nativeEnum(ShiftKind).optional().default(ShiftKind.CANVASS),
  ticketedEventId: z.string().optional(),
});

export const updateShiftSchema = z.object({
  title: z.string().min(1).optional(),
  description: z.string().nullable().optional(),
  date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, 'Date must be YYYY-MM-DD').optional(),
  startTime: z.string().regex(/^\d{2}:\d{2}$/, 'Start time must be HH:MM').optional(),
  endTime: z.string().regex(/^\d{2}:\d{2}$/, 'End time must be HH:MM').optional(),
  location: z.string().nullable().optional(),
  maxVolunteers: z.number().int().min(1).optional(),
  isPublic: z.boolean().optional(),
  status: z.nativeEnum(ShiftStatus).optional(),
  cutId: z.string().nullable().optional(),
  kind: z.nativeEnum(ShiftKind).optional(),
  ticketedEventId: z.string().nullable().optional(),
});

export const listShiftsSchema = z.object({
  page: z.coerce.number().int().positive().default(1),
  limit: z.coerce.number().int().positive().max(100).default(20),
  search: z.string().optional(),
  status: z.nativeEnum(ShiftStatus).optional(),
  kind: z.nativeEnum(ShiftKind).optional(),
  upcoming: z.coerce.boolean().optional(),
  sortBy: z.enum(['date', 'createdAt', 'title']).optional().default('date'),
  sortOrder: z.enum(['asc', 'desc']).optional().default('desc'),
});

export const addSignupSchema = z.object({
  userEmail: z.string().email('Valid email is required'),
  userName: z.string().optional(),
});

export const publicSignupSchema = z.object({
  email: z.string().email('Valid email is required'),
  name: z.string().min(1, 'Name is required'),
  phone: z.string().optional(),
});

export type CreateShiftInput = z.infer<typeof createShiftSchema>;
export type UpdateShiftInput = z.infer<typeof updateShiftSchema>;
export type ListShiftsInput = z.infer<typeof listShiftsSchema>;
export type AddSignupInput = z.infer<typeof addSignupSchema>;
export type PublicSignupInput = z.infer<typeof publicSignupSchema>;
