import { type FC, useEffect, useState } from 'react'
import { useGetSiteSetting, useUpdateSiteSetting } from './hooks/useSettingData'
import { SiteSettingPageEnum } from './enum/Enum'
import { useFormik } from 'formik'
import type { UpdateSiteSetting } from './types/Types'
import { toast } from 'react-toastify'
import { Pages } from '@/config/Pages'
import * as Yup from 'yup'
import { useNavigate } from 'react-router-dom'
import Button from '@/components/Button'
import TextEditor from '@/components/TextEditor'
import PageLoading from '@/components/PageLoading'
import Error from '@/components/Error'
import { TickCircle } from 'iconsax-react'

const ShipmentPage: FC = () => {
    const navigate = useNavigate()
    const { data: siteSetting, isLoading: siteSettingLoading, error: siteSettingError } = useGetSiteSetting(SiteSettingPageEnum.shipmentContent)
    const updateSiteSettingMutation = useUpdateSiteSetting()
    const [formData, setFormData] = useState<UpdateSiteSetting>({ shipmentContent: '' })

    const formik = useFormik<UpdateSiteSetting>({
        initialValues: formData,
        validationSchema: Yup.object({
            shipmentContent: Yup.string().required('روش ارسال الزامی است'),
        }),
        onSubmit: (values) => {
            updateSiteSettingMutation.mutate(values, {
                onSuccess: () => {
                    toast.success('روش ارسال با موفقیت ویرایش شد')
                    navigate(Pages.pages.shipment)
                }
            })
        },
        enableReinitialize: true
    })

    useEffect(() => {
        if (siteSetting?.results?.siteSetting?.shipmentContent) {
            const newData = { shipmentContent: siteSetting.results.siteSetting.shipmentContent }
            setFormData(newData)
        }
    }, [siteSetting])

    if (siteSettingLoading) {
        return (
            <div className="flex justify-center items-center min-h-[400px]">
                <PageLoading />
            </div>
        )
    }

    if (siteSettingError) {
        return (
            <div className="flex justify-center items-center min-h-[400px]">
                <Error errorText="خطا در بارگذاری اطلاعات روش ارسال" />
            </div>
        )
    }

    return (
        <div className='mt-4'>
            <div className='flex justify-between items-center'>
                <div className='flex items-center gap-3'>
                    <h1>ویرایش روش ارسال</h1>
                </div>
                <div>
                    <Button
                        className='px-6'
                        onClick={() => formik.handleSubmit()}
                        isLoading={updateSiteSettingMutation.isPending}
                        disabled={!formik.isValid || updateSiteSettingMutation.isPending}
                    >
                        <div className='flex gap-2 items-center'>
                            <TickCircle size={16} color='#fff' />
                            <div>ذخیره تغییرات</div>
                        </div>
                    </Button>
                </div>
            </div>

            <div className='flex gap-6 xl:mt-8 mt-6'>
                <div className='flex-1'>
                    <div className='bg-white rounded-lg shadow-sm border p-6'>
                        <div className='mb-4'>
                            <div className='mb-4'>
                                متن روش ارسال
                            </div>
                            <TextEditor
                                value={formik.values.shipmentContent || ''}
                                onChange={(value) => formik.setFieldValue('shipmentContent', value)}
                                placeholder='متن روش ارسال را وارد کنید...'
                            />
                            {formik.touched.shipmentContent && formik.errors.shipmentContent && (
                                <div className='text-red-500 text-sm mt-12'>
                                    {formik.errors.shipmentContent}
                                </div>
                            )}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    )
}

export default ShipmentPage