import { Schema, Types } from 'mongoose';
import {
  LeadPriority,
  LeadInterestType,
} from '@/shared/constants/enum.constant';
import { paginate, toJSON } from '@/shared/utils/plugins';

export const LeadBaseSchema = new Schema(
  {
    contact: { type: Types.ObjectId, ref: 'Contact', required: true },
    contactDetails: {
      name: {
        type: String,
      },
      email: {
        type: String,
      },
      companyName: {
        type: String,
      },
      phone: {
        type: String,
      },
    },
    leadScore: { type: Number, default: 0 },
    source: { type: Types.ObjectId, ref: 'Source', required: true },
    company: { type: Types.ObjectId, ref: 'Company', required: true },
    leadStage: { type: Types.ObjectId, ref: 'LeadStage', required: true },
    priority: {
      type: String,
      enum: Object.values(LeadPriority),
      required: true,
    },
    assignedTo: { type: Types.ObjectId, ref: 'User' },
    cpContact: { type: Types.ObjectId, ref: 'CPContact' },
    captureLead: { type: Types.ObjectId, ref: 'CaptureLead' },
    interestType: {
      type: String,
      required: true,
      enum: Object.values(LeadInterestType),
    },
    addressLine1: String,
    addressLine2: String,
    landmark: String,
    state: { type: Types.ObjectId, ref: 'State' },
    propertyCity: { type: Types.ObjectId, ref: 'City' },
    country: { type: Types.ObjectId, ref: 'Country' },
    propertyLocality: { type: Types.ObjectId, ref: 'Area' },
    pincode: Number,
    propertyType: [{ type: Types.ObjectId, ref: 'SubCategory', default: [] }],
    createdBy: { type: Types.ObjectId, ref: 'User' },
    updatedBy: { type: Types.ObjectId, ref: 'User' },
    shortlisted: [{ type: Types.ObjectId, ref: 'individualProperties' }],
    removed: [{ type: Types.ObjectId, ref: 'individualProperties' }],
    shortlistedProject: [{ type: Types.ObjectId, ref: 'Project' }],
    removedProject: [{ type: Types.ObjectId, ref: 'Project' }],
    property: { type: Types.ObjectId, ref: 'individualProperties' },
    notes: { type: String },
    reason: { type: String },
    previousLeadStage: { type: Types.ObjectId, ref: 'LeadStage' },
    transferType: { type: String },
    transferFrom: { type: Types.ObjectId, ref: 'User' },
    transferStartDate: {
      type: Date,
    },
    transferEndDate: {
      type: Date,
    },
    customFields: {
      type: Map,
      of: Schema.Types.Mixed,
      default: {},
    },
    preferredState: { type: Types.ObjectId, ref: 'State' },
    preferredCity: { type: Types.ObjectId, ref: 'City' },
    preferredLocalities: [{ type: Types.ObjectId, ref: 'Area' }],
    category: {
      type: Types.ObjectId,
      ref: 'Category',
    },
    activityCounts: {
      callAttempts: { type: Number, default: 0 },
      meetings: { type: Number, default: 0 },
      siteVisits: { type: Number, default: 0 },
      totalActivity: { type: Number, default: 0 },
    },
    activityCountsUpdatedAt: { type: Date },
    configuration: [
      { type: Types.ObjectId, ref: 'Configuration', default: [] },
    ],
    isConvertedToCustomer: { type: Boolean, default: false },
    budget: { type: Number, default: 0 },
    position: { type: Number, default: 0 },
  },
  {
    timestamps: true,
    discriminatorKey: 'interestType',
  },
);

LeadBaseSchema.virtual('createdOn').get(function () {
  return this.createdAt ? this.createdAt.toISOString() : null;
});

LeadBaseSchema.virtual('updatedOn').get(function () {
  return this.updatedAt ? this.updatedAt.toISOString() : null;
});

LeadBaseSchema.plugin(toJSON);
LeadBaseSchema.plugin(paginate);

// Helpful index for analytics
LeadBaseSchema.index({ company: 1, captureLead: 1 });
LeadBaseSchema.index({ company: 1, leadStage: 1 });

// Kanban view index for efficient sorting and pagination
LeadBaseSchema.index({ leadStage: 1, position: 1 });

// Index for dashboard queries filtering by assignedTo (prevents COLLSCAN)
LeadBaseSchema.index({ assignedTo: 1 });
