import mongoose from 'mongoose';

import { MODEL_NAMES } from './_models';
import { ILoanTransactionLean } from './LoanTransaction.model';

export interface IPostponedTransaction {
  postponedToDate: string;
  transactionId: mongoose.Types.ObjectId;
  productId: mongoose.Types.ObjectId;
}

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

export interface IPostponedTransactionLean extends IPostponedTransaction {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface IPostponedTransactionPlain extends Omit<IPostponedTransaction, 'transactionId' | 'productId'> {
  _id: string;
  transactionId: string;
  productId: string;
}

export interface IPostponedTransactionWithTransaction extends IPostponedTransactionLean {
  transaction: ILoanTransactionLean;
}

export type TPostponedTransactionModel = mongoose.Model<IPostponedTransaction>;

export const PostponedTransactionSchema = new mongoose.Schema(
  {
    postponedToDate: {
      type: String,
      required: true,
    },
    transactionId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.loanTransactions,
      required: true,
    },
    productId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.loanProducts,
      required: true,
    },
    __v: {
      type: Number,
      select: false,
    },
  },
  { timestamps: false },
);

export const PostponedTransaction = mongoose.model<IPostponedTransactionDoc>(MODEL_NAMES.postponedTransactions, PostponedTransactionSchema);
