import { type FC } from 'react'
import PageTitle from '../../components/PageTitle'
import { useDeleteAboutUs, useGetAboutUs } from './hooks/useSettingData'
import { type AboutUsItem } from './types/Types'
import PageLoading from '../../components/PageLoading'
import Error from '../../components/Error'
import Td from '../../components/Td'
import { Calendar, DocumentText, Add } from 'iconsax-react'
import Button from '../../components/Button'
import { useNavigate } from 'react-router-dom'
import { Pages } from '@/config/Pages'
import TrashWithConfrim from '@/components/TrashWithConfrim'
import { useQueryClient } from '@tanstack/react-query'

const AboutUsList: FC = () => {
    const navigate = useNavigate();
    const queryClient = useQueryClient();
    const deleteAboutUs = useDeleteAboutUs();
    const { data, isLoading, error } = useGetAboutUs();

    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 aboutUsItems = data?.results?.aboutUs || [];

    const formatDate = (dateString: string) => {
        if (!dateString) return '-';
        return new Date(dateString).toLocaleDateString('fa-IR');
    };

    const truncateText = (text: string, maxLength: number = 50) => {
        if (text.length <= maxLength) return text;
        return text.substring(0, maxLength) + '...';
    };

    return (
        <div>
            <PageTitle />
            <div className='flex justify-end mt-5'>
                <Button
                    onClick={() => navigate(Pages.pages.aboutUsCreate)}
                    className='w-fit'
                >
                    <div className='flex gap-2 items-center'>
                        <Add size={16} color='#fff' />
                        <span>افزودن درباره ما</span>
                    </div>
                </Button>
            </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>
                        {aboutUsItems.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={4} className="text-center py-8 text-gray-500">
                                    هیچ اطلاعاتی یافت نشد
                                </td>
                            </tr>
                        ) : (
                            aboutUsItems.map((item: AboutUsItem) => (
                                <tr key={item._id} className='tr'>
                                    <Td text="">
                                        <div className="w-16 h-16 rounded-lg overflow-hidden">
                                            <img
                                                src={item.imageUrl || '/placeholder-image.png'}
                                                alt={item.title}
                                                className="w-full h-full object-cover"
                                            />
                                        </div>
                                    </Td>
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <DocumentText size={16} color="#8C90A3" />
                                            <span className="font-medium">{item.title}</span>
                                        </div>
                                    </Td>
                                    <Td text="">
                                        <div className="max-w-xs">
                                            <span className="text-gray-600 text-sm">
                                                {truncateText(item.description)}
                                            </span>
                                        </div>
                                    </Td>
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <Calendar size={16} color="#8C90A3" />
                                            <span>{formatDate(item.createdAt)}</span>
                                        </div>
                                    </Td>
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <TrashWithConfrim
                                                isLoading={deleteAboutUs.isPending}
                                                onDelete={() => {
                                                    deleteAboutUs.mutate(item._id, {
                                                        onSuccess: () => {
                                                            queryClient.invalidateQueries({ queryKey: ['aboutUs'] })
                                                        }
                                                    })
                                                }}
                                            />
                                        </div>
                                    </Td>
                                </tr>
                            ))
                        )}
                    </tbody>
                </table>
            </div>
        </div>
    )
}

export default AboutUsList