import mongoose, { Model } from 'mongoose';

import { MODEL_NAMES } from './_models';

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

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

export interface ICompanyLean extends ICompany {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface ICompanyPlain extends ICompany {
  _id: string;
  createdAt: Date;
  updatedAt: Date;
}

export type CompanyModel = Model<ICompany>;

export const CompanySchema = new mongoose.Schema<ICompany, CompanyModel>(
  {
    name: {
      type: String,
      required: true,
      trim: true,
    },
    order: {
      type: Number,
      required: true,
    },
    isDeleted: {
      type: Boolean,
    },
  },
  {
    timestamps: { createdAt: true, updatedAt: false },
  },
);

export const Company = mongoose.model<ICompany, CompanyModel>(MODEL_NAMES.companies, CompanySchema);
