import { type FC, useState } from 'react'
import PageTitle from '../../components/PageTitle'
import { useGetRequestCreateProduct } from './hooks/useSellerData'
import { type ProductRequest } from './types/Types'
import PageLoading from '../../components/PageLoading'
import Error from '../../components/Error'
import PaginationUi from '../../components/PaginationUi'
import Td from '../../components/Td'
import ProductRequestTableRow from './components/ProductRequestTableRow'
import ProductRequestDetailModal from './components/ProductRequestDetailModal'

const RequestCreateProduct: FC = () => {
    const [currentPage, setCurrentPage] = useState(1);
    const [selectedRequest, setSelectedRequest] = useState<ProductRequest | null>(null);
    const [isDetailModalOpen, setIsDetailModalOpen] = useState(false);
    const { data, isLoading, error } = useGetRequestCreateProduct(currentPage);

    const handleRowClick = (request: ProductRequest) => {
        setSelectedRequest(request);
        setIsDetailModalOpen(true);
    };

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

    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={'عملیات'} />
                        </tr>
                    </thead>
                    <tbody>
                        {productRequests.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={9} className="text-center py-8 text-gray-500">
                                    هیچ درخواست ایجاد محصولی یافت نشد
                                </td>
                            </tr>
                        ) : (
                            productRequests.map((request: ProductRequest) => (
                                <ProductRequestTableRow
                                    key={request._id}
                                    request={request}
                                    onClick={() => handleRowClick(request)}
                                />
                            ))
                        )}
                    </tbody>
                </table>
            </div>

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

            <ProductRequestDetailModal
                open={isDetailModalOpen}
                close={() => setIsDetailModalOpen(false)}
                request={selectedRequest}
            />
        </div>
    )
}

export default RequestCreateProduct