"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createBBCSheet = exports.getUniqueDatesForPeriod = exports.getUniqueDates = exports.getBBCSheetsByTypeGroupedByBBC = exports.getBBCSheetsByType = void 0;
const mongoose_1 = __importDefault(require("mongoose"));
const BBCSheet_model_1 = require("../models/BBCSheet.model");
const _models_1 = require("../models/_models");
const getBBCSheetsByType = async (bbcDateIds, collateralType) => {
    return BBCSheet_model_1.BBCSheetModel.aggregate([
        {
            $match: {
                bbcDateId: { $in: bbcDateIds.map((id) => new mongoose_1.default.Types.ObjectId(id)) },
                dataType: collateralType,
            },
        },
        {
            $sort: { updatedAt: 1 },
        },
    ]);
};
exports.getBBCSheetsByType = getBBCSheetsByType;
const getBBCSheetsByTypeGroupedByBBC = async (bbcDateIds, collateralType) => {
    return BBCSheet_model_1.BBCSheetModel.aggregate([
        {
            $match: {
                bbcDateId: { $in: bbcDateIds.map((id) => new mongoose_1.default.Types.ObjectId(id)) },
                dataType: collateralType,
            },
        },
        {
            $sort: { updatedAt: 1 },
        },
        {
            $group: {
                '_id': '$bbcDateId',
                'sheets': {
                    $addToSet: {
                        '_id': '$_id',
                        'bbcDateId': '$bbcDateId',
                        'dataType': '$dataType',
                        'fileName': '$fileName',
                        'sheetName': '$sheetName',
                        'createdAt': '$createdAt',
                        'updatedAt': '$updatedAt',
                        '__v': '$__v',
                    },
                },
            },
        },
    ]);
};
exports.getBBCSheetsByTypeGroupedByBBC = getBBCSheetsByTypeGroupedByBBC;
const getUniqueDates = async (borrowerId, limit, collateral) => {
    return BBCSheet_model_1.BBCSheetModel.aggregate([
        {
            $match: {
                'dataType': collateral,
            },
        },
        {
            $lookup: {
                from: _models_1.MODEL_NAMES.BBCDates,
                localField: 'bbcDateId',
                foreignField: '_id',
                as: 'bbc',
            },
        },
        {
            $unwind: {
                path: '$bbc',
            },
        },
        {
            $match: {
                'bbc.borrowerId': new mongoose_1.default.Types.ObjectId(borrowerId),
            },
        },
        {
            $project: {
                '_id': 1,
                'bbcDate': '$bbc.bbcDate',
            },
        },
        {
            $group: {
                '_id': '$bbcDate',
                'ids': {
                    $addToSet: '$_id',
                },
            },
        },
        {
            $sort: {
                '_id': -1,
            },
        },
        {
            $limit: limit,
        },
        {
            $project: {
                'bbcDate': '$_id',
                '_id': 0,
                'ids': 1,
            },
        },
    ]);
};
exports.getUniqueDates = getUniqueDates;
const getUniqueDatesForPeriod = async (borrowerId, collateral, startDate, endDate) => {
    return BBCSheet_model_1.BBCSheetModel.aggregate([
        {
            $match: {
                'dataType': collateral,
            },
        },
        {
            $lookup: {
                from: _models_1.MODEL_NAMES.BBCDates,
                localField: 'bbcDateId',
                foreignField: '_id',
                as: 'bbc',
            },
        },
        {
            $unwind: {
                path: '$bbc',
            },
        },
        {
            $match: {
                $and: [
                    { 'bbc.borrowerId': new mongoose_1.default.Types.ObjectId(borrowerId) },
                    { 'bbc.bbcDate': { $gte: startDate } },
                    { 'bbc.bbcDate': { $lte: endDate } },
                ],
            },
        },
        {
            $project: {
                '_id': 1,
                'bbcDate': '$bbc.bbcDate',
                'bbcDateId': '$bbc._id',
            },
        },
        {
            $group: {
                '_id': '$bbcDateId',
                'bbcDate': { $first: '$bbcDate' },
                'ids': {
                    $addToSet: '$_id',
                },
            },
        },
        {
            $sort: {
                'bbcDate': 1,
            },
        },
    ]);
};
exports.getUniqueDatesForPeriod = getUniqueDatesForPeriod;
const createBBCSheet = async (bbcDateId, dataType) => {
    const newSheet = {
        bbcDateId: new mongoose_1.default.Types.ObjectId(bbcDateId),
        comment: 'manual',
        dataType,
        fileName: 'manual.xlsx',
        sheetName: 'manual',
    };
    const newBBCSheet = new BBCSheet_model_1.BBCSheetModel(newSheet);
    return newBBCSheet.save();
};
exports.createBBCSheet = createBBCSheet;
