import { toast } from 'sonner';

export const initialStatePointiers: {
  name: string;
  description: string;
  minimal_point: number | null;
  status: string;
  created_by: string;
  created_at: string;
} = {
  name: '',
  description: '',
  minimal_point: null,
  status: '',
  created_by: '',
  created_at: ''
};

export const validateFormPointiers = (
  formField: typeof initialStatePointiers,
  setErrors: React.Dispatch<React.SetStateAction<Record<string, string>>>
) => {
  const requiredFields = [
    { key: 'name', label: 'Name' },
    { key: 'description', label: 'Description' },
    { key: 'minimal_point', label: 'Minimal Point' },
    { key: 'status', label: 'Status' }
  ];

  const newErrors: Record<string, string> = {};
  let isValid = true;

  requiredFields.forEach(({ key, label }) => {
    if (
      formField[key as keyof typeof formField] === '' ||
      formField[key as keyof typeof formField] === null ||
      formField[key as keyof typeof formField] === undefined
    ) {
      newErrors[key] = `${label} is required`;
      toast.error(`${label} is required`);
      isValid = false;
    }
  });

  setErrors(newErrors);
  return isValid;
};
