import mongoose, { Model } from 'mongoose';

import { MODEL_NAMES } from './_models';

export interface ICashAllocationReference {
  uiId: string;
  order: number;
  reference: string;
  cashAllocationProductId: mongoose.Types.ObjectId;
}

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

export interface ICashAllocationReferenceLean extends ICashAllocationReference {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface ICashAllocationReferencePlain extends Omit<ICashAllocationReference, 'cashAllocationProductId'> {
  _id: string;
  cashAllocationProductId: string;
  createdAt: Date;
  updatedAt: Date;
}

export type ICashAllocationReferenceStringified = {
  [K in keyof ICashAllocationReference]: K extends '_id' | 'cashAllocationProductId' ? string : ICashAllocationReference[K];
};

export type CashAllocationReferenceModel = Model<ICashAllocationReference>;

export const CashAllocationReferenceSchema = new mongoose.Schema<ICashAllocationReferenceDoc>(
  {
    uiId: {
      type: String,
      required: true,
    },
    order: {
      type: Number,
      required: true,
    },
    reference: {
      type: String,
      required: false,
      trim: true,
    },
    cashAllocationProductId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.cashAllocationProducts,
      required: false,
    },
  },
  {
    timestamps: { createdAt: true, updatedAt: false },
  },
);

export const CashAllocationReference = mongoose.model<ICashAllocationReferenceDoc>(MODEL_NAMES.cashAllocationReferences, CashAllocationReferenceSchema);
