import { z } from "zod"

export const UpsertDepartmentSchema = z.object({
  name: z.string().trim().min(1).max(255),
  description: z.string().trim().max(5000).optional().nullable(),
})

export const UpsertAcademicYearSchema = z
  .object({
    name: z.string().trim().min(1).max(100),
    startDate: z.string().min(1),
    endDate: z.string().min(1),
    isActive: z.union([z.literal("on"), z.literal("true"), z.literal("false"), z.string()]).optional(),
  })
  .transform((v) => ({
    name: v.name,
    startDate: v.startDate,
    endDate: v.endDate,
    isActive: v.isActive === "on" || v.isActive === "true",
  }))
  .refine((v) => new Date(v.startDate).getTime() <= new Date(v.endDate).getTime(), {
    message: "startDate must be before endDate",
  })

export const UpsertSchoolSchema = z.object({
  name: z.string().trim().min(1).max(255),
  code: z.string().trim().max(50).optional().nullable(),
})

export const UpsertGradeSchema = z
  .object({
    schoolId: z.string().trim().min(1),
    name: z.string().trim().min(1).max(100),
    order: z.union([z.string(), z.number()]).optional().nullable(),
    gradeHeadId: z.string().trim().optional().nullable(),
    teachingHeadId: z.string().trim().optional().nullable(),
  })
  .transform((v) => ({
    schoolId: v.schoolId,
    name: v.name,
    order:
      typeof v.order === "number"
        ? v.order
        : typeof v.order === "string" && v.order.trim().length > 0
          ? Number(v.order)
          : 0,
    gradeHeadId: v.gradeHeadId && v.gradeHeadId.length > 0 ? v.gradeHeadId : null,
    teachingHeadId: v.teachingHeadId && v.teachingHeadId.length > 0 ? v.teachingHeadId : null,
  }))
  .refine((v) => Number.isFinite(v.order) && Number.isInteger(v.order) && v.order >= 0, {
    message: "order must be a non-negative integer",
  })

export const PromoteGradesSchema = z.object({
  schoolId: z.string().trim().min(1),
})
