import type { PropsWithChildren } from 'react';
import React from 'react';
import { amountFormatter, rateFormatter } from 'src/utils/amount-helpers';
import type { GridTableColumnDefinition } from '@theorchard/suite-components';
import PaymentDescription from 'src/components/shared/payment-description';
import { getCurrencyLabel } from 'src/utils/currency';
import { ContractAdvanceQueuedStatus } from 'src/components/contract-advance-list/contract-advance-queued-status';
import type { CellProps } from '@theorchard/suite-components/dist/esm/src/components/table/base/types';
import type { MilestoneType } from 'src/types/abacus-contract-advance';

const ContractAdvanceQueuedListTableColumns = (
    milestoneOptionsMap: MilestoneType
) => {
    const CONTRACT_ADVANCE_QUEUED_LIST_COLUMNS: GridTableColumnDefinition<unknown>[] =
        [
            {
                name: 'createdAt',
                title: 'Date Added',
                fixed: true,
                Cell: ({
                    data: { createdAt },
                }: PropsWithChildren<CellProps<any>>) => <>{createdAt}</>,
            },
            {
                name: 'advanceDescription',
                title: 'Payment Description',
                minWidth: '200px',
                Cell: ({
                    data: {
                        referencePaymentType,
                        worksheetPaymentContractAdvances,
                    },
                }: PropsWithChildren<CellProps<any>>) => {
                    const worksheetPaymentContractAdvancesItem =
                        worksheetPaymentContractAdvances.items[0];
                    return (
                        <div
                            style={{
                                whiteSpace: 'pre-wrap',
                            }}
                        >
                            <PaymentDescription
                                worksheetPaymentContractAdvanceId={
                                    worksheetPaymentContractAdvancesItem?.worksheetPaymentContractAdvanceId
                                }
                                paymentName={
                                    worksheetPaymentContractAdvancesItem?.paymentName
                                }
                                referencePaymentTypeId={
                                    referencePaymentType?.referencePaymentTypeId
                                }
                            />
                        </div>
                    );
                },
            },
            {
                name: 'amount',
                title: 'Amount',
                align: 'right',
                Cell: ({
                    data: { amount, currencyCode },
                }: PropsWithChildren<CellProps<any>>) => (
                    <>{amountFormatter(amount, currencyCode)}</>
                ),
            },
            {
                name: 'currencyCode',
                title: 'Currency',
                Cell: ({
                    data: { currencyCode },
                }: PropsWithChildren<CellProps<any>>) => (
                    <>{getCurrencyLabel(currencyCode)}</>
                ),
            },
            {
                name: 'advancePayeeCurrencyCode',
                title: 'Payment Currency',
                Cell: ({
                    data: { worksheetPaymentContractAdvances },
                }: PropsWithChildren<CellProps<any>>) => {
                    const worksheetPaymentContractAdvancesItem =
                        worksheetPaymentContractAdvances.items[0];
                    return (
                        <>
                            {getCurrencyLabel(
                                worksheetPaymentContractAdvancesItem?.amount
                                    .convertedAmount.currencyCode
                            )}
                        </>
                    );
                },
            },
            {
                name: 'amountPayeeCurrency',
                title: 'Amount in Payment Currency',
                align: 'right',
                Cell: ({
                    data: { worksheetPaymentContractAdvances },
                }: PropsWithChildren<CellProps<any>>) => {
                    const worksheetPaymentContractAdvancesItem =
                        worksheetPaymentContractAdvances.items[0];
                    return (
                        <>
                            {amountFormatter(
                                worksheetPaymentContractAdvancesItem?.amount
                                    .convertedAmount.amount,
                                worksheetPaymentContractAdvancesItem?.amount
                                    .convertedAmount.currencyCode
                            )}
                        </>
                    );
                },
            },
            {
                name: 'withholdingTaxAmountPayeeCurrency',
                title: 'WHT in Payment Currency',
                align: 'right',
                Cell: ({
                    data: { worksheetPaymentContractAdvances },
                }: PropsWithChildren<CellProps<any>>) => {
                    const worksheetPaymentContractAdvancesItem =
                        worksheetPaymentContractAdvances.items[0];
                    return (
                        <>
                            {amountFormatter(
                                worksheetPaymentContractAdvancesItem
                                    ?.withholdingTaxAmount.convertedAmount
                                    .amount,
                                worksheetPaymentContractAdvancesItem
                                    ?.withholdingTaxAmount.convertedAmount
                                    .currencyCode
                            )}
                        </>
                    );
                },
            },
            {
                name: 'vatAmountPayeeCurrency',
                title: 'VAT in Payment Currency',
                align: 'right',
                Cell: ({
                    data: { worksheetPaymentContractAdvances },
                }: PropsWithChildren<CellProps<any>>) => {
                    const worksheetPaymentContractAdvancesItem =
                        worksheetPaymentContractAdvances.items[0];
                    return (
                        <>
                            {amountFormatter(
                                worksheetPaymentContractAdvancesItem?.vatAmount
                                    .convertedAmount.amount,
                                worksheetPaymentContractAdvancesItem?.vatAmount
                                    .convertedAmount.currencyCode
                            )}
                        </>
                    );
                },
            },
            {
                name: 'amountAfterWithholdingAndVatPayeeCurrency',
                title: 'Post Tax Payable Amount in Payment Currency',
                align: 'right',
                Cell: ({
                    data: { worksheetPaymentContractAdvances },
                }: PropsWithChildren<CellProps<any>>) => {
                    const worksheetPaymentContractAdvancesItem =
                        worksheetPaymentContractAdvances.items[0];
                    return (
                        <>
                            {amountFormatter(
                                worksheetPaymentContractAdvancesItem
                                    ?.amountAfterWithholdingAndVat
                                    .convertedAmount.amount,
                                worksheetPaymentContractAdvancesItem
                                    ?.amountAfterWithholdingAndVat
                                    .convertedAmount.currencyCode
                            )}
                        </>
                    );
                },
            },
            {
                name: 'usSourceIncomeRate',
                title: 'US Source Income Rate',
                align: 'right',
                Cell: ({
                    data: { usSourceIncomeRate },
                }: PropsWithChildren<CellProps<any>>) => (
                    <>{rateFormatter(usSourceIncomeRate)}</>
                ),
            },
            {
                name: 'milestone',
                title: 'Milestone',
                Cell: ({
                    data: { milestone },
                }: PropsWithChildren<CellProps<any>>) => (
                    <>{milestoneOptionsMap[milestone]}</>
                ),
            },
            {
                name: 'milestoneDescription',
                title: 'Milestone Description',
                minWidth: '200px',
                Cell: ({
                    data: { milestoneDescription },
                }: PropsWithChildren<CellProps<any>>) => (
                    <div
                        style={{
                            whiteSpace: 'pre-wrap',
                        }}
                    >
                        {milestoneDescription}
                    </div>
                ),
            },
            {
                name: 'milestoneDate',
                title: 'Milestone Reached On',
                Cell: ({
                    data: { milestoneDate },
                }: PropsWithChildren<CellProps<any>>) => <>{milestoneDate}</>,
            },
            {
                name: 'advanceStatus',
                title: 'Status',
                fixed: true,
                Cell: ({
                    data: { worksheetPaymentContractAdvances },
                }: PropsWithChildren<CellProps<any>>) => (
                    <ContractAdvanceQueuedStatus
                        actionStates={
                            worksheetPaymentContractAdvances.items[0]
                                ?.actionStates
                        }
                    />
                ),
            },
        ];

    return CONTRACT_ADVANCE_QUEUED_LIST_COLUMNS;
};

export default ContractAdvanceQueuedListTableColumns;
