import mongoose from 'mongoose';

import { MODEL_NAMES } from './_models';

export const AI_SUMMARY_TARGET_TYPES = {
  NOTE: 'NOTE',
  NOTE_FILE: 'NOTE_FILE',
} as const;
export type TAiSummaryTargetType = typeof AI_SUMMARY_TARGET_TYPES[keyof typeof AI_SUMMARY_TARGET_TYPES];

export const PROSPECT_FILE_AI_SUMMARY_STATUSES = {
  PENDING: 'PENDING',
  DONE: 'DONE',
  ERROR: 'ERROR',
} as const;
export type TProspectFileAiSummaryStatus = typeof PROSPECT_FILE_AI_SUMMARY_STATUSES[keyof typeof PROSPECT_FILE_AI_SUMMARY_STATUSES];

export interface IBorrowerNoteAiSummary {
  targetType: TAiSummaryTargetType;
  noteId: mongoose.Types.ObjectId;
  fileId?: string;
  summary: string;
  subject: string;
  status: TProspectFileAiSummaryStatus;
  model?: string;
  promptSlug?: string;
  promptVersion?: number;
  contentHash?: string;
  error?: string;
}

export interface IBorrowerNoteAiSummaryDoc extends IBorrowerNoteAiSummary, Document {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface IBorrowerNoteAiSummaryLean extends IBorrowerNoteAiSummary {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface IBorrowerNoteAiSummaryPlain extends Omit<IBorrowerNoteAiSummary, 'fileId' | 'noteId'> {
  _id: string;
  noteId: string;
  fileId: string;
  createdAt: Date;
  updatedAt: Date;
}

export type TProspectFileAiSummaryModel = mongoose.Model<IBorrowerNoteAiSummaryDoc>;

export const BorrowerNoteAiSummarySchema = new mongoose.Schema<IBorrowerNoteAiSummaryDoc>({
  targetType: {
    type: String,
    enum: AI_SUMMARY_TARGET_TYPES,
    required: true,
    index: true,
  },
  noteId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: MODEL_NAMES.borrowerNote,
    required: true,
    index: true,
  },
  fileId: {
    type: String,
    required: false,
    index: true,
  },
  summary: String,
  subject: String,
  status: {
    type: String,
    enum: PROSPECT_FILE_AI_SUMMARY_STATUSES,
    default: 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,
});

BorrowerNoteAiSummarySchema.pre('validate', function(next) {
  if (this.targetType === AI_SUMMARY_TARGET_TYPES.NOTE) {
    this.fileId = null as any;
  }

  if (this.targetType === AI_SUMMARY_TARGET_TYPES.NOTE_FILE && !this.fileId) {
    return next(new Error('fileId is required for NOTE_FILE summary'));
  }

  next();
});

BorrowerNoteAiSummarySchema.index(
  { noteId: 1, targetType: 1 },
  { unique: true, partialFilterExpression: { targetType: AI_SUMMARY_TARGET_TYPES.NOTE } },
);

BorrowerNoteAiSummarySchema.index(
  { noteId: 1, fileId: 1, targetType: 1 },
  { unique: true, partialFilterExpression: { targetType: AI_SUMMARY_TARGET_TYPES.NOTE_FILE } },
);

export const BorrowerNoteAiSummary = mongoose.model<IBorrowerNoteAiSummaryDoc>(MODEL_NAMES.BorrowerNoteAiSummary, BorrowerNoteAiSummarySchema);
