import { type FC, useState } from 'react'
import { useGetSellers } from './hooks/useSellerData'
import { type Seller } from './types/Types'
import PageLoading from '../../components/PageLoading'
import Error from '../../components/Error'
import PaginationUi from '../../components/PaginationUi'
import PageTitle from '../../components/PageTitle'
import Td from '../../components/Td'
import SellerTableRow from './components/SellerTableRow'
import SellerContractModal from './components/SellerContractModal'

const SellerList: FC = () => {

    const [currentPage, setCurrentPage] = useState(1);
    const [selectedContract, setSelectedContract] = useState<Seller['contracts'] | undefined>();
    const [isContractModalOpen, setIsContractModalOpen] = useState(false);
    const { data: sellersData, isLoading, error } = useGetSellers(currentPage);

    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 sellers = sellersData?.results?.sellers || [];
    const pager = sellersData?.results?.pager;

    const handleViewContract = (contract?: Seller['contracts']) => {
        setSelectedContract(contract);
        setIsContractModalOpen(true);
    };

    return (
        <div>
            <PageTitle />
            <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={'تاریخ ایجاد'} />
                            <Td text={'قرارداد'} />
                            <Td text={'مدارک'} />
                            <Td text={'فعال/غیرفعال'} />
                        </tr>
                    </thead>
                    <tbody>
                        {sellers.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={10} className="text-center py-8 text-gray-500">
                                    هیچ فروشنده‌ای یافت نشد
                                </td>
                            </tr>
                        ) : (
                            sellers.map((seller: Seller) => (
                                <SellerTableRow
                                    key={seller._id}
                                    seller={seller}
                                    onViewContract={handleViewContract}
                                />
                            ))
                        )}
                    </tbody>
                </table>
            </div>

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

            <SellerContractModal
                open={isContractModalOpen}
                close={() => setIsContractModalOpen(false)}
                contract={selectedContract}
            />
        </div>
    )
}

export default SellerList