import { mount } from '@vue/test-utils';
import { KviewCrudTable } from '@kview/ui-data/vue';
import type {
    KviewCrudTableColumn,
    KviewCrudTablePagination,
    KviewCrudTableRow,
} from '@kview/ui-data/vue';

/**
 * Shared fixtures for the `KviewCrudTable` spec files (split by
 * responsibility: core rendering, columns, filters, row interactions,
 * export/i18n — mirroring the engine's composables). Not a `*.spec.ts` file,
 * so Vitest's `tests/frontend/**\/*.spec.ts` include pattern never picks it
 * up as a test suite on its own.
 */
export const column = (overrides: Partial<KviewCrudTableColumn> & { key: string }): KviewCrudTableColumn => ({
    label: overrides.key,
    type: 'text',
    alignment: 'start',
    hidden: false,
    sortable: true,
    filterable: true,
    resizable: true,
    hideable: true,
    numeric: false,
    nowrap: false,
    ...overrides,
});

export const columns: KviewCrudTableColumn[] = [
    column({ key: 'name', label: 'GP', type: 'link' }),
    column({ key: 'funds_count', label: 'Funds', type: 'number', alignment: 'end', numeric: true, nowrap: true }),
    column({ key: 'internal', label: 'Internal', hidden: true }),
];

export const rows: KviewCrudTableRow[] = [
    {
        id: 1,
        href: '/gps/1',
        cells: {
            name: { type: 'link', value: 'Alpha Capital', label: 'Alpha Capital', url: '/gps/1' },
            funds_count: { type: 'number', value: 3, formatted: '3' },
            internal: { value: 'hidden-value' },
        },
    },
    {
        id: 2,
        href: '/gps/2',
        cells: {
            name: { type: 'link', value: 'Beta Partners', label: 'Beta Partners', url: '/gps/2' },
            funds_count: { type: 'number', value: 1, formatted: '1' },
            internal: { value: 'hidden-too' },
        },
    },
];

export const pagination = (overrides: Partial<KviewCrudTablePagination> = {}): KviewCrudTablePagination => ({
    page: 1,
    perPage: 25,
    lastPage: 3,
    total: 51,
    from: 1,
    to: 25,
    links: [
        { url: null, label: '&laquo; Previous', active: false },
        { url: '/specimen?page=1', label: '1', active: true },
        { url: '/specimen?page=2', label: '2', active: false },
        { url: '/specimen?page=3', label: '3', active: false },
        { url: '/specimen?page=2', label: 'Next &raquo;', active: false },
    ],
    options: [
        { value: '25', label: '25' },
        { value: '50', label: '50' },
    ],
    ...overrides,
});

/** Lot 1 read-only view: selection is exercised separately by the row-interaction spec. */
export const mountTable = (props: Record<string, unknown> = {}) => mount(KviewCrudTable, {
    props: {
        id: 'specimen-table',
        title: 'General Partners',
        columns,
        rows,
        sort: { key: null, direction: null },
        search: '',
        pagination: pagination(),
        selectable: false,
        ...props,
    },
});

/** Full-featured mount (selection, filters, bulk delete) used by the other spec files. */
export const mountFull = (extra: Record<string, unknown> = {}) => mount(KviewCrudTable, {
    attachTo: document.body,
    props: {
        id: 'lot2-table',
        title: 'General Partners',
        columns,
        rows,
        sort: { key: null, direction: null },
        search: '',
        filters: {},
        pagination: pagination(),
        bulkDeleteUrl: '/gps/bulk',
        ...extra,
    },
});
