import { useAuthModalWorkflow } from '@/business/auth/hooks';
import { AuthModeToggle } from '@/components/sign-in-modal/AuthModeToggle';
import { AuthStatusMessage } from '@/components/sign-in-modal/AuthStatusMessage';
import { SignInForm } from '@/components/sign-in-modal/SignInForm';
import { SignInModalFrame } from '@/components/sign-in-modal/SignInModalFrame';
import { SignInModalHeader } from '@/components/sign-in-modal/SignInModalHeader';
import { SignupForm } from '@/components/sign-in-modal/SignupForm';
import { useAuth } from '@/contexts/useAuth';
import type { SignInModalProps } from '@/business/app-shell/types';

const SignInModal = ({ isOpen, onClose }: SignInModalProps) => {
  const { signIn, signUp } = useAuth();
  const modal = useAuthModalWorkflow({ signIn, signUp, onClose });
  const { state, actions } = modal;

  if (!isOpen) {
    return null;
  }

  return (
    <SignInModalFrame onClose={actions.handleClose}>
      <SignInModalHeader state={state} />
      <AuthStatusMessage error={state.error} success={state.success} />

      {state.mode === 'signin' ? (
        <SignInForm state={state} actions={actions} />
      ) : (
        <SignupForm state={state} actions={actions} />
      )}

      <AuthModeToggle state={state} actions={actions} />
    </SignInModalFrame>
  );
};

export default SignInModal;
