import mongoose, { Document } from 'mongoose';

import { MODEL_NAMES } from './_models';
import { IUploadedFileDoc } from './UploadedFile.model';

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

export interface IUploadedSheetView {
  _id: mongoose.Types.ObjectId;
  fileId: mongoose.Types.ObjectId;
  sheetName: string;
  fileName: string;
}

export interface IUploadedSheet {
  fileId: mongoose.Types.ObjectId;
  sheetName: string;
  total: number;
}

export interface IUploadedSheetDoc extends IUploadedSheet, Document {
}

export interface IUploadedSheetDocWithFile extends IUploadedSheetDoc {
  file: IUploadedFileDoc;
}

export const UploadedSheetSchema = new mongoose.Schema(
  {
    fileId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.uploadedFile,
      required: true,
    },
    sheetName: {
      type: String,
      required: true,
      trim: true,
    },
    total: {
      type: Number,
      required: true,
    },
    __v: { type: Number, select: false },
  },
  { timestamps: true }
);

UploadedSheetSchema.plugin(mongooseLeanId);

export default mongoose.model<IUploadedSheetDoc>(MODEL_NAMES.uploadedSheets, UploadedSheetSchema);
