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'

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>
            </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