/**
 * Twilio cost estimation utilities.
 * Rough estimates based on public Twilio pricing (US).
 */

export interface CostEstimate {
  perUnit: number;
  unit: string;
  description: string;
}

export const PRICING: Record<string, CostEstimate> = {
  sms_outbound_us: { perUnit: 0.0079, unit: 'message', description: 'US outbound SMS' },
  sms_inbound_us: { perUnit: 0.0075, unit: 'message', description: 'US inbound SMS' },
  mms_outbound_us: { perUnit: 0.02, unit: 'message', description: 'US outbound MMS' },
  mms_inbound_us: { perUnit: 0.01, unit: 'message', description: 'US inbound MMS' },
  whatsapp_outbound: { perUnit: 0.005, unit: 'message', description: 'WhatsApp outbound' },
  voice_outbound_us: { perUnit: 0.014, unit: 'minute', description: 'US outbound call' },
  voice_inbound_us: { perUnit: 0.0085, unit: 'minute', description: 'US inbound call' },
  phone_number_local: { perUnit: 1.15, unit: 'month', description: 'US local number' },
  phone_number_tollfree: { perUnit: 2.15, unit: 'month', description: 'US toll-free number' },
  verify_sms: { perUnit: 0.05, unit: 'verification', description: 'Verify SMS' },
  verify_voice: { perUnit: 0.05, unit: 'verification', description: 'Verify voice' },
  lookup_line_type: { perUnit: 0.005, unit: 'lookup', description: 'Line type intelligence' },
  lookup_caller_name: { perUnit: 0.01, unit: 'lookup', description: 'Caller name' },
  lookup_sim_swap: { perUnit: 0.04, unit: 'lookup', description: 'SIM swap check' },
  lookup_identity: { perUnit: 0.03, unit: 'lookup', description: 'Identity match' },
  recording_storage: { perUnit: 0.0025, unit: 'minute', description: 'Recording storage' },
  transcription: { perUnit: 0.05, unit: 'minute', description: 'Transcription' },
  studio_execution: { perUnit: 0.001, unit: 'execution', description: 'Studio flow execution' },
};

/** Estimate cost for a batch operation */
export function estimateBatchCost(
  type: keyof typeof PRICING,
  quantity: number
): { total: number; formatted: string; per_unit: string } {
  const pricing = PRICING[type];
  if (!pricing) return { total: 0, formatted: '$0.00', per_unit: 'unknown' };

  const total = pricing.perUnit * quantity;
  return {
    total,
    formatted: `$${total.toFixed(2)}`,
    per_unit: `$${pricing.perUnit}/${pricing.unit}`,
  };
}

/** Format a dollar amount */
export function formatCost(amount: number): string {
  if (amount < 0.01) return '< $0.01';
  return `$${amount.toFixed(2)}`;
}
