import React from 'react';
import type { PropsWithChildren } from 'react';
import { Link } from 'react-router-dom';
import { CONTRACT_TYPES } from 'src/constants';
import {
    getContractDetail,
    selectAccountingTab,
} from 'src/urls/frontend-royalties';
import { getFormattedAmount } from 'src/utils/accounting-run';
import type { GridTableColumnDefinition } from '@theorchard/suite-components';
import type { CellProps } from '@theorchard/suite-components/dist/esm/src/components/table/base/types';

export const RunSummaryItemListColumns = (
    accountingPeriodType: string,
    isFeatureContractMechanicalDeductionsEnabled: boolean
) => {
    const MECH_DEDUCTION_COLUMNS: GridTableColumnDefinition<unknown>[] = [
        {
            name: 'mechanicalDeductionTotal',
            title: 'Mech Deduction',
            align: 'right',
            Cell: ({
                data: { currencyCode, mechanicalDeductionTotal },
            }: PropsWithChildren<CellProps<any>>) => (
                <>
                    {getFormattedAmount(mechanicalDeductionTotal, currencyCode)}
                </>
            ),
        },
        {
            name: 'mechanicalDeductionAdminFeeTotal',
            title: 'Mech Admin Fee Deduction',
            align: 'right',
            Cell: ({
                data: { currencyCode, mechanicalDeductionAdminFeeTotal },
            }: PropsWithChildren<CellProps<any>>) => (
                <>
                    {getFormattedAmount(
                        mechanicalDeductionAdminFeeTotal,
                        currencyCode
                    )}
                </>
            ),
        },
    ];

    let RUN_SUMMARY_LIST_COLUMNS: GridTableColumnDefinition<unknown>[] = [
        {
            name: 'account',
            title: 'Account',
            Cell: ({
                data: { contract },
            }: PropsWithChildren<CellProps<any>>) => (
                <>
                    {contract && (
                        <Link
                            data-testid={`accountingTabLink-${contract!.account!.accountId}`}
                            key={contract!.account!.accountId}
                            to={selectAccountingTab(
                                contract!.account!.accountId
                            )}
                        >
                            {contract!.account!.accountName}
                        </Link>
                    )}
                </>
            ),
        },
        {
            name: 'contract.account.accountId',
            title: 'Account ID',
        },
        {
            name: 'contract',
            title: 'Contract',
            Cell: ({
                data: { contract },
            }: PropsWithChildren<CellProps<any>>) => (
                <>
                    {contract && (
                        <Link
                            data-testid={`contractDetailLink-${contract!.contractId}`}
                            key={contract!.contractId}
                            to={getContractDetail(contract!.contractId)}
                        >
                            {contract!.contractName}
                        </Link>
                    )}
                </>
            ),
        },
        {
            name: 'contract.contractId',
            title: 'Contract ID',
        },
        {
            name: 'currencyCode',
            title: 'Payment Currency',
        },
        {
            name: 'totalGrossRevenueAmount',
            title: 'Gross Revenue',
            align: 'right',
            Cell: ({
                data: { currencyCode, totalGrossRevenueAmount },
            }: PropsWithChildren<CellProps<any>>) => (
                <>{getFormattedAmount(totalGrossRevenueAmount, currencyCode)}</>
            ),
        },
        {
            name: 'distributionFee',
            title: 'Dist. Fee',
            align: 'right',
            Cell: ({
                data: { currencyCode, distributionFee },
            }: PropsWithChildren<CellProps<any>>) => (
                <>{getFormattedAmount(distributionFee, currencyCode)}</>
            ),
        },
        {
            name: 'totalNetRevenueAmount',
            title: 'Net Revenue',
            align: 'right',
            Cell: ({
                data: { currencyCode, totalNetRevenueAmount },
            }: PropsWithChildren<CellProps<any>>) => (
                <>{getFormattedAmount(totalNetRevenueAmount, currencyCode)}</>
            ),
        },
    ];

    if (
        accountingPeriodType === CONTRACT_TYPES.DISTRIBUTION &&
        isFeatureContractMechanicalDeductionsEnabled
    ) {
        RUN_SUMMARY_LIST_COLUMNS = [
            ...RUN_SUMMARY_LIST_COLUMNS,
            ...MECH_DEDUCTION_COLUMNS,
        ];
    }

    if (!isFeatureContractMechanicalDeductionsEnabled) {
        RUN_SUMMARY_LIST_COLUMNS = [
            ...RUN_SUMMARY_LIST_COLUMNS,
            ...MECH_DEDUCTION_COLUMNS,
        ];
    }

    return RUN_SUMMARY_LIST_COLUMNS;
};
