import { type FC, useState } from 'react'
import PageLoading from '../../components/PageLoading'
import Error from '../../components/Error'
import PaginationUi from '../../components/PaginationUi'
import Td from '../../components/Td'
import TrashWithConfrim from '../../components/TrashWithConfrim'
import { Edit } from 'iconsax-react'
import { Link } from 'react-router-dom'
import Button from '@/components/Button'
import { useNavigate } from 'react-router-dom'
import { type ReturnCategoryType, type PagerType } from './types/Types'

const ReturnCategories: FC = () => {
    const [currentPage, setCurrentPage] = useState(1);
    // TODO: Add category data fetching hook
    const isLoading = false;
    const error = null;
    const refetch = () => { };
    // TODO: Add delete mutation
    const deleteCategoryMutation = { isPending: false, mutateAsync: async (_id: string) => { } };

    const navigate = useNavigate();

    // Mock data for now
    const categories: ReturnCategoryType[] = [];
    const pager: PagerType = { page: currentPage, limit: 10, totalPages: 1, totalItems: 0, prevPage: null, nextPage: null };

    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 handleDeleteCategory = async (categoryId: string) => {
        await deleteCategoryMutation.mutateAsync(categoryId);
        refetch()
    };

    return (
        <div>
            <div className='flex justify-end mt-5'>
                <Button
                    label='افزودن دسته‌بندی مرجوعی'
                    onClick={() => navigate('/orders/return-categories/create')}
                    className='w-fit'
                />
            </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={'عملیات'} />
                        </tr>
                    </thead>
                    <tbody>
                        {categories.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={4} className="text-center py-8 text-gray-500">
                                    هیچ دسته‌بندی مرجوعی یافت نشد
                                </td>
                            </tr>
                        ) : (
                            categories.map((category: any) => (
                                <tr key={category.id} className='tr'>
                                    <Td text={category.title} />
                                    <Td text={category.description} />
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <span className={`px-2 py-1 rounded-full text-xs`}>
                                                {category.status}
                                            </span>
                                        </div>
                                    </Td>
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <Link to={`/orders/return-categories/${category.id}`}>
                                                <Edit color='#8C90A3' size={16} className="cursor-pointer hover:text-blue-500" />
                                            </Link>

                                            <TrashWithConfrim
                                                onDelete={() => handleDeleteCategory(category.id)}
                                                isLoading={deleteCategoryMutation.isPending}
                                            />
                                        </div>
                                    </Td>
                                </tr>
                            ))
                        )}
                    </tbody>
                </table>
            </div>

            <PaginationUi
                pager={pager}
                onPageChange={(page) => {
                    setCurrentPage(page);
                }}
            />
        </div>
    )
}

export default ReturnCategories
