import { model, Schema } from 'mongoose';
import {
  IWebSiteBuilderDoc,
  IWebSiteBuilderModel,
  LeadRoutingType,
} from './makanifySites.interface';
import { paginate, toJSON } from '@/shared/utils/plugins';

const makanifySiteSchema = new Schema<IWebSiteBuilderDoc>(
  {
    subdomainName: { type: String, required: true, unique: true, index: true },
    template: { type: String, required: true },
    leadRoutingType: {
      type: String,
      enum: LeadRoutingType,
      required: true,
      default: LeadRoutingType.ROUNDROBIN,
    },
    userId: { type: Schema.Types.ObjectId, required: false },
    teamId: { type: Schema.Types.ObjectId, required: false },
    companyName: { type: String, required: true },
    companyLogo: { type: String, required: false },
    contactNumber: {
      dialCode: {
        type: Number,
        required: false,
      },
      number: {
        type: Number,
        required: false,
      },
    },
    whatsappNumber: {
      dialCode: {
        type: Number,
        required: false,
      },
      number: {
        type: Number,
        required: false,
      },
    },
    heroBannerTitle: { type: String, required: false },
    heroBannerDesc: { type: String, required: false },
    bannerImg: { type: String, required: false },
    aboutTitle: { type: String, required: false },
    aboutDesc: { type: String, required: false },
    aboutImg: { type: String, required: false },
    serviceTitle: { type: String, required: false },
    serviceDesc: { type: String, required: false },
    findPropertyTitle: { type: String, required: false },
    findPropertyDesc: { type: String, required: false },
    sellPropertyTitle: { type: String, required: false },
    sellPropertyDesc: { type: String, required: false },
    rentPropertyTitle: { type: String, required: false },
    rentPropertyDesc: { type: String, required: false },
    createdBy: { type: Schema.Types.ObjectId, ref: 'User' },
    updatedBy: { type: Schema.Types.ObjectId, ref: 'User' },
    company: {
      type: Schema.Types.ObjectId,
      required: true,
      ref: 'Company',
      index: true,
    },
    isPublished: { type: Schema.Types.Boolean, default: false },
    leadNotifyAdmin: {
      type: Boolean,
      default: false,
    },
  },
  {
    timestamps: true,
  },
);

makanifySiteSchema.pre('validate', function (next) {
  const doc = this as IWebSiteBuilderDoc;

  if (doc.leadRoutingType === LeadRoutingType.USER && !doc.userId)
    return next(new Error('userId is required when leadRoutingType is "user"'));

  if (doc.leadRoutingType === LeadRoutingType.TEAM && !doc.teamId)
    return next(new Error('teamId is required when leadRoutingType is "team"'));

  if (
    doc.leadRoutingType === LeadRoutingType.ROUNDROBIN &&
    (doc.teamId || doc.userId)
  )
    return next(
      new Error(
        'userId and teamId must be empty when leadRoutingType is "roundrobin"',
      ),
    );

  next();
});

makanifySiteSchema.virtual('phoneNumber').get(function () {
  return (
    this.contactNumber.dialCode &&
    this.contactNumber.number &&
    `+${this.contactNumber.dialCode}${this.contactNumber.number}`
  );
});

makanifySiteSchema.virtual('whatsapp').get(function () {
  return (
    this.whatsappNumber.dialCode &&
    this.whatsappNumber.number &&
    `+${this.whatsappNumber.dialCode}${this.whatsappNumber.number}`
  );
});

makanifySiteSchema.plugin(toJSON);
makanifySiteSchema.plugin(paginate);

export const MakanifySites = model<IWebSiteBuilderDoc, IWebSiteBuilderModel>(
  'MakanifySites',
  makanifySiteSchema,
);
