import { Document, Types, Decimal128 } from 'mongoose';
import { IPaginateModel } from '@/shared/interfaces/model.interface';
import { AuditFields } from '@/shared/types/common';
import {
  LeadPriority,
  LeadInterestType,
  LeadBuyingPreference,
  PropertyAgeStatus,
} from '@/shared/constants/enum.constant';
import { Phone } from '../tasks/tasks.interface';

export interface ILeadBase extends AuditFields {
  contact: Types.ObjectId;
  source: Types.ObjectId;
  leadScore: Number;
  leadStage: Types.ObjectId;
  priority: LeadPriority;
  assignedTo?: Types.ObjectId;
  interestType: LeadInterestType;
  company: Types.ObjectId;
  activityCounts?: {
    callAttempts: number;
    meetings: number;
    siteVisits: number;
    totalActivity: number;
  };
  activityCountsUpdatedAt?: Date;
  shortlisted?: Types.ObjectId[];
  removed?: Types.ObjectId[];
  shortlistedProject?: Types.ObjectId[];
  removedProject?: Types.ObjectId[];
  property?: Types.ObjectId;
  configuration?: Types.ObjectId[];
  category?: Types.ObjectId;
  reason?: string;
  contactDetails?: {
    name: string;
    email: string;
    companyName?: string;
    phone?: Phone[];
  };
  customFields?: {
    [key: string]: string | unknown;
  };
  isConvertedToCustomer?: boolean;
  position: number;
}

export interface IBuyLead extends ILeadBase {
  interestType: LeadInterestType.BUY;

  buyingPreference: LeadBuyingPreference | null;
  unit?: Types.ObjectId;
  loanRequired?: boolean;
  loanStage?: string;
  purpose?: Types.ObjectId;
  project?: Types.ObjectId;
  subCategoryId?: Types.ObjectId;
  preferredState?: Types.ObjectId;
  preferredCity?: Types.ObjectId;
  preferredLocalities?: Types.ObjectId[];
  budget?: Decimal128 | Number;
  propertyCategory?: string;
  propertyType?: [string, string?];
  sourceProjectId?: string;
  sourceProjectName?: string;
  sourceLocality?: string;
  sourceUrl?: string;
}

export interface ISellLead extends ILeadBase {
  interestType: LeadInterestType.SELL;

  propertyCategory?: string;
  propertyType?: [string, string?];
  propertySize?: Decimal128;
  propertyBedrooms?: string;
  propertyAge?: PropertyAgeStatus;
  askingPrice?: Decimal128;
  addressLine1?: string;
  addressLine2?: string;
  landmark?: string;
  state?: Types.ObjectId;
  propertyCity?: Types.ObjectId;
  country?: Types.ObjectId;
  propertyLocality?: Types.ObjectId;
  pincode?: number;
}

export interface IRentLead extends ILeadBase {
  interestType: LeadInterestType.RENT;

  propertyCategory?: string;
  propertyType?: [string, string?];
  propertySize?: Decimal128;
  propertyBedrooms?: string;
  furnishingStatus?: string;
  rentAmount?: Decimal128;
  securityDeposit?: Decimal128;
  availableFrom?: Date;
  addressLine1?: string;
  addressLine2?: string;
  landmark?: string;
  state?: Types.ObjectId;
  propertyCity?: Types.ObjectId;
  country?: Types.ObjectId;
  propertyLocality?: Types.ObjectId;
  pincode?: number;
}

export interface ILeaseLead extends ILeadBase {
  interestType: LeadInterestType.LEASE;

  propertyCategory?: string;
  propertyType?: [string, string?];
  propertySize?: Decimal128;
  furnishingStatus?: string;
  leaseAmount?: Decimal128;
  leaseDeposit?: Decimal128;
  leaseTermYears?: number;
  availableFrom?: Date;
  addressLine1?: string;
  addressLine2?: string;
  landmark?: string;
  state?: Types.ObjectId;
  propertyCity?: Types.ObjectId;
  country?: Types.ObjectId;
  propertyLocality?: Types.ObjectId;
  pincode?: number;
  propertyBedrooms?: string;
}

export type ILead = IBuyLead | ISellLead | IRentLead | ILeaseLead;

export type ILeadDoc = ILead & Document;

export interface ILeadModel extends IPaginateModel<ILeadDoc> {}

export type NewCreatedLead = Partial<ILead> &
  Pick<
    ILeadBase,
    'contact' | 'source' | 'leadStage' | 'priority' | 'interestType'
  > &
  Partial<Pick<AuditFields, 'createdBy' | 'updatedBy'>>;

export type UpdateLeadBody = Partial<ILead>;

export type PopulatedLead = {
  _id: Types.ObjectId;
  assignedTo: {
    firstName: string;
    lastName: string;
    email: string;
    _id: Types.ObjectId;
  };
  assignedBy: {
    firstName: string;
    lastName: string;
    email: string;
   _id: Types.ObjectId;
  };
  source: {
    name: string;
  };
  contact: {
    name: string;
    phone: Phone[];
    firstName: string;
    lastName: string;
  };
  createdBy: {
    firstName: string;
    lastName: string;
    email: string;
   _id: Types.ObjectId;
  };
  contactDetails: {
    name: string;
    email: string;
    companyName?: string;
    phone: Phone[];
  };
  company: { name: string };
  name: string;
  spreadDuration: number;
  status: string;
  budget: Number
};
