import { z } from "zod"

export const AttendanceStatusEnum = z.enum([
  "present",
  "absent",
  "late",
  "early_leave",
  "excused",
  "school_activity",
])

/**
 * L-6 节次考勤：节次枚举。
 * - morning_reading: 早读
 * - morning: 上午
 * - afternoon: 下午
 * - evening: 晚自习
 * - full_day: 全日（默认值，向后兼容）
 */
export const AttendancePeriodEnum = z.enum([
  "morning_reading",
  "morning",
  "afternoon",
  "evening",
  "full_day",
])

export const RecordAttendanceSchema = z.object({
  studentId: z.string().min(1),
  classId: z.string().min(1),
  date: z.string().min(1),
  status: AttendanceStatusEnum,
  /** L-6：节次，未传时默认 full_day */
  period: AttendancePeriodEnum.default("full_day"),
  remark: z.string().optional(),
  reason: z.string().max(255).optional(),
  scheduleId: z.string().optional(),
})

export type RecordAttendanceInput = z.infer<typeof RecordAttendanceSchema>

export const BatchRecordAttendanceSchema = z.object({
  records: z.array(RecordAttendanceSchema),
})

export type BatchRecordAttendanceInput = z.infer<typeof BatchRecordAttendanceSchema>

export const UpdateAttendanceSchema = z.object({
  status: AttendanceStatusEnum.optional(),
  period: AttendancePeriodEnum.optional(),
  remark: z.string().optional(),
  reason: z.string().max(255).optional(),
  scheduleId: z.string().optional(),
})

export type UpdateAttendanceInput = z.infer<typeof UpdateAttendanceSchema>

export const AttendanceRuleSchema = z.object({
  classId: z.string().min(1),
  lateThresholdMinutes: z.coerce.number().int().min(0).optional(),
  earlyLeaveThresholdMinutes: z.coerce.number().int().min(0).optional(),
  enableAutoMark: z.coerce.boolean().optional(),
  attendanceRateThreshold: z.coerce.number().int().min(0).max(100).optional(),
  consecutiveAbsenceThreshold: z.coerce.number().int().min(1).optional(),
})

export type AttendanceRuleInput = z.infer<typeof AttendanceRuleSchema>
