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

import { MODEL_NAMES } from './_models';

export const QuickbooksAccountValidationSchema = Joi.object({
  _id: Joi.string(),
  uiId: Joi.string(),
  companyId: Joi.string(),
  order: Joi.number(),
  accountCode: Joi.string().required(),
  fullName: Joi.string().required(),
  description: Joi.string().required(),
});

export interface IQuickbooksAccount {
  uiId: string;
  companyId: mongoose.Types.ObjectId;
  order?: number;
  accountCode: string;
  fullName: string;
  description: string;
}

export interface IQuickbooksAccountDoc extends IQuickbooksAccount, Document {
}

type QuickbooksAccountModel = Model<IQuickbooksAccount, object, object>;

export const QuickbooksAccountSchema = new mongoose.Schema<IQuickbooksAccount, QuickbooksAccountModel>(
  {
    companyId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.companies,
      required: true,
    },
    uiId: {
      type: String,
      required: true,
    },
    order: {
      type: Number,
    },
    accountCode: {
      type: String,
      trim: true,
      required: true,
    },
    fullName: {
      type: String,
      trim: true,
      required: true,
    },
    description: {
      type: String,
      trim: true,
      required: true,
    },
  },
  { timestamps: true },
);

export const QuickbooksAccount = mongoose.model<IQuickbooksAccount, QuickbooksAccountModel>(MODEL_NAMES.quickbooksAccounts, QuickbooksAccountSchema);
