import React, { useContext } from 'react';
import { SettingsContext } from '../../App/SettingsContext';
import { translate } from '../../../Helper';

// Status Badge Component
const StatusBadge = ({ status }) => {
    const getStatusIcon = (status) => {
        const statusLower = status.toLowerCase();
        return <div className={`ecre-inline-block ecre-w-1.5 ecre-h-1.5 ecre-rounded-full ecre-mr-1 ${statusLower === 'available' ? 'ecre-bg-emerald-600' :
                statusLower === 'used' ? 'ecre-bg-red-600' :
                    statusLower === 'expired' ? 'ecre-bg-orange-600' :
                        statusLower === 'draft' ? 'ecre-bg-gray-500' :
                            statusLower === 'pending' ? 'ecre-bg-yellow-500' :
                                statusLower === 'removed' ? 'ecre-bg-pink-600' : 'ecre-bg-gray-600'
            }`} />;
    };

    // Capitalize first letter of status
    const displayStatus = status.charAt(0).toUpperCase() + status.slice(1);

    return (
        <div className="ecre-text-xs ecre-text-[#4f4f4f] ecre-font-semibold">
            {getStatusIcon(status)}
            {displayStatus}
        </div>
    );
};

const RewardCouponsRow = ({ coupon }) => {
    const { currency } = useContext(SettingsContext);
    const couponEditUrl = `post.php?post=${coupon.coupon_id}&action=edit`;
    const orderViewUrl = `admin.php?page=wc-orders&action=edit&id=${coupon.order_id}`;

    // Helper function to format amount with currency
    const formatAmount = (amount) => {
        if (typeof amount === 'string' && amount.includes('%')) {
            return amount; // Already formatted as percentage
        }
        // Remove any existing currency symbols and format with dynamic currency
        const cleanAmount = String(amount).replace(/[$€£¥₹₽¢₩₪₦₡₨₫₱₴₵₸₺₼₾₿]/g, '');
        return `${currency}${cleanAmount}`;
    };

    return (
        <tr className="hover:ecre-bg-gray-50">
            <td className="ecre-px-5 ecre-py-4 ecre-whitespace-nowrap">
                <div className="ecre-text-[#4f4f4f] ecre-text-sm ecre-font-semibold">
                    {coupon.code}
                </div>
                <div className="ecre-text-sm ecre-text-[#757575] ecre-font-normal">
                    {translate('issued')}: {coupon.issuedDate}
                </div>
                <div className="ecre-text-sm ecre-text-[#757575] ecre-font-normal">
                    {translate('expires')}: {coupon.expiryDate}
                </div>
            </td>
            <td className="ecre-px-5 ecre-py-4 ecre-whitespace-nowrap ecre-text-sm ecre-text-gray-900">
                {formatAmount(coupon.amount)}
            </td>
            <td className="ecre-px-5 ecre-py-4 ecre-whitespace-nowrap ecre-text-sm ecre-text-gray-900">
                {coupon.usageLimit}
            </td>
            <td className="ecre-px-5 ecre-py-4 ecre-text-sm ecre-text-gray-900">
                <div className="ecre-max-w-[90px]">
                    {coupon.type}
                </div>
            </td>
            <td className="ecre-px-5 ecre-py-4 ecre-whitespace-nowrap ecre-text-sm ecre-text-gray-900">
                <StatusBadge status={coupon.status} />
            </td>
            <td className="ecre-px-5 ecre-py-4 ecre-whitespace-nowrap">
                <div className="ecre-flex ecre-flex-col ecre-gap-1">
                    <a
                        href={couponEditUrl}
                        target="_blank"
                        rel="noopener noreferrer"
                        className="ecre-text-blue-600 ecre-cursor-pointer hover:ecre-text-[#6A40D5] ecre-text-sm ecre-font-medium"
                    >
                        {translate('manage')}
                    </a>
                    {coupon.order_id && coupon.order_id != 0 && (
                        <a
                            href={orderViewUrl}
                            target="_blank"
                            rel="noopener noreferrer"
                            className="ecre-text-blue-600 ecre-cursor-pointer hover:ecre-text-[#6A40D5] ecre-text-sm ecre-font-medium">
                            {translate('view_order')}
                        </a>
                    )}
                </div>
            </td>
        </tr>
    );
};

export default RewardCouponsRow;