import mongoose from 'mongoose';
import Joi from 'joi';

import { MODEL_NAMES } from './_models';

export interface IAuditor {
  isDeleted: boolean;
  name: string;
  order?: number;
}

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

export interface IAuditorLean extends IAuditor {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface IAuditorPlain extends IAuditor {
  _id: string;
  createdAt: Date;
  updatedAt: Date;
}

export const AuditorValidationSchema = Joi.object({
  _id: Joi.string(),
  name: Joi.string().required(),
  isDeleted: Joi.boolean(),
  order: Joi.number().required().allow  (null),
});

export type AuditorModel = mongoose.Model<IAuditor>;

export const AuditorSchema = new mongoose.Schema<IAuditor, AuditorModel>(
  {
    isDeleted: {
      type: Boolean,
      default: false,
    },
    name: {
      type: String,
      required: true,
    },
    order: {
      type: Number,
      required: true,
    },
  },
  {
    timestamps: { createdAt: true, updatedAt: true },
    versionKey: false,
  },
);

export const Auditor = mongoose.model<IAuditor, AuditorModel>(MODEL_NAMES.auditors, AuditorSchema);
