import dayjs, { Dayjs } from 'dayjs';
import { FillTarget } from 'chart.js';
import { hexToRgba } from '@utils/functions';
import { ILicenseData, ILicenseReport } from '@red/domain';
import { ComplexFillTarget } from 'chart.js/dist/types';

const monthNames = dayjs.monthsShort();

export const verboseDate = (date: Dayjs) => `${monthNames[date.month()]} ${date.year()}`;

export const getLineConfig: (
    color: string,
    target: FillTarget,
) => {
    type: 'line';
    borderColor: string;
    backgroundColor: string;
    pointBackgroundColor: string;
    fill: ComplexFillTarget;
} = (color, target) => ({
    type: 'line',
    borderColor: hexToRgba(color, 1),
    backgroundColor: hexToRgba(color, 1),
    pointBackgroundColor: hexToRgba(color, 1),
    fill: {
        target: target ?? '-1',
        above: hexToRgba(color, 0.3),
        below: hexToRgba(color, 0.3),
    },
});

export const getLabelsFromMonthlyData = (monthlyData: ILicenseData[]) => monthlyData.map(data => verboseDate(dayjs(data.startDate)));

export const getLabelsFromLicense = (license: ILicenseReport) => {
    let startMonth = dayjs(license.startDate);
    const endMonth = dayjs(license.endDate);

    const result = [];

    while (startMonth.isBefore(endMonth)) {
        result.push(verboseDate(startMonth));
        startMonth = startMonth.add(1, 'month');
    }

    return result;
};
