import { Types } from 'mongoose';
import { NewCreatedContact } from '../contacts/contacts.interface';
import {
  ICategory,
  IConfiguration,
  ISubCategory,
} from '../master/property/property.interfaces';
import {
  IInquiryBody,
  IMakanifyLead,
  IOptions,
} from './makanifySites.interface';
import { NewCreatedLead } from '../lead/lead.interface';
import {
  LeadBuyingPreference,
  LeadInterestType,
  LeadPriority,
} from '@/shared/constants/enum.constant';
import { getObjectId } from '@/shared/utils/commonHelper';

export const makeOptionsList = (
  list: ICategory[] | ISubCategory[] | IConfiguration[],
  key: string,
  value: string,
): IOptions[] =>
  list.map((item) => ({
    label: item[key].charAt(0).toUpperCase() + item[key].slice(1),
    value: item[value],
  }));

export const prepareContactBody = (
  contact: IInquiryBody & { source: string; company: Types.ObjectId },
): NewCreatedContact => {
  const { name, email, phone, message, source, company } = contact;
  const firstName = name.split(' ')?.[0];
  const lastName = name.split(' ')?.[1];
  const countryCode = 91;
  return {
    firstName,
    lastName,
    email,
    phone: [{ countryCode, number: phone }],
    source,
    customFields: { message },
    companyName: name,
    company,
    companyId: company,
    addressLine1: '',
    addressLine2: '',
    country: null,
    state: null,
    city: null,
    pinCode: 0,
    position: '',
    workPhoneNumber: '',
    workEmail: '',
  };
};

export const prepareLeadBody = (
  body: IMakanifyLead,
):
  | NewCreatedLead
  | {
      transferEndDate: Date;
      transferStartDate: Date;
      propertyCategory: Types.ObjectId;
    } => {
  const {
    unit,
    listing,
    source,
    contactInfo,
    leadStageId,
    assignedTo,
    assignedBy,
  } = body;
  return {
    unit,
    buyingPreference: LeadBuyingPreference.OPEN_TO_SUGGESTION,
    contact: contactInfo.id,
    contactDetails: {
      name: `${contactInfo.firstName} ${contactInfo.lastName}`,
      email: contactInfo.email,
      companyName: contactInfo.companyName,
    },
    source,
    company: getObjectId(listing.companyId),
    leadStage: leadStageId,
    priority: LeadPriority.MEDIUM,
    assignedTo,
    interestType: LeadInterestType.BUY,
    project: getObjectId(listing.project),
    subCategoryId: getObjectId(listing.subcategory),
    propertyCategory: getObjectId(listing.propertyType),
    transferEndDate: new Date(),
    transferStartDate: new Date(),
    createdBy: getObjectId(assignedBy),
  };
};
