import React from 'react';
import { GridTable, HelpTooltip } from '@theorchard/suite-components';
import { isEmpty } from 'lodash-es';
import { getCountryName } from 'src/utils/country';
import type { GetAbacusReferenceVatRatesQuery } from 'src/apollo/queries/__generated__/reference-vat-rate';

type AbacusReferenceVatRatesItems = NonNullable<
    GetAbacusReferenceVatRatesQuery['abacusReferenceVatRates']['items']
>;
type AbacusReferenceVatRatesItem = NonNullable<AbacusReferenceVatRatesItems[0]>;

export interface VatRatesPropType {
    rateList: (AbacusReferenceVatRatesItem | null)[] | null;
    isLoading: boolean;
}

export const VatRates: React.FC<VatRatesPropType> = ({ rateList }) => {
    const formatBoolean = (
        booleanValue: boolean | null,
        nullValue: string | null = null
    ) => {
        if (booleanValue === true) return 'Yes';
        if (booleanValue === false) return 'No';
        return nullValue || 'N/A';
    };

    const formatTransactionType = (transactionType: string | null) =>
        transactionType || 'All';

    const formatTaxRate = (taxRate: string | null) => {
        if (taxRate !== null) return `${parseInt(taxRate, 10)}%`;
        return 'N/A';
    };

    const formatVATRates = () => {
        if (!isEmpty(rateList)) {
            return rateList?.map(rate => {
                const {
                    effectiveDate,
                    countryOfTaxPolicy,
                    isVatRegisteredInCountryOfTaxPolicy,
                    transactionType,
                    isTaxApplicable,
                    taxRate,
                } = rate as AbacusReferenceVatRatesItem;
                return {
                    effectiveDate: effectiveDate,
                    countryOfTaxPolicy:
                        getCountryName(countryOfTaxPolicy) || 'Other',
                    isVatRegisteredInCountryOfTaxPolicy: formatBoolean(
                        isVatRegisteredInCountryOfTaxPolicy
                    ),
                    transactionType: formatTransactionType(transactionType),
                    taxApplicableRate: formatBoolean(isTaxApplicable, 'Exempt'),
                    taxRate: formatTaxRate(taxRate),
                };
            });
        }
        return [];
    };

    return (
        <>
            <div className="tableHeader">
                <h2>
                    UK VAT Rates{' '}
                    <HelpTooltip
                        id="tableHeaderTooltip"
                        message="Calculated from Closing Balance (Pre-Tax)"
                    />
                </h2>
            </div>
            <GridTable
                bordered
                data={formatVATRates()}
                columnDefs={[
                    {
                        name: 'effectiveDate',
                        title: 'Effective Date',
                    },
                    {
                        name: 'countryOfTaxPolicy',
                        title: 'Country of Signing Entity',
                    },
                    {
                        name: 'isVatRegisteredInCountryOfTaxPolicy',
                        title: 'Account VAT Registered in Country of signing entity?',
                        tooltip: 'Valid VAT number provided',
                        tooltipStyle: 'icon',
                    },
                    {
                        name: 'transactionType',
                        title: 'Transaction Type',
                    },
                    {
                        name: 'taxApplicableRate',
                        title: 'Tax Applicable?',
                    },
                    {
                        name: 'taxRate',
                        title: 'Tax Rate',
                        template: { align: 'right' },
                    },
                ]}
            />
        </>
    );
};

export default VatRates;
