import type { GradeRecordType, GradeRecordSemester } from "../types"
import type { GradeDraftData } from "../data-access-drafts"

const GRADE_TYPES: readonly GradeRecordType[] = ["exam", "quiz", "homework", "other"]
const SEMESTERS: readonly GradeRecordSemester[] = ["1", "2"]

export function isGradeType(v: string): v is GradeRecordType {
  return GRADE_TYPES.some((t) => t === v)
}

export function isSemester(v: string): v is GradeRecordSemester {
  return SEMESTERS.some((s) => s === v)
}

/**
 * Type guard for GradeDraftData (parsed from DB JSON column).
 * Ensures the structure is valid before accessing fields.
 */
export function isGradeDraftData(v: unknown): v is GradeDraftData {
  if (typeof v !== "object" || v === null) return false
  const obj = v
  return (
    "timestamp" in obj &&
    typeof obj.timestamp === "number" &&
    "scores" in obj &&
    typeof obj.scores === "object" &&
    obj.scores !== null
  )
}
