import { type FC, useMemo, useState } from 'react'
import type { Pager } from './types/Types'
import Table from '@/components/Table'
import Filters from '@/components/Filters'
import { usePagerFilters } from './hooks/usePagerFilters'
import { getPagerTableColumns } from './components/PagerTableColumns'
import { usePagerFiltersFields } from './components/PagerFiltersFields'
import type { RowDataType } from '@/components/types/TableTypes'
import { useGetPagers } from './hooks/usePagerData'
import PagerDetailModal from './components/PagerDetailModal'
import { useTranslation } from 'react-i18next'
import { PagerStatus } from './enum/Enum'

const PagersList: FC = () => {
    const { t } = useTranslation('global')
    const [selectedPager, setSelectedPager] = useState<Pager | null>(null)
    const [isModalOpen, setIsModalOpen] = useState(false)

    const { data: pagersData, isLoading } = useGetPagers()

    const {
        filters,
        currentPage,
        handleFiltersChange,
        handlePageChange,
        filteredData,
        paginatedData,
        totalPages,
    } = usePagerFilters()

    const pagersList = useMemo(() => {
        return pagersData?.data || []
    }, [pagersData])

    const filteredPagers = useMemo(() => {
        return filteredData(pagersList) as Pager[]
    }, [pagersList, filteredData])

    const paginatedPagers = useMemo(() => {
        return paginatedData(filteredPagers) as Pager[]
    }, [filteredPagers, paginatedData])

    const totalPagesCount = useMemo(() => {
        return totalPages(filteredPagers.length)
    }, [filteredPagers.length, totalPages])

    const handleViewDetails = (pager: Pager) => {
        setSelectedPager(pager)
        setIsModalOpen(true)
    }

    const handleCloseModal = () => {
        setIsModalOpen(false)
        setSelectedPager(null)
    }

    const columns = getPagerTableColumns({ onViewDetails: handleViewDetails, t })
    const filterFields = usePagerFiltersFields()

    const getRowClassName = (item: Pager & RowDataType) => {
        if (item.status !== PagerStatus.Pending) {
            return 'opacity-30'
        }
        return ''
    }

    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<Pager & RowDataType>
                columns={columns}
                data={paginatedPagers as (Pager & RowDataType)[]}
                isloading={isLoading}
                rowClassName={getRowClassName}
                pagination={{
                    currentPage,
                    totalPages: totalPagesCount,
                    onPageChange: handlePageChange,
                }}
            />

            <PagerDetailModal
                open={isModalOpen}
                close={handleCloseModal}
                pager={selectedPager}
            />
        </div>
    )
}

export default PagersList