import { type FC, useState } from 'react'
import { useGetWarranties, useDeleteWarranty } from './hooks/useWarrantyData'
import { type Warranty } 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 Button from '@/components/Button'
import { useNavigate } from 'react-router-dom'
import { Pages } from '@/config/Pages'

const WarrantyList: FC = () => {

    const navigate = useNavigate()
    const [currentPage, setCurrentPage] = useState(1);
    const { data: warrantiesData, isLoading, error, refetch } = useGetWarranties(currentPage)
    const deleteWarrantyMutation = useDeleteWarranty();

    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 handleDeleteWarranty = async (warrantyId: string) => {
        await deleteWarrantyMutation.mutateAsync(warrantyId);
        refetch()
    };

    const warranties = warrantiesData?.results?.warranties || [];
    const pager = warrantiesData?.results?.pager;

    return (
        <div>
            <div className='flex justify-end mt-5'>
                <Button
                    label='افزودن گارانتی'
                    onClick={() => navigate(`${Pages.products.warranty.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={'عملیات'} />
                        </tr>
                    </thead>
                    <tbody>
                        {warranties.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={5} className="text-center py-8 text-gray-500">
                                    هیچ گارانتی یافت نشد
                                </td>
                            </tr>
                        ) : (
                            warranties.map((warranty: Warranty) => (
                                <tr key={warranty._id} className='tr'>
                                    <Td text="">
                                        <div className="w-12 h-12 rounded-lg overflow-hidden">
                                            <img
                                                src={warranty.logoUrl || '/placeholder-image.png'}
                                                alt={warranty.name}
                                                className="w-full h-full object-cover"
                                            />
                                        </div>
                                    </Td>
                                    <Td text={warranty.name} />
                                    <Td text={warranty.duration} />
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <span className={`px-2 py-1 rounded-full text-xs`}>
                                                {warranty.status}
                                            </span>
                                        </div>
                                    </Td>
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <TrashWithConfrim
                                                onDelete={() => handleDeleteWarranty(warranty._id.toString())}
                                                isLoading={deleteWarrantyMutation.isPending}
                                            />
                                        </div>
                                    </Td>
                                </tr>
                            ))
                        )}
                    </tbody>
                </table>
            </div>

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

export default WarrantyList