import type { ColumnDef } from '@tanstack/react-table';
import { useMemo } from 'react';
import { SortableColumnHeader } from '@/components/ui/sortable-column-header';
import { DocumentPrimaryCell } from '@/features/documents/components/document-primary-cell';
import { DocumentAccountingClassificationBadge } from '@/features/documents/components/document-accounting-classification-badge';
import { DocumentAccountantStatusBadge } from '@/features/documents/components/document-status-badge';
import { DocumentTypeLabel } from '@/features/documents/components/document-type-label';
import type { DocumentListItem } from '@/features/documents/types';
import { useI18n } from '@/hooks/use-i18n';

type SortHandler = (column: 'accountant_status') => void;
type OpenDetailHandler = (documentId: number) => void;
type ToggleExpandHandler = (documentId: number) => void;

function formatBytes(size: number | null): string {
    if (!size || size <= 0) {
        return '-';
    }

    if (size < 1024) {
        return `${size} B`;
    }

    if (size < 1024 * 1024) {
        return `${(size / 1024).toFixed(1)} KB`;
    }

    return `${(size / (1024 * 1024)).toFixed(1)} MB`;
}

function buildClientDocumentsColumns(
    t: (key: string, replacements?: Record<string, string | number>) => string,
    onSort: SortHandler,
    onOpenDetail: OpenDetailHandler,
    expandedDocumentId: number | null,
    onToggleExpand: ToggleExpandHandler,
): ColumnDef<DocumentListItem>[] {
    return [
        {
            accessorKey: 'id',
            header: t('documents.admin_index.table.column_id'),
            cell: ({ row }) => (
                <span className="font-mono text-xs text-muted-foreground tabular-nums">{row.original.id}</span>
            ),
        },
        {
            id: 'document',
            header: t('documents.admin_index.table.column_document'),
            cell: ({ row }) => (
                <DocumentPrimaryCell
                    document={row.original}
                    onOpenDetail={onOpenDetail}
                    isExpanded={expandedDocumentId === row.original.id}
                    onToggleExpand={onToggleExpand}
                    alwaysAllowOpenDetail
                />
            ),
        },
        {
            id: 'type',
            header: t('documents.admin_index.table.column_type'),
            cell: ({ row }) => (
                <DocumentTypeLabel
                    typeKey={row.original.document_type.key}
                    label={row.original.document_type.label}
                />
            ),
        },
        {
            accessorKey: 'size',
            header: t('documents.admin_index.table.column_size'),
            cell: ({ row }) => formatBytes(row.original.size),
        },
        {
            id: 'accounting_classification',
            header: t('documents.admin_index.table.column_accounting_classification'),
            cell: ({ row }) => (
                <DocumentAccountingClassificationBadge
                    classification={row.original.accounting_classification}
                    label={row.original.accounting_classification_label}
                />
            ),
        },
        {
            accessorKey: 'accountant_status',
            header: () => (
                <SortableColumnHeader
                    label={t('documents.admin_index.table.column_accountant_status')}
                    onSort={() => onSort('accountant_status')}
                />
            ),
            cell: ({ row }) => <DocumentAccountantStatusBadge status={row.original.accountant_status} />,
        },
    ];
}

export function useClientDocumentsColumns(
    onSort: SortHandler,
    onOpenDetail: OpenDetailHandler,
    expandedDocumentId: number | null,
    onToggleExpand: ToggleExpandHandler,
) {
    const { t } = useI18n();

    return useMemo(
        () => buildClientDocumentsColumns(t, onSort, onOpenDetail, expandedDocumentId, onToggleExpand),
        [t, onSort, onOpenDetail, expandedDocumentId, onToggleExpand],
    );
}
