"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BorrowerNoteAiSummary = exports.BorrowerNoteAiSummarySchema = exports.PROSPECT_FILE_AI_SUMMARY_STATUSES = exports.AI_SUMMARY_TARGET_TYPES = void 0;
const mongoose_1 = __importDefault(require("mongoose"));
const _models_1 = require("./_models");
exports.AI_SUMMARY_TARGET_TYPES = {
    NOTE: 'NOTE',
    NOTE_FILE: 'NOTE_FILE',
};
exports.PROSPECT_FILE_AI_SUMMARY_STATUSES = {
    PENDING: 'PENDING',
    DONE: 'DONE',
    ERROR: 'ERROR',
};
exports.BorrowerNoteAiSummarySchema = new mongoose_1.default.Schema({
    targetType: {
        type: String,
        enum: exports.AI_SUMMARY_TARGET_TYPES,
        required: true,
        index: true,
    },
    noteId: {
        type: mongoose_1.default.Schema.Types.ObjectId,
        ref: _models_1.MODEL_NAMES.borrowerNote,
        required: true,
        index: true,
    },
    fileId: {
        type: String,
        required: false,
        index: true,
    },
    summary: String,
    subject: String,
    status: {
        type: String,
        enum: exports.PROSPECT_FILE_AI_SUMMARY_STATUSES,
        default: exports.PROSPECT_FILE_AI_SUMMARY_STATUSES.PENDING,
        index: true,
    },
    model: {
        type: String,
        required: false,
    },
    promptSlug: {
        type: String,
        required: false,
    },
    promptVersion: {
        type: Number,
        required: false,
    },
    contentHash: {
        type: String,
        required: false,
    },
    error: {
        type: String,
        required: false,
    },
}, {
    timestamps: true,
    versionKey: false,
});
exports.BorrowerNoteAiSummarySchema.pre('validate', function (next) {
    if (this.targetType === exports.AI_SUMMARY_TARGET_TYPES.NOTE) {
        this.fileId = null;
    }
    if (this.targetType === exports.AI_SUMMARY_TARGET_TYPES.NOTE_FILE && !this.fileId) {
        return next(new Error('fileId is required for NOTE_FILE summary'));
    }
    next();
});
exports.BorrowerNoteAiSummarySchema.index({ noteId: 1, targetType: 1 }, { unique: true, partialFilterExpression: { targetType: exports.AI_SUMMARY_TARGET_TYPES.NOTE } });
exports.BorrowerNoteAiSummarySchema.index({ noteId: 1, fileId: 1, targetType: 1 }, { unique: true, partialFilterExpression: { targetType: exports.AI_SUMMARY_TARGET_TYPES.NOTE_FILE } });
exports.BorrowerNoteAiSummary = mongoose_1.default.model(_models_1.MODEL_NAMES.BorrowerNoteAiSummary, exports.BorrowerNoteAiSummarySchema);
