import type { PropsWithChildren } from 'react';
import React from 'react';
import { TruncatedText } from '@theorchard/suite-components';
import { USER_FEATURES } from 'src/constants';
import { isFeatureFlagEnabled } from 'src/utils/feature-flag';
import { formatNumberWithCommas } from 'src/utils/number-helper';
import type { GridTableColumnDefinition } from '@theorchard/suite-components';
import type { CellProps } from '@theorchard/suite-components/dist/esm/src/components/table/base/types';
import { MAX_TABLE_CELL_CHAR_LIMIT } from 'src/constants';

const renderTruncatedText = (
    text: string | null,
    testIdIdentifier?: string | undefined
) => {
    if (text && text.length > MAX_TABLE_CELL_CHAR_LIMIT) {
        const testId = testIdIdentifier
            ? `${testIdIdentifier}TruncatedText`
            : 'truncatedText';
        return <TruncatedText testId={testId} text={text} maxWidth="130ch" />;
    }
    return <>{text}</>;
};

const AdjustmentsBatchListTableColumns = () => {
    if (
        isFeatureFlagEnabled(
            USER_FEATURES.ABACUS_ADJUSTMENTS_BATCH_PAGE_NEW_COLUMNS
        )
    ) {
        const ADJUSTMENTS_BATCH_LIST_COLUMNS: GridTableColumnDefinition<unknown>[] =
            [
                {
                    name: 'accountName',
                    title: 'Account Name',
                    maxWidth: '250px',
                    minWidth: '250px',
                    Cell: ({
                        data: { account },
                    }: PropsWithChildren<CellProps<any>>) => (
                        <div
                            style={{
                                whiteSpace: 'pre-wrap',
                            }}
                        >
                            {account.accountName}
                        </div>
                    ),
                },
                {
                    name: 'accountId',
                    title: 'Account ID',
                    maxWidth: '1fr',
                    minWidth: 'min-content',
                    Cell: ({
                        data: { account },
                    }: PropsWithChildren<CellProps<any>>) => (
                        <>{account.accountId}</>
                    ),
                },
                {
                    name: 'contractName',
                    title: 'Contract Name',
                    maxWidth: '250px',
                    minWidth: '250px',
                    Cell: ({
                        data: { contract },
                    }: PropsWithChildren<CellProps<any>>) => (
                        <div
                            style={{
                                whiteSpace: 'pre-wrap',
                            }}
                        >
                            {renderTruncatedText(
                                contract.contractName,
                                'contractName'
                            )}
                        </div>
                    ),
                },
                {
                    name: 'contractId',
                    title: 'Contract ID',
                    maxWidth: '1fr',
                    minWidth: 'min-content',
                    Cell: ({
                        data: { contract },
                    }: PropsWithChildren<CellProps<any>>) => (
                        <>{contract.contractId}</>
                    ),
                },
                {
                    name: 'upc',
                    title: 'UPC',
                    maxWidth: '1fr',
                    minWidth: 'min-content',
                    Cell: ({
                        data: { upc },
                    }: PropsWithChildren<CellProps<any>>) => {
                        if (upc) return <>{upc}</>;
                        else return <>{'-'}</>;
                    },
                },
                {
                    name: 'adjustmentAmount',
                    title: 'Amount',
                    maxWidth: '1fr',
                    minWidth: 'min-content',
                    align: 'right',
                    Cell: ({
                        data: { adjustmentAmount },
                    }: PropsWithChildren<CellProps<any>>) => (
                        <>{formatNumberWithCommas(adjustmentAmount, true)}</>
                    ),
                },
                {
                    name: 'adjustmentCurrencyCode',
                    title: 'Currency',
                    maxWidth: '1fr',
                    minWidth: 'min-content',
                    Cell: ({
                        data: { adjustmentCurrencyCode },
                    }: PropsWithChildren<CellProps<any>>) => (
                        <>{adjustmentCurrencyCode}</>
                    ),
                },
                {
                    name: 'adjustmentType',
                    title: 'Adjustment Type',
                    maxWidth: '1fr',
                    minWidth: 'min-content',
                    Cell: ({
                        data: { referenceAdjustmentType },
                    }: PropsWithChildren<CellProps<any>>) => (
                        <>{referenceAdjustmentType.typeName}</>
                    ),
                },
                {
                    name: 'comment',
                    title: 'Comment',
                    maxWidth: '250px',
                    minWidth: '250px',
                    Cell: ({
                        data: { note },
                    }: PropsWithChildren<CellProps<any>>) => (
                        <div
                            style={{
                                whiteSpace: 'pre-wrap',
                            }}
                        >
                            {renderTruncatedText(note, 'comment')}
                        </div>
                    ),
                },
            ];

        ADJUSTMENTS_BATCH_LIST_COLUMNS.splice(
            ADJUSTMENTS_BATCH_LIST_COLUMNS.length - 2,
            0,
            {
                name: 'applyToFlowthroughPayment',
                title: 'Apply To Flowthrough Payment',
                maxWidth: '1fr',
                minWidth: 'min-content',
                Cell: ({
                    data: { applyToFlowthroughPayment },
                }: PropsWithChildren<CellProps<any>>) => {
                    if (applyToFlowthroughPayment !== null)
                        return <>{applyToFlowthroughPayment ? 'Yes' : 'No'}</>;
                    else return <>{'-'}</>;
                },
            }
        );
        return ADJUSTMENTS_BATCH_LIST_COLUMNS;
    }

    const ADJUSTMENTS_BATCH_LIST_COLUMNS: GridTableColumnDefinition<unknown>[] =
        [
            {
                name: 'accountName',
                title: 'Account Name',
                maxWidth: '1fr',
                minWidth: 'min-content',
                Cell: ({
                    data: { account },
                }: PropsWithChildren<CellProps<any>>) => (
                    <>{account.accountName}</>
                ),
            },
            {
                name: 'accountId',
                title: 'Account ID',
                maxWidth: '1fr',
                minWidth: 'min-content',
                Cell: ({
                    data: { account },
                }: PropsWithChildren<CellProps<any>>) => (
                    <>{account.accountId}</>
                ),
            },
            {
                name: 'contractName',
                title: 'Contract Name',
                maxWidth: '1fr',
                minWidth: 'min-content',
                Cell: ({
                    data: { contract },
                }: PropsWithChildren<CellProps<any>>) => (
                    <>{contract.contractName}</>
                ),
            },
            {
                name: 'contractId',
                title: 'Contract ID',
                maxWidth: '1fr',
                minWidth: 'min-content',
                Cell: ({
                    data: { contract },
                }: PropsWithChildren<CellProps<any>>) => (
                    <>{contract.contractId}</>
                ),
            },
            {
                name: 'upc',
                title: 'UPC',
                maxWidth: '1fr',
                minWidth: 'min-content',
                Cell: ({
                    data: { upc },
                }: PropsWithChildren<CellProps<any>>) => {
                    if (upc) return <>{upc}</>;
                    else return <>{'-'}</>;
                },
            },
            {
                name: 'adjustmentAmount',
                title: 'Amount',
                maxWidth: '1fr',
                minWidth: 'min-content',
                align: 'right',
                Cell: ({
                    data: { adjustmentAmount },
                }: PropsWithChildren<CellProps<any>>) => (
                    <>{formatNumberWithCommas(adjustmentAmount, true)}</>
                ),
            },
            {
                name: 'adjustmentCurrencyCode',
                title: 'Currency',
                maxWidth: '1fr',
                minWidth: 'min-content',
                Cell: ({
                    data: { adjustmentCurrencyCode },
                }: PropsWithChildren<CellProps<any>>) => (
                    <>{adjustmentCurrencyCode}</>
                ),
            },
        ];

    ADJUSTMENTS_BATCH_LIST_COLUMNS.push({
        name: 'applyToFlowthroughPayment',
        title: 'Apply To Flowthrough Payment',
        maxWidth: '1fr',
        minWidth: 'min-content',
        Cell: ({
            data: { applyToFlowthroughPayment },
        }: PropsWithChildren<CellProps<any>>) => {
            if (applyToFlowthroughPayment !== null)
                return <>{applyToFlowthroughPayment ? 'Y' : 'N'}</>;
            else return <>{'-'}</>;
        },
    });
    return ADJUSTMENTS_BATCH_LIST_COLUMNS;
};

export default AdjustmentsBatchListTableColumns;
