import * as Yup from "yup";
import dayjs from "dayjs";

export const getInitialValues = (objectToEdit: any): any => {
  // console.log(objectToEdit,"objectToEdit");

  return {
    id: objectToEdit?.id ?? null,
    title: objectToEdit?.title ?? null,
    content: objectToEdit?.content ?? null,
    due_date: objectToEdit?.due_date
      ? dayjs(objectToEdit?.due_date, "YYYY/MM/DD")
      : null,
    assigning_date: objectToEdit?.assigning_date
      ? dayjs(objectToEdit?.assigning_date, "YYYY/MM/DD")
      : null,
    subject_id: objectToEdit?.subject?.id ?? null,
    teacher_id: objectToEdit?.teacher?.id ?? null,
    attachments: objectToEdit?.homeworkAttachments ?? null,
  };
};

export const getValidationSchema = () => {
  // validate input
  return Yup.object().shape({
    title: Yup.string().required("validation.required"),
    content: Yup.string().required("validation.required"),
    assigning_date: Yup.date().required("validation.required"),
    due_date: Yup.date()
      .required("validation.required")
      .min(
        Yup.ref("assigning_date"),
        "validation.Due_date_must_be_before_assigning_date",
      ),
    subject_id: Yup.string().required("validation.required"),
    teacher_id: Yup.string().required("validation.required"),
  });
};
