import { type FC, useState } from 'react'
import { InfoCircle } from 'iconsax-react'
import Table from '@/components/Table'
import Filters from '@/components/Filters'
import { useGetReports } from './hooks/useReportData'
import { useReportFilters } from './hooks/useReportFilters'
import { getReportTableColumns } from './components/ReportTableColumns'
import { useReportFiltersFields } from './components/ReportFiltersFields'
import type { Report } from './types/Types'
import type { RowDataType } from '@/components/types/TableTypes'
import DefaulModal from '@/components/DefaulModal'

const ReportsList: FC = () => {
    const [selectedContent, setSelectedContent] = useState<string | null>(null)
    const [isModalOpen, setIsModalOpen] = useState(false)

    const {
        filters,
        currentPage,
        apiParams,
        handleFiltersChange,
        handlePageChange,
        limit,
    } = useReportFilters()

    const { data: reportsData, isLoading } = useGetReports(apiParams)

    const reports = reportsData?.data || []
    const filterFields = useReportFiltersFields()

    const handleViewContent = (content: string) => {
        setSelectedContent(content)
        setIsModalOpen(true)
    }

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

    const columns = getReportTableColumns({ onViewContent: handleViewContent })

    const totalPages = Math.ceil(reports.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>

            <div className="mt-6 bg-blue-50 border border-blue-200 rounded-lg p-4 flex items-start gap-3">
                <div className="flex-shrink-0 mt-0.5">
                    <InfoCircle color='#0047FF' className="text-blue-600" size={20} variant="Bold" />
                </div>
                <p className="text-sm text-gray-700 leading-relaxed flex-1">
                    در این بخش گزارش مشکلاتی که کاربران ارسال کرده‌اند را می‌توانید مشاهده کنید.
                </p>
            </div>

            <Table<Report & RowDataType>
                columns={columns}
                data={reports as (Report & RowDataType)[]}
                isloading={isLoading}
                pagination={{
                    currentPage,
                    totalPages,
                    onPageChange: handlePageChange,
                }}
            />

            <DefaulModal
                open={isModalOpen}
                close={handleCloseModal}
                isHeader={true}
                title_header="متن کامل گزارش"
            >
                <div className="mt-4">
                    <p className="text-sm text-foreground whitespace-pre-wrap break-words">
                        {selectedContent || '-'}
                    </p>
                </div>
            </DefaulModal>
        </div>
    )
}

export default ReportsList