import { type FC } from 'react'
import { useNavigate } from 'react-router-dom'
import { useGetLearning } from './hooks/useSellerData'
import type { Learning as LearningType } from './types/Types'
import PageLoading from '../../components/PageLoading'
import Error from '../../components/Error'
import PageTitle from '../../components/PageTitle'
import Td from '../../components/Td'
import Button from '@/components/Button'
import LearningTableRow from './components/LearningTableRow'
import { Pages } from '@/config/Pages'

const Learning: FC = () => {
    const navigate = useNavigate()

    const { data: learningData, isLoading, error } = useGetLearning()

    const handleEditLearning = (learning: LearningType) => {
        navigate(`${Pages.sellers.learningUpdate}${learning._id}`)
    }

    const handleCreateLearning = () => {
        navigate(Pages.sellers.learningCreate)
    }

    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 learnings = learningData?.results?.learning || []

    return (
        <div>
            <PageTitle />
            <div className='flex justify-end mt-5'>
                <Button
                    label="ایجاد یادگیری جدید"
                    onClick={handleCreateLearning}
                    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={'وضعیت'} />
                            <Td text={'تاریخ ایجاد'} />
                            <Td text={'عملیات'} />
                        </tr>
                    </thead>
                    <tbody>
                        {learnings.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={7} className="text-center py-8 text-gray-500">
                                    هیچ یادگیری یافت نشد
                                </td>
                            </tr>
                        ) : (
                            learnings.map((learning: LearningType) => (
                                <LearningTableRow
                                    key={learning._id}
                                    learning={learning}
                                    onEdit={handleEditLearning}
                                />
                            ))
                        )}
                    </tbody>
                </table>
            </div>

        </div>
    )
}

export default Learning