import * as Yup from "yup";
import dayjs from "dayjs";
import { Exam } from "../../../../../../../enums/Validation";

export const getInitialValues = (objectToEdit: any): any => {
  return {
    id: objectToEdit?.id ?? null,
    registration_record_id: objectToEdit?.registration_record?.id ?? null,
    title: objectToEdit?.title ?? null,
    exam_type_id:
      objectToEdit?.exam_type?.id ?? objectToEdit?.exam_type_id ?? null,
    subject_id: objectToEdit?.subject?.id ?? objectToEdit?.subject_id ?? null,
    max_grade: objectToEdit?.max_grade ?? null,
    grade_to_pass: objectToEdit?.grade_to_pass ?? null,
    date: objectToEdit?.date
      ? dayjs(objectToEdit?.date, "YYYY/MM/DD")
      : dayjs(Date()),
    duration: objectToEdit?.duration
      ? dayjs(objectToEdit?.duration, "H:mm")
      : null,
  };
};

export const getValidationSchema = () => {
  // validate input
  return Yup.object().shape({
    // registration_record_id: Yup.string().required("validation.Student_name_is_required"),
    title: Yup.string().required("validation.Title_is_required"),
    exam_type_id: Yup.string().required("validation.Exam_type_is_required"),
    subject_id: Yup.string().required("validation.Subject_is_required"),

    grade_to_pass: Yup.number()
      .required("validation.Passing_grade_is_required")
      .typeError("validation.Must_be_a_number")
      .max(Exam?.MAX_VALUE, `Value_must_not_exceed_10000`),
    max_grade: Yup.number()
      .required("validation.Maximum_grade_is_required")
      .typeError("validation.Must_be_a_number")
      .max(Exam?.MAX_VALUE, `Value_must_not_exceed_10000`)
      .min(
        Yup.ref("grade_to_pass"),
        "validation.grade_to_pass_must_be_less_than_max_grade",
      ),
    date: Yup.string().required("validation.Date_is_required"),
    // duration: Yup.string().required("validation.Duration_is_required"),
  });
};

export const getValidationSchemaADD = () => {
  // validate input
  return Yup.object().shape({
    // registration_record_id: Yup.string().required("validation.Student_name_is_required"),
    title: Yup.string().required("validation.Title_is_required"),
    exam_type_id: Yup.string().required("validation.Exam_type_is_required"),
    subject_id: Yup.string().required("validation.Subject_is_required"),

    grade_to_pass: Yup.number()
      .required("validation.Passing_grade_is_required")
      .typeError("validation.Must_be_a_number")
      .max(Exam?.MAX_VALUE, `Value_must_not_exceed_10000`),
    max_grade: Yup.number()
      .required("validation.Maximum_grade_is_required")
      .typeError("validation.Must_be_a_number")
      .max(Exam?.MAX_VALUE, `Value_must_not_exceed_10000`)
      .min(
        Yup.ref("grade_to_pass"),
        "validation.grade_to_pass_must_be_less_than_max_grade",
      ),
    date: Yup.string().required("validation.Date_is_required"),
    // duration: Yup.string().required("validation.Duration_is_required"),
  });
};
