"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoanSnapshot = exports.LoanSnapshotSchema = exports.LoanSnapshotUpdateValidationSchema = exports.LoanSnapshotCreateValidationSchema = void 0;
const mongoose_1 = __importDefault(require("mongoose"));
const joi_1 = __importDefault(require("joi"));
const _models_1 = require("./_models");
const LoanSnapshotChargeCreateValidationSchema = joi_1.default.object({
    chargeId: joi_1.default.string().required(),
    chargeName: joi_1.default.string().required(),
    amount: joi_1.default.number().required(),
    amountPaid: joi_1.default.number().required(),
});
const LoanSnapshotChargeUpdateValidationSchema = joi_1.default.object({
    chargeId: joi_1.default.string().required(),
    chargeName: joi_1.default.string(),
    amount: joi_1.default.number(),
    amountPaid: joi_1.default.number(),
});
exports.LoanSnapshotCreateValidationSchema = joi_1.default.object({
    loanProductId: joi_1.default.string().required(),
    effectiveDate: joi_1.default.date().required(),
    source: joi_1.default.string().required(),
    balance: joi_1.default.number().required(),
    floatedBalance: joi_1.default.number().required(),
    statementBalance: joi_1.default.number().required(),
    charges: joi_1.default.array()
        .items(LoanSnapshotChargeCreateValidationSchema)
        .required(),
});
exports.LoanSnapshotUpdateValidationSchema = joi_1.default.object({
    _id: joi_1.default.string().required(),
    loanProductId: joi_1.default.string(),
    effectiveDate: joi_1.default.date(),
    source: joi_1.default.string(),
    balance: joi_1.default.number(),
    floatedBalance: joi_1.default.number(),
    statementBalance: joi_1.default.number(),
    charges: joi_1.default.array().items(LoanSnapshotChargeUpdateValidationSchema),
});
exports.LoanSnapshotSchema = new mongoose_1.default.Schema({
    loanProductId: {
        type: mongoose_1.default.Schema.Types.ObjectId,
        ref: _models_1.MODEL_NAMES.loanProducts,
        required: true,
    },
    effectiveDate: {
        type: Date,
        required: true,
    },
    source: {
        type: String,
        required: true,
    },
    balance: {
        type: Number,
        required: true,
    },
    floatedBalance: {
        type: Number,
        required: true,
    },
    statementBalance: {
        type: Number,
        required: true,
    },
    charges: [{
            chargeId: {
                type: mongoose_1.default.Schema.Types.ObjectId,
                ref: _models_1.MODEL_NAMES.loanCharges,
                required: true,
            },
            chargeName: {
                type: String,
                required: true,
            },
            amount: {
                type: Number,
                required: true,
            },
            amountPaid: {
                type: Number,
                required: true,
            },
        }],
}, {
    timestamps: { createdAt: true, updatedAt: true },
    versionKey: false,
});
exports.LoanSnapshotSchema.index({ loanProductId: 1, effectiveDate: 1 }, { unique: true });
exports.LoanSnapshot = mongoose_1.default.model(_models_1.MODEL_NAMES.loanSnapshots, exports.LoanSnapshotSchema);
