import { Schema, model } from 'mongoose';
import {
  IWhatsappDoc,
  IWhatsappModel,
} from '@/modules/communication/whatsapp/whatsapp.interface.js';
import { paginate, toJSON } from '@/shared/utils/plugins/index.js';
import { Status, TriggerPoint } from '@/shared/constants/enum.constant';

const whatsappSchema = new Schema<IWhatsappDoc>(
  {
    companyId: { type: Schema.Types.ObjectId, ref: 'Company' },
    templateId: {
      type: String,
    },
    whatsappStatus: {
      type: String,
    },
    status: {
      type: String,
      enum: Object.values(Status),
      default: Status.ACTIVE,
    },
    name: {
      type: String,
    },
    headerText: {
      type: String,
    },
    bodyText: {
      type: String,
    },
    isDefault: {
      type: Boolean,
      default: false,
    },
       triggerPoint: {
          type: String,
          enum: Object.values(TriggerPoint),
        },
  },
  {
    timestamps: true,
  },
);

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

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

export const Whatsapp = model<IWhatsappDoc, IWhatsappModel>(
  'Whatsapp',
  whatsappSchema,
);
