interface CWReference {
  id: number;
  name: string;
  _info?: Record<string, string>;
}

interface CWMemberReference {
  id: number;
  identifier: string;
  name: string;
  _info?: Record<string, string>;
}

interface CWCompanyReference {
  id: number;
  identifier: string;
  name: string;
  _info?: Record<string, string>;
}

interface CWContactReference {
  id: number;
  name: string;
  _info?: Record<string, string>;
}

interface CWSiteReference {
  id: number;
  name: string;
  _info?: Record<string, string>;
}

export interface CWCustomField {
  id: number;
  caption: string;
  type: string;
  entryMethod: string;
  numberOfDecimals: number;
  value: unknown;
  connectWiseId: string;
  rowNum: number;
  userDefinedFieldRecId: number;
  podId: string;
}

export interface CWOpportunity {
  id: number;
  name: string;
  expectedCloseDate: string;
  type: CWReference;
  stage: CWReference;
  status: CWReference;
  priority: CWReference;
  notes: string;
  source: string;
  rating: CWReference;
  campaign: CWReference;
  primarySalesRep: CWMemberReference;
  secondarySalesRep: CWMemberReference;
  locationId: number;
  businessUnitId: number;
  company: CWCompanyReference;
  contact: CWContactReference;
  site: CWSiteReference;
  customerPO: string;
  pipelineChangeDate: string;
  dateBecameLead: string;
  closedDate: string;
  closedBy: CWMemberReference;
  totalSalesTax: number;
  probability: CWReference;
  shipToCompany: CWCompanyReference;
  shipToContact: CWContactReference;
  shipToSite: CWSiteReference;
  billToCompany: CWCompanyReference;
  billToContact: CWContactReference;
  billToSite: CWSiteReference;
  billingTerms: CWReference;
  taxCode: CWReference;
  currency: CWReference;
  companyLocationId: number;
  location: CWReference;
  department: CWReference;
  closedFlag: boolean;
  mobileGuid: string;
  customFields: CWCustomField[];
  _info: CWOpportunityInfo;
}

export interface CWOpportunityInfo {
  lastUpdated: string;
  updatedBy: string;
  dateEntered: string;
  enteredBy: string;
  forecasts_href: string;
  notes_href: string;
  products_href: string;
  contacts_href: string;
  configurations_href: string;
  team_href: string;
  documents_href: string;
  activities_href: string;
}

export interface CWForecastItem {
  id: number;
  forecastDescription: string;
  opportunity: CWReference;
  quantity: number;
  status: CWReference;
  catalogItem?: {
    id: number;
    identifier: string;
    _info?: Record<string, string>;
  };
  productDescription: string;
  customerDescription?: string;
  productClass: string;
  revenue: number;
  cost: number;
  margin: number;
  percentage: number;
  includeFlag: boolean;
  quoteWerksQuantity: number;
  forecastType: string;
  linkFlag: boolean;
  recurringRevenue: number;
  recurringCost: number;
  cycles: number;
  recurringFlag: boolean;
  sequenceNumber: number;
  subNumber: number;
  taxableFlag: boolean;
  procurementNotes?: string | null;
  productNarrative?: string | null;
  customFields?: CWCustomField[];
  _info?: Record<string, string>;
}

export interface CWForecastRevenueSummary {
  id: number;
  revenue: number;
  cost: number;
  margin: number;
  percentage: number;
  _info?: Record<string, string>;
}

export interface CWForecast {
  id: number;
  forecastItems: CWForecastItem[];
  productRevenue: CWForecastRevenueSummary;
  serviceRevenue: CWForecastRevenueSummary;
  agreementRevenue: CWForecastRevenueSummary;
  timeRevenue: CWForecastRevenueSummary;
  expenseRevenue: CWForecastRevenueSummary;
  forecastRevenueTotals: CWForecastRevenueSummary;
  inclusiveRevenueTotals: CWForecastRevenueSummary;
  recurringTotal: number;
  wonRevenue: CWForecastRevenueSummary;
  lostRevenue: CWForecastRevenueSummary;
  openRevenue: CWForecastRevenueSummary;
  otherRevenue1: CWForecastRevenueSummary;
  otherRevenue2: CWForecastRevenueSummary;
  salesTaxRevenue: number;
  forecastTotalWithTaxes: number;
  expectedProbability: number;
  taxCode: CWReference;
  billingTerms: CWReference;
  currency: {
    id: number;
    symbol: string;
    currencyCode: string;
    name: string;
    _info?: Record<string, string>;
  };
  _info?: Record<string, string>;
}

export interface CWOpportunityNote {
  id: number;
  opportunity: CWReference;
  text: string;
  type: CWReference;
  flagged: boolean;
  enteredBy: string;
  mobileGuid: string;
  _info?: Record<string, string>;
}

export interface CWOpportunityNoteCreate {
  text: string;
  type?: { id: number };
  flagged?: boolean;
}

export interface CWOpportunityNoteUpdate {
  text?: string;
  type?: { id: number };
  flagged?: boolean;
}

export interface CWOpportunityContact {
  id: number;
  opportunity: CWReference;
  contact: CWContactReference;
  company: CWCompanyReference;
  role: CWReference;
  notes: string;
  referralFlag: boolean;
  _info?: Record<string, string>;
}

export interface CWForecastItemCreate {
  catalogItem?: { id: number };
  forecastDescription?: string;
  productDescription?: string;
  quantity?: number;
  status?: { id: number };
  productClass?: string;
  forecastType?: string;
  revenue?: number;
  cost?: number;
  includeFlag?: boolean;
  linkFlag?: boolean;
  recurringFlag?: boolean;
  taxableFlag?: boolean;
  recurringRevenue?: number;
  recurringCost?: number;
  cycles?: number;
  sequenceNumber?: number;
  customFields?: Array<
    Partial<Omit<CWCustomField, "connectWiseId" | "rowNum" | "podId">>
  >;
}

export interface CWProcurementProductCreate {
  opportunity?: { id: number };
  catalogItem: { id: number };
  description: string;
  customerDescription?: string;
  quantity?: number;
  price?: number;
  cost?: number;
  taxableFlag?: boolean;
  dropshipFlag?: boolean;
  billableOption?: string;
  customFields?: Array<
    Partial<Omit<CWCustomField, "connectWiseId" | "rowNum" | "podId">>
  >;
}

export interface CWProcurementProduct {
  id: number;
  forecastDetailId?: number;
  description?: string;
  customerDescription?: string;
  quantity?: number;
  price?: number;
  cost?: number;
  taxableFlag?: boolean;
  specialOrderFlag?: boolean;
  customFields?: CWCustomField[];
  _info?: Record<string, string>;
}

export interface CWOpportunityUpdate {
  name?: string;
  notes?: string;
  rating?: { id: number };
  type?: { id: number };
  stage?: { id: number };
  status?: { id: number };
  priority?: { id: number };
  probability?: { id: number };
  campaign?: { id: number };
  primarySalesRep?: { id: number };
  secondarySalesRep?: { id: number } | null;
  company?: { id: number };
  contact?: { id: number } | null;
  site?: { id: number } | null;
  expectedCloseDate?: string;
  customerPO?: string | null;
  source?: string | null;
  locationId?: number;
  businessUnitId?: number;
  customFields?: CWCustomField[];
}

export interface CWOpportunityCreate {
  name: string;
  expectedCloseDate: string;
  primarySalesRep: { id: number };
  company: { id: number };
  contact: { id: number };
  type?: { id: number };
  stage?: { id: number };
  status?: { id: number };
  priority?: { id: number };
  probability?: { id: number };
  campaign?: { id: number };
  secondarySalesRep?: { id: number } | null;
  site?: { id: number } | null;
  notes?: string;
  rating?: { id: number };
  source?: string | null;
  customerPO?: string | null;
  locationId?: number;
  businessUnitId?: number;
}

export interface CWOpportunitySummary {
  id: number;
  _info?: Record<string, string>;
}

export interface CWOpportunityProduct {
  id: number;
  catalogItem?: {
    id: number;
    identifier: string;
    _info?: Record<string, string>;
  };
  description: string;
  sequenceNumber: number;
  quantity: number;
  unitOfMeasure?: {
    id: number;
    name: string;
    _info?: Record<string, string>;
  };
  price: number;
  cost: number;
  extPrice: number;
  extCost: number;
  discount: number;
  margin: number;
  billableOption: string;
  locationId: number;
  location?: CWReference;
  businessUnitId: number;
  businessUnit?: CWReference;
  vendor?: {
    id: number;
    identifier: string;
    name: string;
    _info?: Record<string, string>;
  };
  vendorSku?: string;
  taxableFlag: boolean;
  dropshipFlag: boolean;
  specialOrderFlag: boolean;
  phaseProductFlag: boolean;
  cancelledFlag: boolean;
  quantityCancelled: number;
  customerDescription: string;
  productSuppliedFlag: boolean;
  subContractorAmountLimit: number;
  opportunity?: {
    id: number;
    name: string;
    _info?: Record<string, string>;
  };
  calculatedPriceFlag: boolean;
  calculatedCostFlag: boolean;
  forecastDetailId?: number;
  taxCode?: CWReference;
  listPrice?: number;
  company?: CWCompanyReference;
  forecastStatus?: CWReference;
  productClass: string;
  needToPurchaseFlag: boolean;
  minimumStockFlag: boolean;
  poApprovedFlag: boolean;
  uom?: string;
  customFields?: CWCustomField[];
  _info?: {
    lastUpdated: string;
    updatedBy: string;
  };
}
