import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { ClicToPayTransaction } from '@/types';
import { Link } from '@inertiajs/react';
import { ColumnDef } from '@tanstack/react-table';
import { Copy } from 'lucide-react';
import { toast } from 'sonner';

function formatDate(value: string | null): string {
    if (!value) return '—';
    const d = new Date(value);
    return Number.isNaN(d.getTime()) ? '—' : d.toLocaleString();
}

type BadgeVariant = 'default' | 'secondary' | 'destructive' | 'outline';

function statusBadgeProps(status: string): { variant: BadgeVariant; className: string } {
    switch (status) {
        case 'paid':
            return { variant: 'default', className: 'border-transparent bg-green-600 text-white' };
        case 'declined':
        case 'failed':
        case 'init_failed':
            return { variant: 'destructive', className: '' };
        case 'refunded':
            return { variant: 'default', className: 'border-transparent bg-blue-600 text-white' };
        case 'expired':
        case 'initiated':
        case 'new':
        default:
            return { variant: 'secondary', className: 'border-transparent bg-amber-100 text-amber-800' };
    }
}

export const getColumns = (t: (namespace: string, key: string) => string): ColumnDef<ClicToPayTransaction>[] => [
    {
        accessorKey: 'created_at',
        header: t('clictopay', 'col_created_at'),
        cell: ({ row }) => <span className="whitespace-nowrap text-sm">{formatDate(row.original.created_at)}</span>,
    },
    {
        accessorKey: 'order_number',
        header: t('clictopay', 'col_order_number'),
        cell: ({ row }) => <span className="font-mono text-xs">{row.original.order_number}</span>,
    },
    {
        accessorKey: 'source',
        header: t('clictopay', 'col_source'),
        cell: ({ row }) => (
            <Badge variant="outline" className="text-xs whitespace-nowrap">
                {t('clictopay', `source_${row.original.source}`)}
            </Badge>
        ),
    },
    {
        accessorKey: 'amount',
        header: t('clictopay', 'col_amount'),
        cell: ({ row }) => (
            <span className="font-medium whitespace-nowrap">{row.original.amount.toFixed(3)} TND</span>
        ),
    },
    {
        accessorKey: 'status',
        header: t('clictopay', 'col_status'),
        cell: ({ row }) => {
            const { variant, className } = statusBadgeProps(row.original.status);
            return (
                <Badge variant={variant} className={className}>
                    {t('clictopay', `status_${row.original.status}`)}
                </Badge>
            );
        },
    },
    {
        accessorKey: 'masked_pan',
        header: t('clictopay', 'col_card'),
        cell: ({ row }) => {
            const { masked_pan, cardholder_name } = row.original;
            if (!masked_pan && !cardholder_name) return <span className="text-gray-400">—</span>;
            return (
                <span className="text-sm">
                    <span className="font-mono">{masked_pan ?? '—'}</span>
                    {cardholder_name && <span className="ml-1 text-xs text-gray-400">({cardholder_name})</span>}
                </span>
            );
        },
    },
    {
        accessorKey: 'created_by',
        header: t('clictopay', 'col_created_by'),
        cell: ({ row }) => row.original.created_by ?? '—',
    },
    {
        id: 'actions',
        header: t('clictopay', 'col_actions'),
        cell: ({ row }) => {
            const { id, form_url } = row.original;
            return (
                <div className="flex items-center justify-end gap-1">
                    <Button asChild size="sm" variant="outline">
                        <Link href={route('clictopay.show', id)}>{t('clictopay', 'view')}</Link>
                    </Button>
                    {form_url && (
                        <Button
                            size="sm"
                            variant="ghost"
                            title={t('clictopay', 'copy_link')}
                            onClick={() => {
                                navigator.clipboard.writeText(form_url);
                                toast.success(t('clictopay', 'copied'));
                            }}
                        >
                            <Copy className="h-4 w-4" />
                        </Button>
                    )}
                </div>
            );
        },
    },
];
