import { gradeHistory, positionHistory, salaryEffectiveInfo, teamHistory } from "@/app/api/staff/actions";
import { GridRowId, GridRowModel } from "@mui/x-data-grid";
import dayjs, { Dayjs } from "dayjs";
import { TeamModalRow } from "./TeamHistoryModal";
import { GradeModalRow } from "./GradeHistoryModal";
import { PositionModalRow } from "./PositionHistoryModal";
import { SeModalRow } from "./SalaryEffectiveModel";
export type ValidateError = {
    from: number[]
    to: number[]
}

type RowModel = Partial<TeamModalRow & GradeModalRow & PositionModalRow & SeModalRow> 

type AllFields = Partial<teamHistory & gradeHistory & positionHistory & salaryEffectiveInfo>

export type StaffEntryError = {
    [field in keyof AllFields]?: string;
  };

export const validateRowAndRowBefore = (
    prevRow: RowModel | null,
    currRow: RowModel
): StaffEntryError | undefined => {
  const error: StaffEntryError = {}

  if (prevRow) {
    if ('from' in currRow && currRow.from !== undefined) {
      if (dayjs(prevRow.from).diff(dayjs(currRow.from)) == 0) {
          error.from = "The date should not be the same as last entry"
      }
    } else if ('date' in currRow && currRow.date !== undefined) {
      if (dayjs(prevRow.date).diff(dayjs(currRow.date)) == 0) {
        error.date = "The date should not be the same as last entry"
    }
    }
  }
  console.log(currRow)
  if ('from' in currRow && !currRow.from) {
      error.from = "The date cannot be empty"
  }
  if ('date' in currRow && !currRow.date) {
    error.date = "The date cannot be empty"
  }
  // Check specific fields based on row type
  if ('grade' in currRow && !currRow.grade) {
      error.grade = "Grade cannot be empty"
  }
  if ('position' in currRow && !currRow.position) {
      error.position = "Position cannot be empty"
  }
  if ('team' in currRow && !currRow.team) {
      error.team = "Team cannot be empty"
  }
  console.log("error")
  console.log(error)
  console.log(currRow.from)

  return Object.keys(error).length > 0 ? error : undefined;

}