import mongoose, { Document } from 'mongoose';

import { MODEL_NAMES } from './_models';
import { EUploadTypes } from '../enums/upload-types.enum';

const mongooseLeanId = require('../plugins/id.plugin');

export interface IUploadedFile {
  filename: string;
  originalName: string;
  borrowerId: mongoose.Types.ObjectId;
  userId: string;
  converted?: boolean;
  type: EUploadTypes;
}

export interface IUploadedFileDoc extends IUploadedFile, Document {
}

export const UploadedFileSchema = new mongoose.Schema(
  {
    filename: {
      type: String,
      required: false,
      trim: false,
    },
    originalName: {
      type: String,
      required: false,
      trim: false,
    },
    borrowerId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.borrowers,
      required: false,
    },
    userId: {
      type: String,
      required: false,
    },
    converted: {
      type: Boolean,
      default: false,
    },
    type: {
      type: String,
      required: true,
    },
    __v: { type: Number, select: false },
  },
  { timestamps: true }
);

UploadedFileSchema.plugin(mongooseLeanId);

export default mongoose.model<IUploadedFileDoc>(MODEL_NAMES.uploadedFile, UploadedFileSchema);
