import type { PropsWithChildren } from 'react';
import React from 'react';
import { Link } from 'react-router-dom';
import { LEDGER_EVENTS } from 'src/apollo/type-constants/account';
import {
    getAccountingPeriodDetail,
    getContractDetail,
} from 'src/urls/frontend-royalties';
import { smartFormatter } from 'src/utils/amount-helpers';
import type { GridTableColumnDefinition } from '@theorchard/suite-components';
import type { CellProps } from '@theorchard/suite-components/dist/esm/src/components/table/base/types';
import { ABACUS_PROFILE } from 'src/constants';

const AccountLedgerTableColumns = (profileType?: string) => {
    const ACCOUNT_LEDGER_TABLE_COLUMNS: GridTableColumnDefinition<unknown>[] = [
        {
            name: 'date',
            title: 'Date',
            maxWidth: '1fr',
            minWidth: 'min-content',
            Cell: ({ data: { date } }: PropsWithChildren<CellProps<any>>) => (
                <>{date}</>
            ),
        },
        {
            name: 'transactionType',
            title: 'Event',
            maxWidth: '1fr',
            minWidth: 'min-content',
            Cell: ({
                data: { transactionType },
            }: PropsWithChildren<CellProps<any>>) => (
                <>{LEDGER_EVENTS[transactionType]}</>
            ),
        },
        {
            name: 'accountingPeriod.accountingPeriodName',
            title: 'Accounting Period',
            maxWidth: '1fr',
            minWidth: 'min-content',
            Cell: ({
                data: { accountingPeriod },
            }: PropsWithChildren<CellProps<any>>) => {
                if (!accountingPeriod) return <span>-</span>;

                const isAbacusProfile = profileType === ABACUS_PROFILE;

                return isAbacusProfile ? (
                    <Link
                        key={accountingPeriod.accountingPeriodId}
                        to={getAccountingPeriodDetail(
                            accountingPeriod.accountingPeriodId
                        )}
                        data-testid={`accountLedgerAccPeriod-${accountingPeriod.accountingPeriodId}`}
                    >
                        {accountingPeriod.accountingPeriodName}
                    </Link>
                ) : (
                    <span>{accountingPeriod.accountingPeriodName}</span>
                );
            },
        },
        {
            name: 'statementPeriod.statementPeriodName',
            title: 'Statement Period',
            maxWidth: '1fr',
            minWidth: 'min-content',
            Cell: ({
                data: { statementPeriod },
            }: PropsWithChildren<CellProps<any>>) => (
                <>{statementPeriod.statementPeriodName}</>
            ),
        },
        {
            name: 'contract.contractName',
            title: 'Contract',
            maxWidth: '300px',
            minWidth: '300px',
            Cell: ({
                data: { contract },
            }: PropsWithChildren<CellProps<any>>) => (
                <div
                    style={{
                        whiteSpace: 'pre-wrap',
                    }}
                >
                    <Link
                        key={contract?.contractId}
                        to={getContractDetail(contract?.contractId)}
                        data-testid={`accountLedgerContract-${contract?.contractId}`}
                    >
                        {contract?.contractName}
                    </Link>
                </div>
            ),
        },
        {
            name: 'amount',
            title: 'Amount',
            maxWidth: '1fr',
            minWidth: 'min-content',
            align: 'right',
            Cell: ({
                data: { amount, currencyCode },
            }: PropsWithChildren<CellProps<any>>) => (
                <>{smartFormatter(amount, currencyCode)}</>
            ),
        },
    ];

    return ACCOUNT_LEDGER_TABLE_COLUMNS;
};

export default AccountLedgerTableColumns;
