import { useState } from 'react';
import type { AuthType } from '../../types/authTypes';
import { ForgettedPasswordInfo } from './ForgettedPasswordInfo';
import { ForgetPasswordOtp } from './ForgetPasswordOtp';
import { ChangePassword } from './ChangePassword';
import type { CountryCode } from '@/types/commonTypes';
import { useNavigate } from 'react-router-dom';

export const ForgetPasswordContainer = () => {
  const navigate = useNavigate();
  const [forgetPassCurrentStep, setForgetPassCurrentStep] = useState<
    'enterInfo' | 'verifyOtp' | 'setPassword'
  >('enterInfo');
  const [forgettedPasswordInfo, setForgettedPasswordInfo] =
    useState<string>('');
  const [infoCountryCode, setInfoCountryCode] = useState<CountryCode>('+98');
  const [infoType, setInfoType] = useState<AuthType>('email');

  const handleVerifyOtp = () => {
    setForgetPassCurrentStep('verifyOtp');
  };

  const handleEditInfo = () => {
    setForgetPassCurrentStep('enterInfo');
  };

  const handleOtpVerified = () => {
    setForgetPassCurrentStep('setPassword');
  };

  const handlePasswordChanged = () => {
    navigate('/login');
  };

  return (
    <>
      {forgetPassCurrentStep === 'enterInfo' && (
        <ForgettedPasswordInfo
          infoType={infoType}
          setInfoType={setInfoType}
          forgettedPasswordInfo={forgettedPasswordInfo}
          setForgettedPasswordInfo={setForgettedPasswordInfo}
          onVerifyOtp={handleVerifyOtp}
          countryCode={infoCountryCode}
          setCountryCode={setInfoCountryCode}
        />
      )}

      {forgetPassCurrentStep === 'verifyOtp' && (
        <ForgetPasswordOtp
          countryCode={infoCountryCode}
          infoType={infoType}
          onEditInfo={handleEditInfo}
          forgettedPasswordInfo={forgettedPasswordInfo}
          onOTPVerified={handleOtpVerified}
        />
      )}

      {forgetPassCurrentStep === 'setPassword' && (
        <ChangePassword
          onEditInfo={handleEditInfo}
          forgettedPasswordInfo={forgettedPasswordInfo}
          onPasswordChanged={handlePasswordChanged}
          infoType={infoType}
          countryCode={infoCountryCode}
        />
      )}
    </>
  );
};
