import {useMemo} from "react";
import {MRT_ColumnDef} from "mantine-react-table";
import {PaymentRecordGetSchema} from "../../../../client";
import {PaySchemeType} from "../../../../shared/enums/PaySchemeType.ts";
import {getPluralForm} from "../../../../shared/lib/utils.ts";
import {formatDate} from "../../../../types/utils.ts";
import {isEqual} from "lodash";

export const usePaymentRecordsTableColumns = () => {
    const getWorkUnitsText = (paymentRecord: PaymentRecordGetSchema) => {
        const payrollScheme = paymentRecord.payrollScheme;
        if (payrollScheme.key === PaySchemeType.HOURLY) {
            return getPluralForm(paymentRecord.workUnits, "час", "часа", "часов")
        } else if (
            payrollScheme.key === PaySchemeType.DAILY
        ) {
            return getPluralForm(paymentRecord.workUnits, "день", "дня", "дней")
        } else if (
            payrollScheme.key === PaySchemeType.MONTHLY
        ) {
            return getPluralForm(paymentRecord.workUnits, "месяц", "месяца", "месяцев");
        }
        return "";
    }
    const getDateRangesText = (paymentRecord: PaymentRecordGetSchema) => {
        if (paymentRecord.endDate && !isEqual(paymentRecord.startDate, paymentRecord.endDate)) {
            return `${formatDate(paymentRecord.startDate)} - ${formatDate(paymentRecord.endDate)}`
        }
        return `${formatDate(paymentRecord.startDate)}`;
    }

    return useMemo<MRT_ColumnDef<PaymentRecordGetSchema>[]>(() => [
        {
            header: "Дата начисления",
            Cell: ({row}) => new Date(row.original.createdAt).toLocaleString('ru-RU')
        },
        {
            header: "Получил начисление",
            Cell: ({row}) => `${row.original.user.firstName} ${row.original.user.secondName}`
        },
        {
            header: "Создал начисление",
            Cell: ({row}) => `${row.original.createdByUser.firstName} ${row.original.createdByUser.secondName}`
        },
        {
            header: "Количество",
            Cell: ({row}) => `${row.original.workUnits} ${getWorkUnitsText(row.original)}`
        },
        {
            header: "Сумма начисления",
            Cell: ({row}) => row.original.amount.toLocaleString("ru-RU")
        },
        {
            header: "Временной промежуток",
            Cell: ({row}) => getDateRangesText(row.original)
        }
    ], [])
}
