/**
 * Hook for managing modal states in CreateRequest
 */

import { useState } from 'react';
import {
  ValidationModalState,
  PolicyViolationModalState,
  DocumentErrorModalState,
} from '../types/createRequest.types';

export function useRequestModals() {
  const [validationModal, setValidationModal] = useState<ValidationModalState>({
    open: false,
    type: 'error',
    email: '',
    message: '',
  });

  const [policyViolationModal, setPolicyViolationModal] =
    useState<PolicyViolationModalState>({
      open: false,
      violations: [],
    });

  const [documentErrorModal, setDocumentErrorModal] =
    useState<DocumentErrorModalState>({
      open: false,
      errors: [],
    });

  const openValidationModal = (
    type: 'error' | 'self-assign' | 'not-found',
    email: string,
    message: string = ''
  ) => {
    setValidationModal({
      open: true,
      type,
      email,
      message,
    });
  };

  const closeValidationModal = () => {
    setValidationModal((prev) => ({ ...prev, open: false }));
  };

  const openPolicyViolationModal = (
    violations: Array<{
      type: string;
      message: string;
      currentValue?: number;
      maxValue?: number;
    }>
  ) => {
    setPolicyViolationModal({
      open: true,
      violations,
    });
  };

  const closePolicyViolationModal = () => {
    setPolicyViolationModal({ open: false, violations: [] });
  };

  const openDocumentErrorModal = (
    errors: Array<{ fileName: string; reason: string }>
  ) => {
    setDocumentErrorModal({
      open: true,
      errors,
    });
  };

  const closeDocumentErrorModal = () => {
    setDocumentErrorModal({ open: false, errors: [] });
  };

  return {
    validationModal,
    policyViolationModal,
    documentErrorModal,
    openValidationModal,
    closeValidationModal,
    openPolicyViolationModal,
    closePolicyViolationModal,
    openDocumentErrorModal,
    closeDocumentErrorModal,
  };
}

