"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AcademicYearRepository = void 0;
const common_1 = require("@nestjs/common");
const mongoose_1 = require("@nestjs/mongoose");
const mongoose_2 = require("mongoose");
const academic_year_schema_1 = require("./academic-year.schema");
const academic_year_utils_1 = require("./academic-year.utils");
let AcademicYearRepository = class AcademicYearRepository {
    model;
    constructor(model) {
        this.model = model;
    }
    serialize(doc) {
        if (!doc)
            return null;
        if (doc._id) {
            if (typeof doc._id !== 'string')
                doc._id = String(doc._id);
            doc.id = doc._id;
        }
        if (doc.startDate && doc.endDate) {
            doc.displayLabel = (0, academic_year_utils_1.displayLabelFromDates)(doc.startDate, doc.endDate);
        }
        return doc;
    }
    serializeMany(docs) {
        return docs.map(doc => {
            if (doc._id) {
                if (typeof doc._id !== 'string')
                    doc._id = String(doc._id);
                doc.id = doc._id;
            }
            if (doc.startDate && doc.endDate) {
                doc.displayLabel = (0, academic_year_utils_1.displayLabelFromDates)(doc.startDate, doc.endDate);
            }
            return doc;
        });
    }
    async create(data) {
        const doc = await this.model.create(data);
        const serialized = this.serialize(doc.toJSON());
        if (!serialized)
            throw new Error('Failed to create academic year');
        return serialized;
    }
    async findById(id) {
        const doc = await this.model.findById(id).lean().exec();
        return this.serialize(doc);
    }
    async findOne(filter) {
        const doc = await this.model.findOne(filter).lean().exec();
        return this.serialize(doc);
    }
    async findMany(filter = {}, sort = { startDate: -1 }) {
        const docs = await this.model.find(filter).sort(sort).lean().exec();
        return this.serializeMany(docs || []);
    }
    async updateById(id, patch) {
        const doc = await this.model.findByIdAndUpdate(id, patch, { new: true, runValidators: true }).lean().exec();
        return this.serialize(doc);
    }
    async unsetAllCurrent() { await this.model.updateMany({ isCurrent: true }, { $set: { isCurrent: false } }); }
    async hasOverlap(startDate, endDate, excludeId) {
        const filter = { $and: [{ startDate: { $lte: endDate } }, { endDate: { $gte: startDate } }] };
        if (excludeId)
            filter._id = { $ne: excludeId };
        return !!(await this.model.exists(filter));
    }
};
exports.AcademicYearRepository = AcademicYearRepository;
exports.AcademicYearRepository = AcademicYearRepository = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, mongoose_1.InjectModel)(academic_year_schema_1.AcademicYear.name)),
    __metadata("design:paramtypes", [mongoose_2.Model])
], AcademicYearRepository);
//# sourceMappingURL=academic-year.repository.js.map