import { CWOpportunity } from "./opportunity.types";
import { normalizeProbabilityPercent } from "../../sales-utils/normalizeProbability";

export type ProcessedOpportunity = ReturnType<
  typeof processOpportunityResponse
>;

/**
 * Processes raw CW opportunity data into a cleaner, normalized shape
 * suitable for API responses and internal consumption.
 */
export const processOpportunityResponse = (opportunity: CWOpportunity) => ({
  id: opportunity.id,
  name: opportunity.name,
  expectedCloseDate: opportunity.expectedCloseDate,
  closedDate: opportunity.closedDate,
  closedFlag: opportunity.closedFlag,
  probability: normalizeProbabilityPercent(opportunity.probability?.name),
  type: opportunity.type
    ? { id: opportunity.type.id, name: opportunity.type.name }
    : null,
  stage: opportunity.stage
    ? { id: opportunity.stage.id, name: opportunity.stage.name }
    : null,
  status: opportunity.status
    ? { id: opportunity.status.id, name: opportunity.status.name }
    : null,
  priority: opportunity.priority
    ? { id: opportunity.priority.id, name: opportunity.priority.name }
    : null,
  rating: opportunity.rating
    ? { id: opportunity.rating.id, name: opportunity.rating.name }
    : null,
  source: opportunity.source,
  notes: opportunity.notes,
  customerPO: opportunity.customerPO,
  company: opportunity.company
    ? {
        id: opportunity.company.id,
        identifier: opportunity.company.identifier,
        name: opportunity.company.name,
      }
    : null,
  contact: opportunity.contact
    ? { id: opportunity.contact.id, name: opportunity.contact.name }
    : null,
  site: opportunity.site
    ? { id: opportunity.site.id, name: opportunity.site.name }
    : null,
  primarySalesRep: opportunity.primarySalesRep
    ? {
        id: opportunity.primarySalesRep.id,
        identifier: opportunity.primarySalesRep.identifier,
        name: opportunity.primarySalesRep.name,
      }
    : null,
  secondarySalesRep: opportunity.secondarySalesRep
    ? {
        id: opportunity.secondarySalesRep.id,
        identifier: opportunity.secondarySalesRep.identifier,
        name: opportunity.secondarySalesRep.name,
      }
    : null,
  closedBy: opportunity.closedBy
    ? {
        id: opportunity.closedBy.id,
        identifier: opportunity.closedBy.identifier,
        name: opportunity.closedBy.name,
      }
    : null,
  campaign: opportunity.campaign
    ? { id: opportunity.campaign.id, name: opportunity.campaign.name }
    : null,
  totalSalesTax: opportunity.totalSalesTax,
  location: opportunity.location
    ? { id: opportunity.location.id, name: opportunity.location.name }
    : null,
  department: opportunity.department
    ? { id: opportunity.department.id, name: opportunity.department.name }
    : null,
  pipelineChangeDate: opportunity.pipelineChangeDate,
  dateBecameLead: opportunity.dateBecameLead,
  info: opportunity._info,
});

/**
 * Processes an array of raw CW opportunities.
 */
export const processOpportunitiesResponse = (opportunities: CWOpportunity[]) =>
  opportunities.map(processOpportunityResponse);
