/**
 * Shared types for Opportunity services
 */

import type { CancellationOpportunityData } from "@customer-portal/domain/opportunity";

/**
 * Raw Opportunity record from Salesforce query
 */
export interface SalesforceOpportunityRecord {
  Id: string;
  Name: string;
  AccountId: string;
  StageName: string;
  CloseDate: string;
  IsClosed: boolean;
  IsWon?: boolean;
  CreatedDate: string;
  LastModifiedDate: string;
  // Existing custom fields
  Application_Stage__c?: string;
  CommodityType?: string;
  // Internet cancellation fields
  ScheduledCancellationDateAndTime__c?: string;
  CancellationNotice__c?: string;
  LineReturn__c?: string;
  // SIM cancellation fields
  SIMCancellationNotice__c?: string;
  SIMScheduledCancellationDateAndTime__c?: string;
  // Portal integration custom fields
  Opportunity_Source__c?: string;
  WHMCS_Service_ID__c?: number;
  // Relationship fields
  Account?: { Name?: string };
}

export type InternetCancellationOpportunityDataInput = Pick<
  CancellationOpportunityData,
  "scheduledCancellationDate" | "cancellationNotice" | "lineReturnStatus"
>;

export type SimCancellationOpportunityDataInput = Pick<
  CancellationOpportunityData,
  "scheduledCancellationDate" | "cancellationNotice"
>;

export function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null;
}

export function requireStringField(record: Record<string, unknown>, field: string): string {
  const value = record[field];
  if (typeof value === "string") return value;
  throw new Error(`Invalid ${field}`);
}
