import { type FC } from 'react'
import { useTranslation } from 'react-i18next'
import Table from '@/components/Table'
import Filters from '@/components/Filters'
import { useGetRestaurantShipmentMethods, useDeleteRestaurantShipmentMethod } from './hooks/useShipmentMethodData'
import { useShipmentMethodFilters } from './hooks/useShipmentMethodFilters'
import { getShipmentMethodTableColumns } from './components/ShipmentMethodTableColumns'
import { useShipmentMethodFiltersFields } from './components/ShipmentMethodFiltersFields'
import { Add } from 'iconsax-react'
import { Link } from 'react-router-dom'
import { Pages } from '@/config/Pages'
import Button from '@/components/Button'

const ShipmentMethodList: FC = () => {
    const { t } = useTranslation('global')
    const {
        filters,
        currentPage,
        apiParams,
        handleFiltersChange,
        handlePageChange,
        limit,
    } = useShipmentMethodFilters()

    const { data: shipmentMethodsData, isLoading } = useGetRestaurantShipmentMethods(apiParams)
    const { mutate: deleteShipmentMethod, isPending: isDeleting } = useDeleteRestaurantShipmentMethod()

    const shipmentMethods = shipmentMethodsData?.data || []
    const columns = getShipmentMethodTableColumns({
        onDelete: (id: string) => deleteShipmentMethod(id),
        isDeleting,
        t
    })
    const filterFields = useShipmentMethodFiltersFields()

    const totalPages = Math.ceil(shipmentMethods.length / limit) || 1

    return (
        <div className='mt-5'>
            <div className='flex justify-between items-center'>
                <h1 className='text-lg font-light'>لیست روش های ارسال</h1>
                <Link to={Pages.shipment_methods.add}>
                    <Button className='w-fit px-6'>
                        <div className='flex gap-2 items-center'>
                            <Add color='#fff' size={20} />
                            <span>افزودن روش ارسال</span>
                        </div>
                    </Button>
                </Link>
            </div>

            <div className='mt-8'>
                <Filters
                    fields={filterFields}
                    onChange={handleFiltersChange}
                    initialValues={filters}
                    searchField="search"
                />
            </div>

            <Table
                columns={columns}
                data={shipmentMethods}
                isloading={isLoading}
                pagination={{
                    currentPage,
                    totalPages,
                    onPageChange: handlePageChange,
                }}
            />
        </div>
    )
}

export default ShipmentMethodList