"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BBCSheetModel = exports.BBCSheetSchema = void 0;
const mongoose_1 = __importDefault(require("mongoose"));
const _models_1 = require("./_models");
const InventoryItem_model_1 = require("./InventoryItem.model");
const AccountPayableItem_model_1 = require("./AccountPayableItem.model");
const ReceivableItem_model_1 = require("./ReceivableItem.model");
const SalesItem_model_1 = require("./SalesItem.model");
exports.BBCSheetSchema = new mongoose_1.default.Schema({
    bbcDateId: {
        type: mongoose_1.default.Schema.Types.ObjectId,
        ref: _models_1.MODEL_NAMES.BBCDates,
        required: true,
    },
    dataType: {
        type: String,
        required: true,
        trim: true,
    },
    uploadUser: {
        type: String,
    },
    fileHash: {
        type: String,
        trim: true,
    },
    fileName: {
        type: String,
        trim: true,
    },
    sheetName: {
        type: String,
        trim: true,
    },
    comment: {
        type: String,
        trim: true,
    },
    __v: { type: Number, select: false },
}, { timestamps: true });
exports.BBCSheetSchema.pre('findOneAndDelete', async function () {
    const bbcSheetId = this.getQuery()['_id'];
    await InventoryItem_model_1.InventoryItemModel.deleteMany({ bbcSheetId: bbcSheetId });
    await ReceivableItem_model_1.ReceivableItemModel.deleteMany({ bbcSheetId: bbcSheetId });
    await AccountPayableItem_model_1.AccountPayableItemModel.deleteMany({ bbcSheetId: bbcSheetId });
    await SalesItem_model_1.SalesItem.deleteMany({ bbcSheetId: bbcSheetId });
});
exports.BBCSheetSchema.index({
    dataType: 1,
    bbcDateId: 1,
});
exports.BBCSheetModel = mongoose_1.default.model(_models_1.MODEL_NAMES.BBCSheets, exports.BBCSheetSchema);
