import { type FC } from 'react'
import Table from '@/components/Table'
import Filters from '@/components/Filters'
import { useGetAdmins, useDeleteAdmin } from './hooks/useAdminData'
import { useAdminFilters } from './hooks/useAdminFilters'
import { getAdminTableColumns } from './components/AdminTableColumns'
import { useAdminFiltersFields } from './components/AdminFiltersFields'
import { Add } from 'iconsax-react'
import { Link } from 'react-router-dom'
import { Pages } from '@/config/Pages'
import Button from '@/components/Button'

const AdminList: FC = () => {
    const {
        filters,
        currentPage,
        apiParams,
        handleFiltersChange,
        handlePageChange,
        limit,
    } = useAdminFilters()

    const { data: adminsData, isLoading } = useGetAdmins(apiParams)
    const { mutate: deleteAdmin, isPending: isDeleting } = useDeleteAdmin()

    const admins = adminsData?.data || []
    const columns = getAdminTableColumns({
        onDelete: (id: string) => deleteAdmin(id),
        isDeleting
    })
    const filterFields = useAdminFiltersFields()

    const totalPages = Math.ceil(admins.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.admins.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={admins}
                isloading={isLoading}
                pagination={{
                    currentPage,
                    totalPages,
                    onPageChange: handlePageChange,
                }}
            />
        </div>
    )
}

export default AdminList