import { Schema, model } from 'mongoose';
import {
  IEmailDoc,
  IEmailModel,
} from '@/modules/communication/email/email.interface.js';
import { paginate, toJSON } from '@/shared/utils/plugins/index.js';
import { Status } from '@/shared/constants/enum.constant';

const emailSchema = new Schema<IEmailDoc>(
  {
    key: {
      type: String,
    },
    companyId: { type: Schema.Types.ObjectId, ref: 'Company' },
    templateId: {
      type: Number,
    },
    emailStatus: {
      type: String,
    },
    status: {
      type: String,
      enum: Object.values(Status),
      default: Status.ACTIVE,
    },
    name: {
      type: String,
    },

    subject: {
      type: String,
    },
    isDefault: {
      type: Boolean,
      default: false,
    },
  },
  {
    timestamps: true,
  },
);

// add plugin that converts mongoose to json
emailSchema.plugin(toJSON);
emailSchema.plugin(paginate);

emailSchema.index({ templateId: 1, companyId: 1 }, { unique: true });

export const Email = model<IEmailDoc, IEmailModel>('Email', emailSchema);
