import { type FC, useState } from 'react'
import PageTitle from '../../components/PageTitle'
import Button from '@/components/Button';
import CreateReportCategoryModal from './components/CreateReportCategoryModal.tsx';
import { useGetReportCategories } from './hooks/useReportData.ts';
import { type Reason } from './types/Types';
import PageLoading from '../../components/PageLoading';
import Error from '../../components/Error';
import Td from '../../components/Td';
import { useQueryClient } from '@tanstack/react-query';

const CategoryReport: FC = () => {

    const queryClient = useQueryClient();
    const { data: reportCategoriesResponse, isLoading, error } = useGetReportCategories();
    const [isModalOpen, setIsModalOpen] = useState(false);

    const handleCloseModal = () => {
        setIsModalOpen(false);
        queryClient.invalidateQueries({ queryKey: ['reportCategories'] });
    };

    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 reportCategories = Array.isArray(reportCategoriesResponse?.results?.questions)
        ? reportCategoriesResponse.results.questions
        : [];

    return (
        <div>
            <PageTitle />
            <div className='flex justify-end mt-5'>
                <Button
                    label='افزودن دسته‌بندی'
                    onClick={() => setIsModalOpen(true)}
                    className='w-fit'
                />
            </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={'نوع'} />
                        </tr>
                    </thead>
                    <tbody>
                        {reportCategories.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={3} className="text-center py-8 text-gray-500">
                                    هیچ دسته‌بندی یافت نشد
                                </td>
                            </tr>
                        ) : (
                            reportCategories.map((category: Reason) => (
                                <tr key={category._id} className='tr'>
                                    <Td text={category.title} />
                                    <Td text={new Date(category.createdAt).toLocaleDateString('fa-IR')} />
                                    <Td text={category.type} />
                                </tr>
                            ))
                        )}
                    </tbody>
                </table>
            </div>

            <CreateReportCategoryModal
                isOpen={isModalOpen}
                onClose={handleCloseModal}
            />
        </div>
    )
}

export default CategoryReport