/* eslint-disable no-useless-escape */
/* eslint-disable @typescript-eslint/no-explicit-any */

import { CaptureLead } from './captureLeads.model';
import {
  ICaptureLeadDoc,
  NewCaptureLead,
  QueryFilter,
} from './captureLead.interface';
import responseCodes from '@/shared/utils/responseCode/responseCode';
import { ApiError } from '@/shared/utils/errors';
import { defaultStatus } from '@/shared/utils/responseCode/httpStatusAlias';
import { PaginateOptions } from '@/shared/utils/plugins/paginate/paginate';
import { getObjectId } from '@/shared/utils/commonHelper';
import config from '@/shared/config/config';
import { Lead } from '@/modules/lead/lead.model';
import { Team } from '@/modules/teams/teams.model';
import User from '@/modules/user/user.model';
import { Project } from '@/modules/project/project.model';
import {
  Status,
  UserType,
  LeadInterestType,
  LeadBuyingPreference,
} from '@/shared/constants/enum.constant';
import { PageConnection } from '../meta/pageConnection.model';
import { encryptAndCombine } from '../meta/crypto';

const { CaptureLeadResponseCodes } = responseCodes;

export const createCaptureLead = async (
  payload: NewCaptureLead,
  userId: string,
): Promise<{
  id: string;
  name: string;
  platform: string;
  webhookUrl: string;
}> => {
  const existing = await CaptureLead.findOne({
    name: payload.name,
    platform: payload.platform,
    company: getObjectId((payload as any).company),
    isDeleted: { $ne: true },
  });

  if (existing)
    throw new ApiError(
      defaultStatus.OK,
      'Capture Lead already exists',
      true,
      '',
      CaptureLeadResponseCodes.CAPTURE_LEAD_ALREADY_EXISTS,
    );

  // Validate flattened payload and references belong to company
  const companyId = getObjectId((payload as any).company);

  // Normalize assignmentMode constraints
  if (payload.assignmentMode === 'equal') {
    if (!payload.team)
      throw new ApiError(
        defaultStatus.BAD_REQUEST,
        'Team is required for equal',
        true,
      );
    (payload as any).assignedTo = [];
  } else if (payload.assignmentMode === 'roundRobin') {
    if (!Array.isArray(payload.assignedTo) || payload.assignedTo.length < 1)
      throw new ApiError(
        defaultStatus.BAD_REQUEST,
        'At least 1 user required for roundRobin',
        true,
      );
    (payload as any).team = null as any;
  } else if (payload.assignmentMode === 'specificUser') {
    if (!Array.isArray(payload.assignedTo) || payload.assignedTo.length !== 1)
      throw new ApiError(
        defaultStatus.BAD_REQUEST,
        'Exactly 1 user required for specificUser',
        true,
      );
    (payload as any).team = null as any;
  }

  // Enforce buying preference rules for interestType BUY
  if ((payload as any).interestType === LeadInterestType.BUY) {
    const pref = (payload as any).buyingPreference;
    if (pref === LeadBuyingPreference.PREFERRED_PROJECT) {
      if ((payload as any).project) {
        const proj = await Project.findOne({
          _id: getObjectId((payload as any).project),
          companyId,
        })
          .select('_id')
          .lean();
        if (!proj)
          throw new ApiError(
            defaultStatus.BAD_REQUEST,
            'Invalid project for this company',
            true,
          );
      }
    } else if (pref === LeadBuyingPreference.OPEN_TO_SUGGESTION) {
      if ((payload as any).propertyTypeBuy || (payload as any).preferredState) {
        const hasProps =
          Array.isArray((payload as any).propertyTypeBuy) &&
          (payload as any).propertyTypeBuy.length >= 1;
        if ((payload as any).preferredState && !hasProps)
          throw new ApiError(
            defaultStatus.BAD_REQUEST,
            'propertyTypeBuy (>=1) is required when preferredState is provided with open_to_suggestions preference',
            true,
          );
      }
    }
  }

  if ((payload as any).project) {
    const proj = await Project.findOne({
      _id: getObjectId((payload as any).project),
      companyId,
    })
      .select('_id')
      .lean();
    if (!proj)
      throw new ApiError(
        defaultStatus.BAD_REQUEST,
        'Invalid project for this company',
        true,
      );
  }

  // Validate team
  if ((payload as any).team) {
    const team = await Team.findOne({
      _id: getObjectId((payload as any).team),
      companyId,
    })
      .select('_id')
      .lean();
    if (!team)
      throw new ApiError(
        defaultStatus.BAD_REQUEST,
        'Invalid team for this company',
        true,
      );
  }

  // Validate users
  if (
    Array.isArray((payload as any).assignedTo) &&
    (payload as any).assignedTo.length
  ) {
    const users = await User.find({
      _id: { $in: (payload as any).assignedTo.map((u: any) => getObjectId(u)) },
      'company.id': companyId,
      status: Status.ACTIVE,
    })
      .select('_id userType')
      .lean();
    if (users.length !== (payload as any).assignedTo.length)
      throw new ApiError(
        defaultStatus.BAD_REQUEST,
        'Assignees must be users of your company; superAdmin cannot be assigned',
        true,
      );
    if (users.some((u: any) => u.userType === UserType.SUPERADMIN))
      throw new ApiError(
        defaultStatus.BAD_REQUEST,
        'Assignees must be users of your company; superAdmin cannot be assigned',
        true,
      );
  }

  // Validate source exists (same company optional if you enforce)
  if ((payload as any).source && typeof (payload as any).source === 'string')
    // optional: ensure ObjectId shape only
    (payload as any).source = getObjectId((payload as any).source);

  const { company, platform } = payload;
  const isGoogleAds = platform === 'google_ads_form';
  let encryptGoogleAdsWebhooKey;

  if (isGoogleAds) {
    encryptGoogleAdsWebhooKey = await encryptAndCombine(
      config.googleAdsWebhookKey,
    );

    (payload as any).googleAdsWebhookKey = encryptGoogleAdsWebhooKey;
  }

  const captureLead = await CaptureLead.create(payload);

  if (!captureLead)
    throw new ApiError(
      defaultStatus.OK,
      'Failed to create capture lead',
      true,
      '',
      CaptureLeadResponseCodes.CAPTURE_LEAD_ERROR,
    );

  if (payload.platform === 'meta' && payload.metaPageDbId !== null)
    await PageConnection.findOneAndUpdate(
      {
        client: getObjectId(userId),
        companyId: getObjectId(companyId),
        'metaPageIds.pageId': payload.metaPageDbId,
      },
      {
        $set: {
          'metaPageIds.$.captureLead': captureLead._id,
        },
      },
      { new: true },
    );

  const adsPath = isGoogleAds && 'google-ads';

  const baseUrl = `${config.backendUrl}/webhook/leads/${company}/${captureLead._id}`;

  const webhookUrl = isGoogleAds ? `${baseUrl}/${adsPath}` : baseUrl;

  return {
    id: String(captureLead._id),
    name: (payload as any).name,
    platform: (payload as any).platform,
    webhookUrl,
    ...((payload as any).platform === 'google_ads_form'
      ? { googleAdsWebhookKey: encryptGoogleAdsWebhooKey }
      : {}),
  };
};

export const getCaptureLeadById = async (
  id: string,
): Promise<ICaptureLeadDoc | null> => {
  const lead = await CaptureLead.findById(id)
    .populate('project', 'projectName')
    .populate('team', 'name')
    .populate('assignedTo', 'firstName lastName')
    .populate('source', 'name')
    .lean();
  if (!lead)
    throw new ApiError(
      defaultStatus.OK,
      'Capture Lead not found',
      true,
      '',
      CaptureLeadResponseCodes.CAPTURE_LEAD_NOT_FOUND,
    );

  const isGoogleAds = lead.platform === 'google_ads_form';

  const adsPath = isGoogleAds && 'google-ads';

  const baseUrl = `${config.backendUrl}/webhook/leads/${String((lead as any).company)}/${String(lead._id)}`;

  const webhookUrl = isGoogleAds ? `${baseUrl}/${adsPath}` : baseUrl;

  const mode = lead.assignmentMode as 'equal' | 'roundRobin' | 'specificUser';
  const team = lead.team
    ? { id: String((lead.team as any)._id), name: (lead.team as any).name }
    : null;
  const assignedUsersArr = Array.isArray(lead.assignedTo)
    ? (lead.assignedTo as any[])
    : lead.assignedTo
      ? [lead.assignedTo]
      : [];
  let label = 'Invalid Configuration';
  if (mode === 'equal') label = 'Equal Distribution';
  else if (mode === 'roundRobin') label = 'Round Robin';
  else if (mode === 'specificUser') label = 'Specific User';

  return {
    id: String(lead._id),
    name: (lead as any).name,
    platform: (lead as any).platform,
    interestType: (lead as any).interestType,
    isActive: Boolean((lead as any).isActive ?? true),
    project: (lead as any).project
      ? {
          id: String((lead as any).project._id),
          name: (lead as any).project.projectName,
        }
      : null,
    assignment: {
      mode,
      label,
      ...(team ? { team } : {}),
      users: assignedUsersArr.map((u: any) => ({
        id: String(u._id),
        name: `${u.firstName} ${u.lastName}`.trim(),
      })),
    },
    // Mirrors
    assignedTo: assignedUsersArr.map((u: any) => ({
      id: String(u._id),
      name: `${u.firstName} ${u.lastName}`.trim(),
    })),
    team,
    source: (lead as any).source
      ? {
          id: String((lead as any).source._id ?? (lead as any).source),
          name: (lead as any).source?.name,
        }
      : undefined,
    buyingPreference: (lead as any).buyingPreference,
    category: (lead as any).category
      ? { id: String((lead as any).category) }
      : undefined,
    configuration: Array.isArray((lead as any).configuration)
      ? (lead as any).configuration.map((c: any) => ({ id: String(c) }))
      : undefined,
    propertyTypeBuy: Array.isArray((lead as any).propertyTypeBuy)
      ? (lead as any).propertyTypeBuy.map((c: any) => ({ id: String(c) }))
      : undefined,
    preferredState: (lead as any).preferredState
      ? { id: String((lead as any).preferredState) }
      : undefined,
    preferredCity: (lead as any).preferredCity
      ? { id: String((lead as any).preferredCity) }
      : undefined,
    preferredLocalities: Array.isArray((lead as any).preferredLocalities)
      ? (lead as any).preferredLocalities.map((c: any) => ({ id: String(c) }))
      : undefined,
    integrationStatus: String((lead as any).integrationStatus ?? 'active'),
    webhookUrl: webhookUrl,
    ...(lead.googleAdsWebhookKey && {
      googleAdsWebhookKey: lead.googleAdsWebhookKey,
    }),
    ...(String((lead as any).platform) === 'custom_form'
      ? {
          formEmbedHtml: `
<form 
  action="${(lead as any).webhookUrl || `${config.backendUrl}/webhook/leads/${String((lead as any).company)}/${String(lead._id)}`}" 
  method="POST"
  target="mkf_hidden_iframe"
  onsubmit='(function(f){var b=f.querySelector("button[type=submit]");if(b){b.disabled=true;b.style.opacity="0.8";b.style.cursor="not-allowed";b.innerHTML="\n<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"18\" height=\"18\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"white\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" style=\"margin-right:8px;vertical-align:-3px;\"><circle cx=\"12\" cy=\"12\" r=\"10\" opacity=\".25\"></circle><path d=\"M12 2a10 10 0 0 1 10 10\"><animateTransform attributeName=\"transform\" attributeType=\"XML\" type=\"rotate\" from=\"0 12 12\" to=\"360 12 12\" dur=\"0.8s\" repeatCount=\"indefinite\"/></path></svg>Sending…";}})(this)'
  style="
    max-width: 420px;
    margin: 60px auto;
    padding: 32px 26px;
    border-radius: 14px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.08);
    background: #fff;
    font-family: 'Inter', 'Segoe UI', sans-serif;
  "
>
  <div style="text-align:center; margin-bottom:22px;">
    <img 
      src='https://makanify.com/logo.png' 
      alt='Makanify Logo' 
      style="height:48px; margin-bottom:10px;"
      onerror="this.style.display='none'"
    />
    <h2 style="color:#222; font-weight:600; margin:0;">Contact Form</h2>
  </div>

  <input 
    type="text" 
    name="name" 
    placeholder="Your Name" 
    required
    style="
      width:100%;
      padding:12px 14px;
      margin-bottom:16px;
      border:1px solid #e0e0e0;
      border-radius:8px;
      font-size:15px;
      outline:none;
      transition:0.25s;
      box-sizing:border-box;
    "
    onfocus="this.style.borderColor='#F15A29'"
    onblur="this.style.borderColor='#e0e0e0'"
  />

  <input 
    type="email" 
    name="email" 
    placeholder="Your Email" 
    required
    style="
      width:100%;
      padding:12px 14px;
      margin-bottom:16px;
      border:1px solid #e0e0e0;
      border-radius:8px;
      font-size:15px;
      outline:none;
      transition:0.25s;
      box-sizing:border-box;
    "
    onfocus="this.style.borderColor='#F15A29'"
    onblur="this.style.borderColor='#e0e0e0'"
  />

  <input 
    type="tel" 
    name="phone" 
    placeholder="Your Phone" 
    required
    style="
      width:100%;
      padding:12px 14px;
      margin-bottom:16px;
      border:1px solid #e0e0e0;
      border-radius:8px;
      font-size:15px;
      outline:none;
      transition:0.25s;
      box-sizing:border-box;
    "
    onfocus="this.style.borderColor='#F15A29'"
    onblur="this.style.borderColor='#e0e0e0'"
  />

  <textarea 
    name="message" 
    placeholder="Message"
    style="
      width:100%;
      padding:12px 14px;
      height:110px;
      margin-bottom:22px;
      border:1px solid #e0e0e0;
      border-radius:8px;
      font-size:15px;
      outline:none;
      resize:none;
      transition:0.25s;
      box-sizing:border-box;
    "
    onfocus="this.style.borderColor='#F15A29'"
    onblur="this.style.borderColor='#e0e0e0'"
  ></textarea>

  <button 
    type="submit"
    style="
      width:100%;
      padding:14px;
      background:#F15A29;
      color:#fff;
      border:none;
      border-radius:8px;
      font-size:16px;
      font-weight:500;
      cursor:pointer;
      transition:0.25s;
    "
    onmouseover="this.style.background='#d94d22'"
    onmouseout="this.style.background='#F15A29'"
  >
    Submit
  </button>
  <div class="mkf-toast" style="display:none;position:fixed;left:50%;bottom:26px;transform:translateX(-50%);background:#16a34a;color:#fff;border-radius:12px;padding:12px 16px;font-size:14px;box-shadow:0 10px 24px rgba(2,6,23,0.18);">We received your message — we will contact you soon.</div>
</form>
<iframe name="mkf_hidden_iframe" style="display:none" onload="(function(iframe){var f=iframe.previousElementSibling; if(!f||f.tagName!=='FORM') return; var b=f.querySelector('button[type=submit]'); if(b){b.disabled=false;b.style.opacity='';b.style.cursor='';b.textContent='Submit';} var t=f.querySelector('.mkf-toast'); if(t){t.style.display='block'; t.style.opacity='1'; setTimeout(function(){t.style.opacity='0'; t.style.display='none';}, 3500);} try{f.reset();}catch(e){} })(this)"></iframe>`,
        }
      : {}),
    // Audit (nice-to-have)
    createdAt: (lead as any).createdAt,
    updatedAt: (lead as any).updatedAt,
    company: String((lead as any).company),
  } as any;
};

export const updateCaptureLead = async (
  id: string,
  updateData: any,
  currentUser: { _id: string; company: { id: string }; userType?: string },
): Promise<any> => {
  const companyId = getObjectId(currentUser.company.id);

  // Load integration and scope to company + not deleted
  const existing = await CaptureLead.findOne({
    _id: getObjectId(id),
    company: companyId,
    isDeleted: { $ne: true },
  })
    .populate('project', 'projectName')
    .populate('team', 'name')
    .populate('assignedTo', 'firstName lastName')
    .lean();

  if (!existing)
    throw new ApiError(
      defaultStatus.NOT_FOUND,
      'Integration not found',
      true,
      '',
      CaptureLeadResponseCodes.CAPTURE_LEAD_NOT_FOUND,
    );

  // Guard updates if integration is pending
  if ((existing as any).integrationStatus === 'pending')
    throw new ApiError(
      defaultStatus.CONFLICT,
      'Integration is pending activation',
      true,
    );

  // Map incoming API fields
  const updates: any = {};
  if (typeof updateData.name === 'string') updates.name = updateData.name;
  if (typeof updateData.platform === 'string')
    updates.platform = updateData.platform;
  if (typeof updateData.interestType === 'string')
    updates.interestType = updateData.interestType;
  // Accept either 'source' or 'sourceId' (prefer 'source' when both provided)
  if (Object.prototype.hasOwnProperty.call(updateData, 'source'))
    updates.source = updateData.source ? getObjectId(updateData.source) : null;
  else if (Object.prototype.hasOwnProperty.call(updateData, 'sourceId'))
    updates.source = updateData.sourceId
      ? getObjectId(updateData.sourceId)
      : null;

  if (typeof updateData.isActive === 'boolean')
    updates.isActive = updateData.isActive;

  // Validate and map project (accept either project or projectId, prefer 'project')
  if (
    Object.prototype.hasOwnProperty.call(updateData, 'project') ||
    Object.prototype.hasOwnProperty.call(updateData, 'projectId')
  ) {
    const projId = Object.prototype.hasOwnProperty.call(updateData, 'project')
      ? updateData.project
      : updateData.projectId;
    if (projId) {
      const proj = await Project.findOne({
        _id: getObjectId(projId),
        companyId,
      })
        .select('_id')
        .lean();
      if (!proj)
        throw new ApiError(
          defaultStatus.BAD_REQUEST,
          'Invalid project for this company',
          true,
        );
      updates.project = getObjectId(projId);
    } else {
      updates.project = null;
    }
  }

  // Normalize nested assignment object into canonical fields if provided
  if (
    Object.prototype.hasOwnProperty.call(updateData, 'assignment') &&
    updateData.assignment &&
    typeof updateData.assignment === 'object'
  ) {
    const a = updateData.assignment as any;
    if (a.mode != null) updateData.assignmentMode = a.mode;
    if (a.team != null) updateData.team = a.team;
    if (a.teamId != null) updateData.team = a.teamId;
    if (Array.isArray(a.users))
      updateData.assignedTo = a.users
        .map((u: any) => (typeof u === 'string' ? u : u?.id))
        .filter(Boolean);

    if (Array.isArray(a.userIds)) updateData.assignedTo = a.userIds;
  }

  // Accept alias 'userIds' for assignedTo
  if (
    Array.isArray((updateData as any).userIds) &&
    (updateData as any).userIds.length >= 0
  )
    updateData.assignedTo = (updateData as any).userIds;

  // Validate assignment via canonical fields assignmentMode/team/assignedTo
  if (Object.prototype.hasOwnProperty.call(updateData, 'assignmentMode')) {
    const mode = updateData.assignmentMode as
      | 'equal'
      | 'roundRobin'
      | 'specificUser';
    if (!['equal', 'roundRobin', 'specificUser'].includes(mode))
      throw new ApiError(
        defaultStatus.BAD_REQUEST,
        'Invalid assignment mode',
        true,
      );
    updates.assignmentMode = mode;

    if (mode === 'equal') {
      const teamId = updateData.team;
      if (!teamId)
        throw new ApiError(
          defaultStatus.BAD_REQUEST,
          'Invalid assignment: team required for equal',
          true,
        );
      const team = await Team.findOne({ _id: getObjectId(teamId), companyId })
        .select('_id')
        .lean();
      if (!team)
        throw new ApiError(
          defaultStatus.BAD_REQUEST,
          'Invalid team for this company',
          true,
        );
      updates.team = getObjectId(teamId);
      updates.assignedTo = [];
    }

    if (mode === 'roundRobin' || mode === 'specificUser') {
      const list = Array.isArray(updateData.assignedTo)
        ? updateData.assignedTo
        : [];
      if (mode === 'specificUser' && list.length !== 1)
        throw new ApiError(
          defaultStatus.BAD_REQUEST,
          'Invalid assignment: exactly 1 user required for specificUser',
          true,
        );
      if (mode === 'roundRobin' && list.length < 1)
        throw new ApiError(
          defaultStatus.BAD_REQUEST,
          'Invalid assignment: at least 1 user required for roundRobin',
          true,
        );

      const users = await User.find({
        _id: { $in: list.map((u: string) => getObjectId(u)) },
        'company.id': companyId,
        status: Status.ACTIVE,
      })
        .select('_id firstName lastName userType')
        .lean();

      if (users.length !== list.length)
        throw new ApiError(
          defaultStatus.BAD_REQUEST,
          'Assignees must be users of your company; superAdmin cannot be assigned',
          true,
        );
      if (users.some((u: any) => u.userType === UserType.SUPERADMIN))
        throw new ApiError(
          defaultStatus.BAD_REQUEST,
          'Assignees must be users of your company; superAdmin cannot be assigned',
          true,
        );

      updates.assignedTo = users.map((u: any) => u._id);
      updates.team = null;
    }
  }

  // Uniqueness validation: prevent duplicates on update (name + platform within the same company)
  const candidateName = Object.prototype.hasOwnProperty.call(updates, 'name')
    ? updates.name
    : existing.name;
  const candidatePlatform = Object.prototype.hasOwnProperty.call(
    updates,
    'platform',
  )
    ? updates.platform
    : existing.platform;
  if (candidateName && candidatePlatform) {
    const dup = await CaptureLead.findOne({
      _id: { $ne: getObjectId(id) },
      company: companyId,
      name: candidateName,
      platform: candidatePlatform,
      isDeleted: { $ne: true },
    })
      .select('_id')
      .lean();
    if (dup)
      throw new ApiError(
        defaultStatus.CONFLICT,
        'Capture Lead already exists',
        true,
        '',
        CaptureLeadResponseCodes.CAPTURE_LEAD_ALREADY_EXISTS,
      );
  }

  // Apply update
  await CaptureLead.updateOne({ _id: getObjectId(id) }, { $set: updates });

  // Return shaped doc like list
  const refreshed = await CaptureLead.findById(getObjectId(id))
    .populate('project', 'projectName')
    .populate('team', 'name')
    .populate('assignedTo', 'firstName lastName')
    .lean();

  if (!refreshed)
    throw new ApiError(defaultStatus.NOT_FOUND, 'Integration not found', true);

  // Build list shape
  const mode = refreshed.assignmentMode as
    | 'equal'
    | 'roundRobin'
    | 'specificUser';
  const team = refreshed.team
    ? { id: String(refreshed.team._id), name: (refreshed.team as any).name }
    : null;
  const assignedUsersArr = Array.isArray(refreshed.assignedTo)
    ? (refreshed.assignedTo as any[])
    : refreshed.assignedTo
      ? [refreshed.assignedTo]
      : [];
  // Deterministic label
  let label = 'Invalid Configuration';
  if (mode === 'equal') label = 'Equal Distribution';
  else if (mode === 'roundRobin') label = 'Round Robin';
  else if (mode === 'specificUser') label = 'Specific User';

  const counts = await Lead.aggregate([
    { $match: { captureLead: getObjectId(id), company: companyId } },
    { $group: { _id: '$captureLead', count: { $sum: 1 } } },
  ]);
  const leadsCount = counts.length ? counts[0].count : 0;

  return {
    id: String(refreshed._id),
    name: refreshed.name,
    platform: refreshed.platform,
    isActive: Boolean((refreshed as any).isActive ?? true),
    project: refreshed.project
      ? {
          id: String((refreshed.project as any)._id),
          name: (refreshed.project as any).projectName,
        }
      : null,
    assignment: {
      mode,
      label,
      ...(team ? { team } : {}),
      users: assignedUsersArr.map((u: any) => ({
        id: String(u._id),
        name: `${u.firstName} ${u.lastName}`.trim(),
      })),
    },
    leadsCount,
    // Mirrors for backward compatibility
    assignedTo: assignedUsersArr.map((u: any) => ({
      id: String(u._id),
      name: `${u.firstName} ${u.lastName}`.trim(),
    })),
    team,
    integrationStatus: String((refreshed as any).integrationStatus ?? 'active'),
    // Other refs
    source: (refreshed as any).source
      ? {
          id: String((refreshed as any).source),
          name: (refreshed as any).source?.name,
        }
      : undefined,
    interestType: (refreshed as any).interestType,
    buyingPreference: (refreshed as any).buyingPreference,
  };
};

export const queryCaptureLead = async (
  rawFilter: QueryFilter,
  options: PaginateOptions,
) => {
  const {
    search,
    platform,
    interestType,
    company,
    from,
    to,
    sourceId,
    projectId,
    isActive,
    ...baseFilters
  } = rawFilter as any;

  const merged = {
    ...baseFilters,
    ...(platform && { platform }),
    ...(interestType && interestType !== 'all' && { interestType }),
    ...(company && { company: getObjectId(company) }),
    ...(sourceId && { source: getObjectId(sourceId) }),
    ...(projectId && { project: getObjectId(projectId) }),
    ...(typeof isActive !== 'undefined' &&
      isActive !== '' && {
        isActive:
          typeof isActive === 'string'
            ? isActive === 'true'
            : Boolean(isActive),
      }),
    isDeleted: { $ne: true },
  };

  const filter: Record<string, unknown> = Object.fromEntries(
    Object.entries(merged).filter(([, v]) => v != null),
  );

  if (search && typeof search === 'string')
    filter.$or = [
      { name: { $regex: search, $options: 'i' } },
      { platform: { $regex: search, $options: 'i' } },
    ];

  const page = await CaptureLead.paginate(filter, {
    ...options,
    // default sort: newest first if not provided
    sortBy: options?.sortBy || 'createdAt:desc',
    // paginate plugin expects a string like "path:fields;path:fields"
    populate:
      'project:projectName;team:name;assignedTo:firstName,lastName;source:name',
  });

  const ids = page.results
    .map((r: any) => r._id ?? r.id)
    .filter(Boolean)
    .map((v: any) => getObjectId(String(v)));
  const match: Record<string, any> = { captureLead: { $in: ids } };
  if (company) match.company = getObjectId(company);
  if (from || to) {
    const range: any = {};
    if (from) range.$gte = new Date(from);
    if (to) range.$lte = new Date(to);
    match.createdAt = range;
  }

  const counts = await Lead.aggregate([
    { $match: match },
    { $group: { _id: '$captureLead', count: { $sum: 1 } } },
  ]);
  const countMap = new Map<string, number>(
    counts.map((c: any) => [String(c._id), c.count]),
  );

  // const debugId = '68ffbe463b1adcc4a789c9f4';

  const results = page.results.map((r: any) => {
    const rid = String(r._id ?? r.id); // paginate moves _id -> id
    const mode = r.assignmentMode as 'equal' | 'roundRobin' | 'specificUser';
    const team = r.team
      ? { id: String(r.team._id ?? r.team.id), name: r.team.name }
      : null;
    const assignedUsersArr = Array.isArray(r.assignedTo)
      ? r.assignedTo
      : r.assignedTo
        ? [r.assignedTo]
        : [];
    // Deterministic label
    let label = 'Invalid Configuration';
    if (mode === 'equal') label = 'Equal Distribution';
    else if (mode === 'roundRobin') label = 'Round Robin';
    else if (mode === 'specificUser') label = 'Specific User';

    const countKey = String(rid);
    // Normalize source
    const src: any = (r as any).source;
    const source = src
      ? {
          id: src?._id
            ? String(src._id)
            : typeof src === 'string'
              ? String(src)
              : undefined,
          name: src?.name,
        }
      : undefined;

    // if (rid === debugId) {
    //   // eslint-disable-next-line no-console
    //   console.log('CLIST_DEBUG_RAW', { rid, rawName: r.name, rawSourceName: (r as any).source?.name, rawSource: (r as any).source });
    // }

    const mapped = {
      id: rid,
      name: r.name,
      platform: r.platform,
      isActive: Boolean((r as any).isActive ?? true),
      project: r.project
        ? {
            id: String(r.project._id ?? r.project.id),
            name: r.project.projectName,
          }
        : null,
      assignment: {
        mode,
        label,
        ...(team ? { team } : {}),
        users: assignedUsersArr.map((u: any) => ({
          id: String(u._id ?? u.id),
          name: `${u.firstName} ${u.lastName}`.trim(),
        })),
      },
      leadsCount: countMap.get(countKey) ?? 0,
      source,
      interestType: r.interestType,
      integrationStatus: String((r as any).integrationStatus ?? 'active'),
    };

    // if (rid === debugId) {
    //   // eslint-disable-next-line no-console
    //   console.log('CLIST_DEBUG_MAPPED', { rid, name: mapped.name, sourceName: mapped.source?.name });
    // }

    return mapped;
  });

  // try {
  //   // eslint-disable-next-line no-console
  //   console.log('CLIST_DEBUG_SUMMARY', results.slice(0, 20).map((x: any) => ({ id: x.id, name: x.name, src: x.source?.name })));
  // } catch (_e) {
  //   // noop
  // }

  return {
    ...page,
    results,
  };
};

export const deleteCaptureLead = async (
  id: string,
  currentUser: { _id: string; company: { id: string } },
): Promise<{ id: string }> => {
  const companyId = getObjectId(currentUser.company.id);
  const found = await CaptureLead.findOne({
    _id: getObjectId(id),
    company: companyId,
    isDeleted: { $ne: true },
  }).select('_id');

  if (!found)
    throw new ApiError(
      defaultStatus.NOT_FOUND,
      'Integration not found',
      true,
      '',
      CaptureLeadResponseCodes.CAPTURE_LEAD_NOT_FOUND,
    );

  await CaptureLead.updateOne(
    { _id: getObjectId(id) },
    {
      $set: {
        isDeleted: true,
        deletedAt: new Date(),
        deletedBy: getObjectId(currentUser._id),
      },
    },
  );

  return { id: String(found._id) };
};
