import type { PropsWithChildren } from 'react';
import React from 'react';
import { Link } from 'react-router-dom';
import { CONTRACT_TYPE_MAP } from 'src/apollo/type-constants/contract';
import { getAccountingPeriodDetail } from 'src/urls/frontend-royalties';
import { getAccountingPeriodStatus } from 'src/utils/accounting-period';
import { getContractEndDateByPeriod } from 'src/utils/date-helpers';
import type { GridTableColumnDefinition } from '@theorchard/suite-components';
import type { CellProps } from '@theorchard/suite-components/dist/esm/src/components/table/base/types';

const AccountingPeriodListTableColumns = () => {
    const ACCOUNTING_PERIOD_LIST_COLUMNS: GridTableColumnDefinition<unknown>[] =
        [
            {
                name: 'contractType',
                title: 'Contract Type',
                maxWidth: '1fr',
                minWidth: 'min-content',
                Cell: ({
                    data: { contractType },
                }: PropsWithChildren<CellProps<any>>) => (
                    <>{CONTRACT_TYPE_MAP[contractType]}</>
                ),
            },
            {
                name: 'accountingPeriodName',
                title: 'Name',
                maxWidth: '1fr',
                minWidth: 'min-content',
                Cell: ({
                    data: { accountingPeriodId, accountingPeriodName },
                }: PropsWithChildren<CellProps<any>>) => (
                    <Link
                        key={accountingPeriodId}
                        to={getAccountingPeriodDetail(accountingPeriodId)}
                    >
                        {accountingPeriodName}
                    </Link>
                ),
            },
            {
                name: 'statementPeriodName',
                title: 'Statement Period',
                maxWidth: '1fr',
                minWidth: 'min-content',
                Cell: ({
                    data: { statementPeriod },
                }: PropsWithChildren<CellProps<any>>) => (
                    <>{statementPeriod?.statementPeriodName}</>
                ),
            },
            {
                name: 'statementPeriodId',
                title: 'Statement Period ID',
                maxWidth: '1fr',
                minWidth: 'min-content',
                Cell: ({
                    data: { statementPeriod },
                }: PropsWithChildren<CellProps<any>>) => (
                    <>{statementPeriod?.statementPeriodId}</>
                ),
            },
            {
                name: 'contractEndDate',
                title: 'Contract End Date',
                maxWidth: '1fr',
                minWidth: 'min-content',
                Cell: ({
                    data: { statementPeriod },
                }: PropsWithChildren<CellProps<any>>) => (
                    <>
                        {getContractEndDateByPeriod(
                            parseInt(statementPeriod?.statementPeriodId, 10)
                        )}
                    </>
                ),
            },
            {
                name: 'accountingPeriodStatus',
                title: 'Status',
                maxWidth: '1fr',
                minWidth: 'min-content',
                Cell: ({
                    data: { accountingPeriodStatus, closedDate },
                }: PropsWithChildren<CellProps<any>>) => (
                    <>
                        {getAccountingPeriodStatus(
                            accountingPeriodStatus,
                            closedDate
                        )}
                    </>
                ),
            },
        ];

    return ACCOUNTING_PERIOD_LIST_COLUMNS;
};

export default AccountingPeriodListTableColumns;
