import { string } from 'yup';
import { buildYupChain } from '@/UI-kit/utils';
import {
    correctDateValidator,
    requiredValueValidator,
} from '@/modules/documents/constants/validation/common.validation';
import { uiKitI18n } from '@/configs';

const rangeDate = (value: string) => {
    const userDate = new Date(value);
    const today = new Date();
    const oneYearAgo = new Date();
    const oneYearAfter = new Date();
    oneYearAgo.setFullYear(today.getFullYear() - 1);
    oneYearAfter.setFullYear(today.getFullYear() + 1);

    return { userDate, oneYearAgo, today, oneYearAfter };
};

const patentDocumentNumberValidator = () => ({
    name: 'document',
    message: uiKitI18n?.global.t('helpers.validation.error'),
    test: (value?: string) => {
        return !value || value.length >= 10;
    },
});

const patentBlankNumberValidator = () => ({
    name: 'blank',
    message: uiKitI18n?.global.t('helpers.validation.error'),
    test: (value?: string) => {
        return !value || value.length >= 9;
    },
});

const patentIssueDateMaxNumberValidator = () => ({
    name: 'issue-date-max',
    message: uiKitI18n?.global.t('helpers.validation.patentIssueDateMaxNumberValidator'),
    test: (value?: string) => {
        if (!value || value.length < 9) {
            return true;
        }

        const { userDate, today } = rangeDate(value);

        return userDate < today;
    },
});

const patentIssueDateMinNumberValidator = () => ({
    name: 'issue-date-min',
    message: uiKitI18n?.global.t('helpers.validation.patentIssueDateMinNumberValidator'),
    test: (value?: string) => {
        if (!value || value.length < 9) {
            return true;
        }

        const { userDate, oneYearAgo } = rangeDate(value);
        return userDate >= oneYearAgo;
    },
});

const patentPayDateMinNumberValidator = () => ({
    name: 'issue-date-max',
    message: uiKitI18n?.global.t('helpers.validation.patentPayDateMinNumberValidator'),
    test: (value?: string) => {
        if (!value || value.length < 9) {
            return true;
        }

        const { userDate, oneYearAgo } = rangeDate(value);
        return userDate > oneYearAgo;
    },
});

const patentPayDateMaxNumberValidator = () => ({
    name: 'issue-date-min',
    message: uiKitI18n?.global.t('helpers.validation.patentPayDateMaxNumberValidator'),
    test: (value?: string) => {
        if (!value || value.length < 9) {
            return true;
        }

        const { userDate, oneYearAfter } = rangeDate(value);

        return userDate <= oneYearAfter;
    },
});

export const getPatentValidator = () => ({
    documentNumber: buildYupChain(string(), [
        requiredValueValidator(),
        patentDocumentNumberValidator(),
    ]),
    blankNumber: buildYupChain(string(), [requiredValueValidator(), patentBlankNumberValidator()]),
    issueDate: buildYupChain(string(), [
        requiredValueValidator(),
        correctDateValidator(),
        patentIssueDateMinNumberValidator(),
        patentIssueDateMaxNumberValidator(),
    ]),
    payDate: buildYupChain(string(), [
        requiredValueValidator(),
        correctDateValidator(),
        patentPayDateMinNumberValidator(),
        patentPayDateMaxNumberValidator(),
    ]),
});
