import { getFormDraftContentReferenceIds } from '../../../../src/api/kernels/forms/business/types/formDraftContent';
import { PerformanceReviewFormTemplateWithScoringInfoSC } from '../../../../src/api/modules/performanceReviews/presentation/serializationClasses/performanceReviewFormTemplateWithScoringInfoSC';
import { QUESTION_TEMPLATES_QUESTION_TYPES } from '../../../../src/api/modules/questionTemplates/business/types/types';
import { fakePositiveInteger } from '../../../common/common';
import { generateQuestionTemplateWithValues } from '../../questionTemplates/common';
import {
    checkPerformanceReviewFormTemplateWithScoringInfoSerialized,
    generatePerformanceReviewFormTemplateWithDraft,
    generateQuestionWeightsFromIds,
    generateRandomPerformanceReviewFormTemplate,
} from '../common';

describe('PerformanceReviewFormTemplateWithScoringInfoSC', () => {
    it('should serialize review form template with non-empty question weights', () => {
        const reviewFormTemplate = generatePerformanceReviewFormTemplateWithDraft(fakePositiveInteger(), fakePositiveInteger(), {
            withScore: true,
        });
        const referenceIds = getFormDraftContentReferenceIds(reviewFormTemplate.template!.formTemplate!.draft!.content);
        reviewFormTemplate.scoreConfig!.formTemplateQuestionWeights = generateQuestionWeightsFromIds(referenceIds);
        const reviewFormTemplateSerialized = new PerformanceReviewFormTemplateWithScoringInfoSC().serialize(reviewFormTemplate);

        expect(reviewFormTemplateSerialized).not.toBeNull();

        checkPerformanceReviewFormTemplateWithScoringInfoSerialized(reviewFormTemplate, reviewFormTemplateSerialized);
    });

    it('should serialize review form template with rating question templates', () => {
        const instanceId = fakePositiveInteger();
        const reviewFormTemplate = generateRandomPerformanceReviewFormTemplate(fakePositiveInteger());
        const questionsCount = fakePositiveInteger({ max: 3, min: 1 });
        reviewFormTemplate.ratingQuestionTemplates = Array(questionsCount)
            .fill(1)
            .map(() => generateQuestionTemplateWithValues(fakePositiveInteger(), instanceId, { type: QUESTION_TEMPLATES_QUESTION_TYPES.RATING }));
        const reviewFormTemplateSerialized = new PerformanceReviewFormTemplateWithScoringInfoSC().serialize(reviewFormTemplate);

        expect(reviewFormTemplateSerialized).not.toBeNull();

        checkPerformanceReviewFormTemplateWithScoringInfoSerialized(reviewFormTemplate, reviewFormTemplateSerialized);
    });
});
