"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateRollNumber = generateRollNumber;
exports.generateUniqueId = generateUniqueId;
exports.generateStudentId = generateStudentId;
exports.generateTeacherId = generateTeacherId;
exports.generateAdminId = generateAdminId;
async function generateRollNumber(enrollmentModel, className, sectionName, academicYearId) {
    const normalizedClass = className.toUpperCase().trim();
    const rollPrefix = sectionName
        ? `${normalizedClass}-${sectionName.toUpperCase().trim()}-`
        : `${normalizedClass}-`;
    const latestEnrollment = await enrollmentModel
        .findOne({
        academicYearId,
        rollNumber: { $regex: `^${rollPrefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}` },
    })
        .sort({ rollNumber: -1 })
        .select('rollNumber')
        .lean();
    let nextNumber = 1;
    if (latestEnrollment?.rollNumber) {
        const match = latestEnrollment.rollNumber.match(/(\d+)$/);
        if (match) {
            nextNumber = parseInt(match[1], 10) + 1;
        }
    }
    const paddedNumber = String(nextNumber).padStart(3, '0');
    return `${rollPrefix}${paddedNumber}`;
}
async function generateUniqueId(model, prefix) {
    const currentYear = new Date().getFullYear();
    const yearPattern = `${prefix}-${currentYear}-`;
    const latestUser = await model
        .findOne({
        uniqueId: { $regex: `^${yearPattern}` },
    })
        .sort({ uniqueId: -1 })
        .select('uniqueId')
        .lean();
    let nextNumber = 1;
    if (latestUser?.uniqueId) {
        const match = latestUser.uniqueId.match(/(\d+)$/);
        if (match) {
            nextNumber = parseInt(match[1], 10) + 1;
        }
    }
    const paddedNumber = String(nextNumber).padStart(4, '0');
    return `${yearPattern}${paddedNumber}`;
}
async function generateStudentId(studentModel) {
    return generateUniqueId(studentModel, 'STU');
}
async function generateTeacherId(teacherModel) {
    return generateUniqueId(teacherModel, 'TCH');
}
async function generateAdminId(adminModel) {
    return generateUniqueId(adminModel, 'ADM');
}
//# sourceMappingURL=id-generator.js.map