import { type FC, useState } from 'react'
import PageTitle from '../../components/PageTitle'
import { useGetPayments } from './hooks/usePaymentData'
import { type Payment } 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 { Calendar, User as UserIcon, Card } from 'iconsax-react'

const PaymentList: FC = () => {
    const [currentPage, setCurrentPage] = useState(1);
    const { data: paymentsData, isLoading, error } = useGetPayments(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 payments = paymentsData?.results?.payments || [];
    const pager = paymentsData?.results?.pager;

    const getStatusVariant = (status: string) => {
        switch (status.toLowerCase()) {
            case 'success':
            case 'successful':
                return 'success';
            case 'failed':
            case 'failure':
                return 'error';
            case 'pending':
                return 'warning';
            case 'processing':
                return 'warning';
            default:
                return 'warning';
        }
    };

    const getStatusText = (status: string) => {
        switch (status.toLowerCase()) {
            case 'success':
            case 'successful':
                return 'موفق';
            case 'failed':
            case 'failure':
                return 'ناموفق';
            case 'pending':
                return 'در انتظار';
            case 'processing':
                return 'در حال پردازش';
            default:
                return status;
        }
    };

    const formatPrice = (price: number) => {
        return `${price.toLocaleString('fa-IR')} تومان`;
    };

    return (
        <div>
            <PageTitle />
            <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={'تاریخ ایجاد'} />
                        </tr>
                    </thead>
                    <tbody>
                        {payments.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={6} className="text-center py-8 text-gray-500">
                                    هیچ پرداختی یافت نشد
                                </td>
                            </tr>
                        ) : (
                            payments.map((payment: Payment) => (
                                <tr key={payment._id} className='tr'>
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <UserIcon size={16} color="#8C90A3" />
                                            <span>{payment.user?.fullName || '-'}</span>
                                        </div>
                                    </Td>
                                    <Td text={payment.payment_method?.title_fa || '-'} />
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <Card size={16} color="#8C90A3" />
                                            <span>{payment.transaction_id || '-'}</span>
                                        </div>
                                    </Td>
                                    <Td text={formatPrice(payment.totalPrice)} />
                                    <Td text="">
                                        <StatusWithText
                                            variant={getStatusVariant(payment.paymentStatus)}
                                            text={getStatusText(payment.paymentStatus)}
                                        />
                                    </Td>
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <Calendar size={16} color="#8C90A3" />
                                            <span>{payment.createdAt}</span>
                                        </div>
                                    </Td>
                                </tr>
                            ))
                        )}
                    </tbody>
                </table>
            </div>

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

export default PaymentList