import { toast } from 'sonner';

export interface WalletRuleProps {
  ID: string;
  id_wallet: string;
  id_group: string;
  max_transaction_per_day: number | null;
  balance_minimum: number | null;
  balance_maximum: number | null;
  credit_limit: number | null;
  monthly_limit: number | null;
  status: string;
}

export const initialStateWalletRule: {
  id_wallet: string;
  id_group: string;
  max_transaction_per_day: string | number | null;
  balance_minimum: string | number | null;
  balance_maximum: string | number | null;
  credit_limit: string | number | null;
  monthly_limit: string | number | null;
  status: string;
} = {
  id_wallet: '',
  id_group: '',
  max_transaction_per_day: null,
  balance_minimum: null,
  balance_maximum: null,
  credit_limit: null,
  monthly_limit: null,
  status: ''
};

export interface GroupProps {
  ID: string;
  is_bank: string;
  name: string;
  status: string;
}

export interface WalletProps {
  ID: string;
  name: string;
  id_currency: string;
  status: string;
}

export const validateFormsWalletRule = (
  formField: typeof initialStateWalletRule,
  setErrors: React.Dispatch<React.SetStateAction<Record<string, string>>>
) => {
  const requiredFields = [
    { key: 'id_wallet', label: 'Wallet' },
    { key: 'id_group', label: 'Group' },
    { key: 'max_transaction_per_day', label: 'Max Transaction Per Day' },
    { key: 'balance_minimum', label: 'Balance Minimum' },
    { key: 'balance_maximum', label: 'Balance Maximum' },
    { key: 'credit_limit', label: 'Credit Limit' },
    { key: 'monthly_limit', label: 'Monthly Limit' },
    { 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;
};
