import { type FC } from 'react'
import Input from '../../../components/Input'
import { useTranslation } from 'react-i18next'
import { useFormik } from 'formik'
import * as Yup from 'yup'
import { useAuthStore } from '../store/AuthStore'
import Error from '../../../components/Error'
import Button from '../../../components/Button'
import { useLoginWithOtp } from '../hooks/useAuthData'
import { toast } from 'react-toastify'
import { type ErrorType } from '../../../helpers/types'

type LoginStep1FormType = {
    phone: string;
    slug: string;
}

const LoginStep1: FC = () => {

    const { t } = useTranslation('global')
    const { setPhone, phone, setStepLogin, setSlug } = useAuthStore()
    const loginWithOtp = useLoginWithOtp()

    const formik = useFormik<LoginStep1FormType>({
        initialValues: {
            phone: phone,
            slug: ''
        },
        validationSchema: Yup.object({
            phone: Yup.string()
                .required(t('errors.required')),
            slug: Yup.string()
                .required(t('errors.required'))
        }),
        onSubmit(values) {
            setPhone(values.phone)
            setSlug(values.slug)
            loginWithOtp.mutate(values, {
                onSuccess() {
                    setStepLogin(2)
                    toast.success(t('auth.otp_sent'))
                },
                onError(error: ErrorType) {
                    toast.error(error?.response?.data?.error?.message[0])
                },
            })
        },
    })

    return (
        <div>
            <div className='mt-5'>
                <h2 className='text-2xl font-bold'>
                    {t('auth.welcome')}
                </h2>
                <p className='text-description text-sm mt-2'>
                    {t('auth.enter_info_login')}
                </p>
            </div>

            <div className='mt-16'>
                <Input
                    label={t('auth.mobile')}
                    placeholder={t('auth.enter_mobile')}
                    type='text'
                    className='text-right'
                    name='phone'
                    onChange={formik.handleChange}
                    value={formik.values.phone}
                />

                {
                    formik.touched.phone && formik.errors.phone &&
                    <Error
                        errorText={formik.errors.phone}
                    />
                }

            </div>

            <div className='mt-4'>
                <Input
                    label={t('auth.slug')}
                    placeholder={t('auth.enter_slug')}
                    type='text'
                    className='text-right'
                    name='slug'
                    onChange={formik.handleChange}
                    value={formik.values.slug}
                />

                {
                    formik.touched.slug && formik.errors.slug &&
                    <Error
                        errorText={formik.errors.slug}
                    />
                }

            </div>


            <Button
                label={t('auth.next')}
                className='mt-8'
                onClick={() => formik.handleSubmit()}
                isloading={loginWithOtp.isPending}
            />

        </div>
    )
}

export default LoginStep1