import { type FC, useState } from 'react'
import { useGetBrands, useDeleteBrand } from './hooks/useBrandData'
import { type BrandType } from './types/Types'
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 { Pages } from '@/config/Pages'
import Button from '@/components/Button'
import { useNavigate } from 'react-router-dom'

const BrandList: FC = () => {
    const [currentPage, setCurrentPage] = useState(1);
    const { data: brandsData, isLoading, error, refetch } = useGetBrands(currentPage)
    const deleteBrandMutation = useDeleteBrand();
    const navigate = useNavigate();
    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 handleDeleteBrand = async (brandId: string) => {
        await deleteBrandMutation.mutateAsync(brandId);
        refetch()
    };

    const brands = brandsData?.results?.brands || [];
    const pager = brandsData?.results?.pager;

    return (
        <div>
            <div className='flex justify-end mt-5'>
                <Button
                    label='افزودن برند'
                    onClick={() => navigate(`${Pages.products.brands.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={'دسته‌بندی'} />
                            <Td text={'وضعیت'} />
                            <Td text={'عملیات'} />
                        </tr>
                    </thead>
                    <tbody>
                        {brands.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={6} className="text-center py-8 text-gray-500">
                                    هیچ برندی یافت نشد
                                </td>
                            </tr>
                        ) : (
                            brands.map((brand: BrandType) => (
                                <tr key={brand._id} className='tr'>
                                    <Td text="">
                                        <div className="w-12 h-12 rounded-lg overflow-hidden">
                                            <img
                                                src={brand.logoUrl || '/placeholder-image.png'}
                                                alt={brand.title_fa}
                                                className="w-full h-full object-cover"
                                            />
                                        </div>
                                    </Td>
                                    <Td text={brand.title_fa} />
                                    <Td text={brand.title_en} />
                                    <Td text={brand.category?.name || 'بدون دسته‌بندی'} />
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <span className={`px-2 py-1 rounded-full text-xs`}>
                                                {brand.status}
                                            </span>
                                        </div>
                                    </Td>
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <Link to={`${Pages.products.brands.update}${brand._id}`}>
                                                <Edit color='#8C90A3' size={16} className="cursor-pointer hover:text-blue-500" />
                                            </Link>

                                            <TrashWithConfrim
                                                onDelete={() => handleDeleteBrand(brand._id)}
                                                isLoading={deleteBrandMutation.isPending}
                                            />
                                        </div>
                                    </Td>
                                </tr>
                            ))
                        )}
                    </tbody>
                </table>
            </div>

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

export default BrandList