import { type FC, useState } from 'react'
import PageTitle from '../../components/PageTitle'
import { useGetLearningCategory } from './hooks/useSellerData'
import { type LearningCategory } from './types/Types'
import PageLoading from '../../components/PageLoading'
import Error from '../../components/Error'
import Td from '../../components/Td'
import Button from '@/components/Button'
import CategoryLearningTableRow from './components/CategoryLearningTableRow'
import CreateUpdateCategoryModal from './components/CreateUpdateCategoryModal'

const CategoryLearning: FC = () => {
    const [isModalOpen, setIsModalOpen] = useState(false)
    const [selectedCategory, setSelectedCategory] = useState<LearningCategory | undefined>()

    const { data: categoriesData, isLoading, error, refetch } = useGetLearningCategory()

    const handleModalClose = () => {
        setIsModalOpen(false)
        setSelectedCategory(undefined)
    }

    const handleModalSuccess = () => {
        refetch()
    }

    const handleEditCategory = (category: LearningCategory) => {
        setSelectedCategory(category)
        setIsModalOpen(true)
    }

    const handleCreateCategory = () => {
        setSelectedCategory(undefined)
        setIsModalOpen(true)
    }

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

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

    const categories = categoriesData?.results?.learningCategory || []

    return (
        <div>
            <PageTitle />
            <div className='flex justify-end mt-5'>
                <Button
                    label="ایجاد دسته‌بندی جدید"
                    onClick={handleCreateCategory}
                    className="w-auto"
                />
            </div>

            <div className='relative overflow-x-auto rounded-3xl mt-5 w-full'>
                <table className='w-full text-sm'>
                    <thead className='thead'>
                        <tr>
                            <Td text={'عنوان'} />
                            <Td text={'تاریخ ایجاد'} />
                            <Td text={'تاریخ بروزرسانی'} />
                            <Td text={'وضعیت'} />
                            <Td text={'عملیات'} />
                        </tr>
                    </thead>
                    <tbody>
                        {categories.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={5} className="text-center py-8 text-gray-500">
                                    هیچ دسته‌بندی یادگیری یافت نشد
                                </td>
                            </tr>
                        ) : (
                            categories.map((category: LearningCategory) => (
                                <CategoryLearningTableRow
                                    key={category._id}
                                    category={category}
                                    onEdit={handleEditCategory}
                                />
                            ))
                        )}
                    </tbody>
                </table>
            </div>

            <CreateUpdateCategoryModal
                open={isModalOpen}
                close={handleModalClose}
                onSuccess={handleModalSuccess}
                category={selectedCategory}
            />
        </div>
    )
}

export default CategoryLearning