import { type FC, useState } from 'react'
import PageTitle from '../../components/PageTitle'
import { useGetAnnouncementSeller } from './hooks/useSellerData'
import { type Notification } from './types/Types'
import PageLoading from '../../components/PageLoading'
import Error from '../../components/Error'
import PaginationUi from '../../components/PaginationUi'
import Td from '../../components/Td'
import AnnouncementTableRow from './components/AnnouncementTableRow'
import Button from '@/components/Button'
import CreateAnnouncementModal from './components/CreateAnnouncementModal'

const Annoncement: FC = () => {
    const [currentPage, setCurrentPage] = useState(1)
    const [isModalOpen, setIsModalOpen] = useState(false)
    const { data: announcementsData, isLoading, error, refetch } = useGetAnnouncementSeller(currentPage)

    const handleModalClose = () => {
        setIsModalOpen(false)
    }

    const handleModalSuccess = () => {
        refetch()
        setCurrentPage(1) // Reset to first page after creating new announcement
    }

    if (isLoading) {
        return (
            <div className="flex justify-center items-center min-h-[400px]">
                <PageLoading />
            </div>
        )
    }

    if (error) {
        return (
            <div className="flex justify-center items-center min-h-[400px]">
                <Error errorText="خطا در بارگذاری اطلاعیه‌ها" />
            </div>
        )
    }

    const notifications = announcementsData?.results?.notifications || []
    const notificationPager = announcementsData?.results?.pager

    // Convert NotificationPager to PagerType expected by PaginationUi
    const pager = notificationPager ? {
        ...notificationPager,
        nextPage: notificationPager.nextPage ? `${notificationPager.page + 1}` : null,
        prevPage: notificationPager.prevPage
    } : undefined

    return (
        <div>
            <PageTitle />
            <div className='flex justify-end mt-5'>
                <Button
                    label="ایجاد اطلاعیه جدید"
                    onClick={() => setIsModalOpen(true)}
                    className="w-auto"
                />
            </div>

            <div className='relative overflow-x-auto rounded-3xl mt-5 w-full'>
                <table className='w-full text-sm'>
                    <thead className='thead'>
                        <tr>
                            <Td text={'عنوان'} />
                            <Td text={'متن اطلاعیه'} />
                            <Td text={'نوع'} />
                            <Td text={'تاریخ ایجاد'} />
                            <Td text={'وضعیت'} />
                        </tr>
                    </thead>
                    <tbody>
                        {notifications.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={5} className="text-center py-8 text-gray-500">
                                    هیچ اطلاعیه‌ای یافت نشد
                                </td>
                            </tr>
                        ) : (
                            notifications.map((notification: Notification) => (
                                <AnnouncementTableRow
                                    key={notification._id}
                                    notification={notification}
                                />
                            ))
                        )}
                    </tbody>
                </table>
            </div>

            <PaginationUi
                pager={pager}
                onPageChange={(page) => {
                    setCurrentPage(page)
                }}
            />

            <CreateAnnouncementModal
                open={isModalOpen}
                close={handleModalClose}
                onSuccess={handleModalSuccess}
            />
        </div>
    )
}

export default Annoncement