import { languageMap } from '@dwengo-1/common/util/language';
import { LearningObjectIdentifier } from '../../../entities/content/learning-object-identifier.js';
import { fetchSubmission } from '../../../services/submissions.js';
import { AuthenticatedRequest } from '../authenticated-request.js';
import { AuthenticationInfo } from '../authentication-info.js';
import { authorize } from './auth-checks.js';
import { FALLBACK_LANG } from '../../../config.js';
import { mapToUsername } from '../../../interfaces/user.js';
import {AccountType} from "@dwengo-1/common/util/account-types";

export const onlyAllowSubmitter = authorize(
    (auth: AuthenticationInfo, req: AuthenticatedRequest) => (req.body as { submitter: string }).submitter === auth.username
);

export const onlyAllowIfHasAccessToSubmission = authorize(async (auth: AuthenticationInfo, req: AuthenticatedRequest) => {
    const { hruid: lohruid, id: submissionNumber } = req.params;
    const { language: lang, version: version } = req.query;

    const loId = new LearningObjectIdentifier(lohruid, languageMap[lang as string] ?? FALLBACK_LANG, Number(version));
    const submission = await fetchSubmission(loId, Number(submissionNumber));

    if (auth.accountType === AccountType.Teacher) {
        // Dit kan niet werken om dat al deze objecten niet gepopulate zijn.
        return submission.onBehalfOf.assignment.within.teachers.map(mapToUsername).includes(auth.username);
    }

    return submission.onBehalfOf.members.map(mapToUsername).includes(auth.username);
});
