import { Schema, model } from 'mongoose';
import { toJSON, paginate } from '@/shared/utils/plugins';
import { ICampaignDoc, ICampaignModel } from './campaign.interface';

const campaignSchema = new Schema<ICampaignDoc>(
  {
    name: { type: String, required: true },
    description: { type: String },
    trackingLink: { type: String },
    type: { type: String, enum: ['project', 'property'] },
    project: {
      type: Schema.Types.ObjectId,
      ref: 'Project',
    },
    property: {
      type: Schema.Types.ObjectId,
      ref: 'individualProperties',
    },

    targetedCity: [{ type: Schema.Types.ObjectId, ref: 'City' }],
    targetedAreas: [{ type: Schema.Types.ObjectId, ref: 'Area' }],

    budgetRange: {
      min: { type: Number },
      max: { type: Number },
    },

    category: { type: Schema.Types.ObjectId, ref: 'Category' },
    subCategories: [{ type: Schema.Types.ObjectId, ref: 'SubCategory' }],

    leadIds: [{ type: Schema.Types.ObjectId, ref: 'Lead' }],

    channel: [{ type: String, enum: ['whatsapp', 'email', 'sms'] }],

    //TODO: change this type to ref with sms,email & whatsapp.
    whatsappTemplates: { type: String },
    emailTemplates: { type: String },
    smsTemplates: { type: String },

    team: { type: Schema.Types.ObjectId, ref: 'Team' },

    targetStrategy: { type: String, enum: ['all', 'spread'] },
    sendToAll: { type: Boolean, default: false },

    //TODO: change enum according to frontend.
    spreadDuration: { type: Number, enum: [2, 5, 7] },

    launchTime: {
      type: String,
      enum: ['immediate', 'schedule'],
      default: 'immediate',
    },
    scheduleTime: { type: Date },
    company: { type: Schema.Types.ObjectId, ref: 'Company' },
    status: {
      type: String,
      enum: ['running', 'draft', 'pending', 'scheduled'],
      default: 'pending',
    },
    createdBy: { type: Schema.Types.ObjectId, ref: 'User' },
    updatedBy: { type: Schema.Types.ObjectId, ref: 'User' },
  },
  {
    timestamps: true,
  },
);

campaignSchema.plugin(toJSON);
campaignSchema.plugin(paginate);

export const Campaign = model<ICampaignDoc, ICampaignModel>('Campaign', campaignSchema);
