import { toast } from 'sonner';

export const initialStateReward: {
  name: string;
  type: string;
  amount: number | null;
  status: string;
  created_by: string;
  created_at: string;
} = {
  name: '',
  type: '',
  amount: null,
  status: '',
  created_by: '',
  created_at: ''
};

export const validateFormReward = (
  formField: typeof initialStateReward,
  setErrors: React.Dispatch<React.SetStateAction<Record<string, string>>>
) => {
  const requiredFields = [
    { key: 'name', label: 'Name' },
    { key: 'type', label: 'Type' },
    { key: 'amount', label: 'Amount' },
    { 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;
};
