import { z } from "zod"

export const SchedulingRuleSchema = z.object({
  classId: z.string().min(1),
  maxDailyHours: z.coerce.number().int().min(1).max(24).optional(),
  maxContinuousHours: z.coerce.number().int().min(1).max(12).optional(),
  lunchBreakStart: z.string().optional(),
  lunchBreakEnd: z.string().optional(),
  morningStart: z.string().optional(),
  afternoonEnd: z.string().optional(),
  avoidBackToBack: z.coerce.boolean().optional(),
  balancedSubjects: z.coerce.boolean().optional(),
})

export type SchedulingRuleInput = z.infer<typeof SchedulingRuleSchema>

export const ScheduleChangeStatusEnum = z.enum([
  "pending",
  "approved",
  "rejected",
  "completed",
])

export const ScheduleChangeSchema = z.object({
  classId: z.string().min(1),
  originalScheduleId: z.string().optional(),
  originalTeacherId: z.string().optional(),
  substituteTeacherId: z.string().optional(),
  originalDate: z.string().optional(),
  newDate: z.string().optional(),
  newStartTime: z.string().optional(),
  newEndTime: z.string().optional(),
  reason: z.string().min(1, "Reason is required"),
})

export type ScheduleChangeInput = z.infer<typeof ScheduleChangeSchema>

export const AutoScheduleParamsSchema = z.object({
  classId: z.string().min(1),
  rules: SchedulingRuleSchema,
  subjects: z
    .array(
      z.object({
        subjectId: z.string().min(1),
        subjectName: z.string().min(1),
        weeklyHours: z.coerce.number().int().min(1),
        teacherId: z.string().optional(),
      })
    )
    .min(1, "At least one subject is required"),
  teachers: z.array(
    z.object({
      id: z.string().min(1),
      name: z.string().min(1),
    })
  ),
  classrooms: z.array(
    z.object({
      id: z.string().min(1),
      name: z.string().min(1),
    })
  ),
  timeSlots: z
    .array(
      z.object({
        weekday: z.coerce.number().int().min(1).max(7),
        startTime: z.string().min(1),
        endTime: z.string().min(1),
      })
    )
    .min(1, "At least one time slot is required"),
})

export type AutoScheduleParamsInput = z.infer<typeof AutoScheduleParamsSchema>

export const ApproveScheduleChangeSchema = z.object({
  changeId: z.string().min(1),
  reason: z.string().optional(),
})

export type ApproveScheduleChangeInput = z.infer<typeof ApproveScheduleChangeSchema>
