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

export const createShiftSeriesSchema = z.object({
  title: z.string().min(1, 'Title is required'),
  description: z.string().optional(),
  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),

  // Recurrence
  frequency: z.nativeEnum(RecurrenceFrequency),
  daysOfWeek: z.array(z.number().int().min(0).max(6)).optional(), // 0=Sun, 6=Sat
  startDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, 'Start date must be YYYY-MM-DD'),
  endDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, 'End date must be YYYY-MM-DD').optional(),
}).refine(
  (data) => {
    // Weekly requires daysOfWeek
    if (data.frequency === RecurrenceFrequency.WEEKLY && !data.daysOfWeek?.length) {
      return false;
    }
    return true;
  },
  { message: 'Weekly recurrence requires at least one day of week', path: ['daysOfWeek'] }
);

export const updateShiftSeriesSchema = z.object({
  title: z.string().min(1).optional(),
  description: z.string().nullable().optional(),
  startTime: z.string().regex(/^\d{2}:\d{2}$/).optional(),
  endTime: z.string().regex(/^\d{2}:\d{2}$/).optional(),
  location: z.string().nullable().optional(),
  maxVolunteers: z.number().int().min(1).optional(),
  isPublic: z.boolean().optional(),
  cutId: z.string().nullable().optional(),
  editMode: z.enum(['THIS', 'FUTURE', 'ALL']),
  fromDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/).optional(), // Required for FUTURE
});

export type CreateShiftSeriesInput = z.infer<typeof createShiftSeriesSchema>;
export type UpdateShiftSeriesInput = z.infer<typeof updateShiftSeriesSchema>;
