import type { ColumnDef } from '@tanstack/react-table';
import { Switch } from '@/components/ui/switch';
import { MailboxRowActions } from '@/features/mailboxes/table/mailbox-row-actions';
import type { MailboxAccountListItem } from '@/features/mailboxes/types';
import type { TranslateFn } from '@/features/users/lib/permission-i18n';
import { formatDateTimeRo } from '@/lib/datetime';

type OpenEditHandler = (mailbox: MailboxAccountListItem) => void;
type ToggleActiveHandler = (mailbox: MailboxAccountListItem, nextActive: boolean) => void;
type ActionHandler = (mailbox: MailboxAccountListItem) => void;

export function getMailboxesColumns(
    onOpenEdit: OpenEditHandler,
    onToggleActive: ToggleActiveHandler,
    onTestConnection: ActionHandler,
    onSync: ActionHandler,
    t: TranslateFn,
): ColumnDef<MailboxAccountListItem>[] {
    const emptyCell = t('settings.mailboxes.table.empty_cell');

    return [
        {
            id: 'name',
            header: t('settings.mailboxes.table.column_name'),
            cell: ({ row }) => (
                <div className="space-y-1">
                    <button
                        type="button"
                        className="cursor-pointer text-left text-sm font-semibold text-primary hover:opacity-80 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
                        onClick={() => onOpenEdit(row.original)}
                    >
                        {row.original.name}
                    </button>
                    <div className="text-xs text-muted-foreground">
                        {row.original.server_name ?? emptyCell}
                    </div>
                </div>
            ),
        },
        {
            accessorKey: 'email',
            header: t('settings.mailboxes.table.column_email'),
            cell: ({ row }) => <span className="text-sm">{row.original.email}</span>,
        },
        {
            id: 'server',
            header: t('settings.mailboxes.table.column_server'),
            cell: ({ row }) => (
                <span className="font-mono text-xs">
                    {row.original.server_host && row.original.server_port
                        ? `${row.original.server_host}:${row.original.server_port}`
                        : emptyCell}
                </span>
            ),
        },
        {
            accessorKey: 'client_name',
            header: t('settings.mailboxes.table.column_client'),
            cell: ({ row }) => <span className="text-sm">{row.original.client_name ?? emptyCell}</span>,
        },
        {
            id: 'active',
            header: t('settings.mailboxes.table.column_active'),
            cell: ({ row }) => (
                <Switch checked={row.original.active} onCheckedChange={(checked) => onToggleActive(row.original, checked)} />
            ),
        },
        {
            accessorKey: 'last_synced_at',
            header: t('settings.mailboxes.table.column_last_sync'),
            cell: ({ row }) => (
                <span className="text-sm">
                    {row.original.last_synced_at ? formatDateTimeRo(row.original.last_synced_at) : emptyCell}
                </span>
            ),
        },
        {
            id: 'actions',
            header: () => (
                <span className="block text-end text-[11px] font-semibold leading-tight text-foreground/85">
                    {t('settings.mailboxes.table.column_actions')}
                </span>
            ),
            cell: ({ row }) => (
                <MailboxRowActions mailbox={row.original} onTestConnection={onTestConnection} onSync={onSync} />
            ),
        },
    ];
}
