import type { ExecutionMetricsSummary, ResourceMetricsConfig } from './types'

/**
 * MetricsCollector
 * Tracks execution timing and ROI metrics
 */
export class MetricsCollector {
  private timings: Map<string, number> = new Map()
  private durationMs?: number

  /**
   * Start a timer with a label
   */
  startTimer(label: string): void {
    this.timings.set(label, Date.now())
  }

  /**
   * End a timer and calculate duration
   * If label is 'execution', stores duration for metrics summary
   */
  endTimer(label: string): number | null {
    const start = this.timings.get(label)
    if (!start) return null

    const duration = Date.now() - start

    if (label === 'execution') {
      this.durationMs = duration
    }

    this.timings.delete(label)
    return duration
  }

  /**
   * Build execution metrics summary with optional ROI calculation
   */
  buildExecutionMetrics(metricsConfig?: ResourceMetricsConfig): ExecutionMetricsSummary {
    const automationSavingsUsd = metricsConfig
      ? (metricsConfig.estimatedManualMinutes / 60) * metricsConfig.hourlyLaborRateUsd
      : undefined

    return {
      durationMs: this.durationMs,
      automationSavingsUsd
    }
  }
}
