import { type FC } from 'react'
import Table from '@/components/Table'
import Filters from '@/components/Filters'
import { useGetReviews } from './hooks/useReviewData'
import { useReviewFilters } from './hooks/useReviewFilters'
import { getReviewTableColumns } from './components/ReviewTableColumns'
import { useReviewFiltersFields } from './components/ReviewFiltersFields'
import type { Review } from './types/Types'
import type { RowDataType } from '@/components/types/TableTypes'

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

    const { data: reviewsData, isLoading } = useGetReviews(apiParams)

    const reviews = reviewsData?.data || []
    const columns = getReviewTableColumns()
    const filterFields = useReviewFiltersFields()

    const totalPages = Math.ceil(reviews.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<Review & RowDataType>
                columns={columns}
                data={reviews as (Review & RowDataType)[]}
                isloading={isLoading}
                pagination={{
                    currentPage,
                    totalPages,
                    onPageChange: handlePageChange,
                }}
            />
        </div>
    )
}

export default ReviewsList