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 { floatHoursToHoursAndMinutes, formatDate } from "../../../../types/utils.ts";
import { isEqual } from "lodash";

export const usePaymentRecordsTableColumns = () => {
    const getPluralMonths = (months: number) => {
        return getPluralForm(
            months,
            "месяц",
            "месяца",
            "месяцев",
        );
    };
    const getPluralDays = (days: number) => {
        return getPluralForm(
            days,
            "день",
            "дня",
            "дней",
        );
    };
    const getPluralHours = (hours: number) => {
        return getPluralForm(
            hours,
            "час",
            "часа",
            "часов",
        );
    };
    const getPluralMinutes = (minutes: number) => {
        return getPluralForm(
            minutes,
            "минута",
            "минуты",
            "минут",
        );
    };

    const getWorkUnitsText = (paymentRecord: PaymentRecordGetSchema) => {
        const payrollScheme = paymentRecord.payrollScheme;
        if (payrollScheme.key === PaySchemeType.DAILY) {
            return getPluralDays(paymentRecord.workUnits);
        } else if (payrollScheme.key === PaySchemeType.MONTHLY) {
            return getPluralMonths(paymentRecord.workUnits);
        }
        return "";
    };

    const getWorkUnits = (paymentRecord: PaymentRecordGetSchema) => {
        const payrollScheme = paymentRecord.payrollScheme;
        if (payrollScheme.key === PaySchemeType.HOURLY) {
            const [hours, minutes] = floatHoursToHoursAndMinutes(paymentRecord.workUnits);
            const hoursPlural = getPluralHours(hours);
            if (minutes === 0) {
                return `${hours} ${hoursPlural}`;
            }
            const minutesPlural = getPluralMinutes(minutes);
            return `${hours} ${hoursPlural} ${minutes} ${minutesPlural}`;
        }

        return `${paymentRecord.workUnits} ${getWorkUnitsText(paymentRecord)}`;
    };
    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 }) => getWorkUnits(row.original),
            },
            {
                header: "Сумма начисления",
                Cell: ({ row }) => Math.round(row.original.amount).toLocaleString("ru-RU"),
            },
            {
                header: "Временной промежуток",
                Cell: ({ row }) => getDateRangesText(row.original),
            },
        ],
        [],
    );
};
