import { type FC, useState } from 'react'
import { useGetResumes } from './hooks/useJobsData'
import { type Resume } from './types/Types'
import PageLoading from '@/components/PageLoading'
import Error from '@/components/Error'
import PaginationUi from '@/components/PaginationUi'
import Td from '@/components/Td'
import StatusWithText from '@/components/StatusWithText'
import { ArrowLeft2 } from 'iconsax-react'

const Resumes: FC = () => {
    const [currentPage, setCurrentPage] = useState(1);
    const { data, isLoading, error } = useGetResumes(currentPage);

    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 resumes = data?.results?.resumes || [];
    const pager = data?.results?.pager;

    const getStatusVariant = (status: string) => {
        switch (status) {
            case 'Accepted': return 'success';
            case 'Rejected': return 'error';
            case 'Pending': return 'warning';
            default: return 'warning';
        }
    };

    const getStatusText = (status: string) => {
        switch (status) {
            case 'Accepted': return 'تایید شده';
            case 'Rejected': return 'رد شده';
            case 'Pending': return 'در انتظار بررسی';
            default: return status;
        }
    };

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

    const handleDownloadResume = (resumeUrl: string, fullName: string) => {
        const link = document.createElement('a');
        link.href = resumeUrl;
        link.download = `${fullName}_resume.pdf`;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    };

    return (
        <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={'عنوان شغل'} />
                            <Td text={'وضعیت'} />
                            <Td text={'تاریخ ارسال'} />
                            <Td text={'عملیات'} />
                        </tr>
                    </thead>
                    <tbody>
                        {resumes.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={8} className="text-center py-8 text-gray-500">
                                    هیچ رزومه‌ای یافت نشد
                                </td>
                            </tr>
                        ) : (
                            resumes.map((resume: Resume) => (
                                <tr key={resume._id} className='tr'>
                                    <Td text={resume.fullName} />
                                    <Td text={resume.fatherName} />
                                    <Td text={resume.placeOfBirth} />
                                    <Td text={new Date(resume.birthday).toLocaleDateString('fa-IR')} />
                                    <Td text={truncateText(resume.job.title)} />
                                    <Td text="">
                                        <StatusWithText
                                            variant={getStatusVariant(resume.status)}
                                            text={getStatusText(resume.status)}
                                        />
                                    </Td>
                                    <Td text={new Date(resume.createdAt).toLocaleDateString('fa-IR')} />
                                    <Td text="">
                                        <div
                                            onClick={() => handleDownloadResume(resume.resumeUrl, resume.fullName)}
                                            className="flex items-center gap-2 cursor-pointer">
                                            رزرومه
                                            <ArrowLeft2
                                                color='#8C90A3'
                                                size={16}
                                                className="cursor-pointer hover:text-blue-500"
                                            />
                                        </div>
                                    </Td>
                                </tr>
                            ))
                        )}
                    </tbody>
                </table>
            </div>

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

export default Resumes