"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CRMCalculationModel = exports.CRMCalculationSchema = exports.CRMCalculationResultsSchema = exports.CRMCalculationResultsDataSchema = exports.CRMCalculationInputsSchema = exports.CRMYieldCalculationResultsKeys = exports.CRMCalculationInputsValidationSchema = void 0;
const mongoose_1 = __importStar(require("mongoose"));
const joi_1 = __importDefault(require("joi"));
const _models_1 = require("./_models");
exports.CRMCalculationInputsValidationSchema = joi_1.default.object({
    primeRate: joi_1.default.number().required(),
    leverageCost: joi_1.default.number().required(),
    equityCost: joi_1.default.number().required(),
    yearsRevolver: joi_1.default.number().required(),
    yearsTerm: joi_1.default.number().required(),
    commitmentRevolver: joi_1.default.number().required(),
    commitmentTerm: joi_1.default.number().required(),
    utilizationRevolver: joi_1.default.number().required(),
    utilizationTerm: joi_1.default.number().required(),
    marginRevolver: joi_1.default.number().required(),
    marginTerm: joi_1.default.number().required(),
    adminFeeRevolver: joi_1.default.number().required(),
    adminFeeTerm: joi_1.default.number().required(),
    closingPointsRevolver: joi_1.default.number().required(),
    closingPointsTerm: joi_1.default.number().required(),
    annualLineFeeRevolver: joi_1.default.number().required(),
    annualLineFeeTerm: joi_1.default.number().required(),
    unusedLineFee: joi_1.default.number().required(),
    referralPointsRevolver: joi_1.default.number().required(),
    referralPointsTerm: joi_1.default.number().required(),
    referralPercentRevolver: joi_1.default.number().required(),
    referralPercentTerm: joi_1.default.number().required(),
    leveragePercentRevolver: joi_1.default.number().required(),
    leveragePercentTerm: joi_1.default.number().required(),
});
exports.CRMYieldCalculationResultsKeys = [
    'margin',
    'outstanding',
    'interest',
    'adminFee',
    'closing',
    'annual',
    'unusedLineFee',
    'grossYield',
    'referralFeePoints',
    'referralFeePercent',
    'opYield',
    'costOfCapital',
    'netYield',
    'leveragedReturn',
    'leveragedMOIC',
    'impliedIRR',
];
exports.CRMCalculationInputsSchema = new mongoose_1.Schema({
    primeRate: { type: Number, required: true },
    leverageCost: { type: Number, required: true },
    equityCost: { type: Number, required: true },
    yearsTerm: { type: Number, required: true },
    yearsRevolver: { type: Number, required: true },
    commitmentRevolver: { type: Number, required: true },
    commitmentTerm: { type: Number, required: true },
    utilizationRevolver: { type: Number, required: true },
    utilizationTerm: { type: Number, required: true },
    marginRevolver: { type: Number, required: true },
    marginTerm: { type: Number, required: true },
    adminFeeRevolver: { type: Number, required: true },
    adminFeeTerm: { type: Number, required: true },
    closingPointsRevolver: { type: Number, required: true },
    closingPointsTerm: { type: Number, required: true },
    annualLineFeeRevolver: { type: Number, required: true },
    annualLineFeeTerm: { type: Number, required: true },
    unusedLineFee: { type: Number, required: true },
    referralPointsRevolver: { type: Number, required: true },
    referralPointsTerm: { type: Number, required: true },
    referralPercentRevolver: { type: Number, required: true },
    referralPercentTerm: { type: Number, required: true },
    leveragePercentRevolver: { type: Number, required: true },
    leveragePercentTerm: { type: Number, required: true },
}, { timestamps: false, versionKey: false, _id: false });
exports.CRMCalculationResultsDataSchema = new mongoose_1.Schema({
    revolver: { type: Number, required: true },
    term: { type: Number, required: true },
    blended: { type: Number, required: true },
}, { timestamps: false, versionKey: false, _id: false });
const resultFields = Object.fromEntries(exports.CRMYieldCalculationResultsKeys.map((key) => [
    key,
    { type: exports.CRMCalculationResultsDataSchema, required: true },
]));
exports.CRMCalculationResultsSchema = new mongoose_1.Schema(resultFields, { timestamps: false, versionKey: false, _id: false });
exports.CRMCalculationSchema = new mongoose_1.Schema({
    prospectId: {
        type: mongoose_1.default.Schema.Types.ObjectId,
        ref: _models_1.MODEL_NAMES.CRMProspects,
        required: true,
        index: true,
    },
    inputs: { type: exports.CRMCalculationInputsSchema, required: true },
    results: { type: exports.CRMCalculationResultsSchema, required: true },
}, { timestamps: true, versionKey: false });
exports.CRMCalculationModel = (0, mongoose_1.model)(_models_1.MODEL_NAMES.CRMYieldCalculations, exports.CRMCalculationSchema);
