import mongoose, { Document, Model } from 'mongoose';
import Joi from 'joi';

import { MODEL_NAMES } from './_models';

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

export interface IAppraiserWithID extends IAppraiser {
  _id: string;
}

export interface IAppraiserDoc extends IAppraiser, Document {
}

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

export type AppraiserModel = Model<IAppraiser, object, object>;

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

export const Appraiser = mongoose.model<IAppraiser, AppraiserModel>(MODEL_NAMES.appraiser, AppraiserSchema);
