import mongoose, { Document } from 'mongoose';

import { EMicroserviceTask } from '../enums/microservice-task.enum';
import { EMicroserviceTaskStatuses } from '../enums/microservice-task-statuses.enum';
import { MODEL_NAMES } from './_models';

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

export interface IMicroserviceTask {
  cmd: EMicroserviceTask;
  params: any;
  userId: string;
  status: EMicroserviceTaskStatuses;
  executeAt: Date;
}

export interface IMicroserviceTaskDoc extends IMicroserviceTask, Document {
}

export const MicroserviceTaskSchema = new mongoose.Schema(
  {
    cmd: {
      type: String,
      required: true,
    },
    params: {
      type: mongoose.Schema.Types.Mixed,
      required: true,
    },
    userId: {
      type: String,
    },
    status: {
      type: String,
    },
    executeAt: {
      type: Date,
    },
    __v: { type: Number, select: false },
  },
  { timestamps: true }
);

MicroserviceTaskSchema.plugin(mongooseLeanId);

export const MicroserviceTaskModel: mongoose.Model<IMicroserviceTaskDoc> = mongoose.model<IMicroserviceTaskDoc>(MODEL_NAMES.microserviceTasks, MicroserviceTaskSchema);
