import type { TraceEvent } from '../../../types/trace'

export interface QueueAnalysis {
  reason: 'connection_limit' | 'priority_queue' | 'resource_contention' | 'unknown'
  description: string
  concurrentRequests: number
  connectionId?: number
  relatedRequests?: string[]
}

export interface CDNAnalysis {
  provider: 'cloudflare' | 'akamai' | 'aws_cloudfront' | 'fastly' | 'azure_cdn' | 'google_cdn' | 'cdn77' | 'keycdn' | 'unknown'
  isEdge: boolean
  cacheStatus?: 'hit' | 'miss' | 'expired' | 'bypass' | 'unknown'
  edgeLocation?: string
  confidence: number // 0-1 confidence score
  detectionMethod: string // How we detected it
}

export interface ScreenshotEvent {
  timestamp: number
  screenshot: string // base64 image data
  index: number
}

export interface CallFrame {
  scriptId: string
  url: string
  lineNumber: number
  columnNumber: number
  functionName: string
}

export interface InitiatorStack {
  callFrames: CallFrame[]
}

export interface RequestInitiator {
  type: 'parser' | 'script' | 'preload' | 'other'
  fetchType?: 'script' | 'link' | 'fetch' | 'xhr' | 'img' | 'other'
  url?: string
  lineNumber?: number
  columnNumber?: number
  stack?: InitiatorStack
}

export interface HTTPRequest {
  requestId: string
  url: string
  hostname: string
  method: string
  resourceType: string
  priority: string
  connectionId?: number
  connectionNumber?: number
  requestNumberOnConnection?: number
  statusCode?: number
  mimeType?: string
  protocol?: string
  events: {
    willSendRequest?: TraceEvent
    sendRequest?: TraceEvent
    receiveResponse?: TraceEvent
    receivedData?: TraceEvent[]
    finishLoading?: TraceEvent
  }
  timing: {
    start: number
    startOffset?: number
    end?: number
    duration?: number
    totalResponseTime?: number
    downloadTime?: number
    queueTime?: number
    serverLatency?: number
    networkDuration?: number
    dnsStart?: number
    dnsEnd?: number
    connectStart?: number
    connectEnd?: number
    sslStart?: number
    sslEnd?: number
    sendStart?: number
    sendEnd?: number
    receiveHeadersStart?: number
    receiveHeadersEnd?: number
  }
  responseHeaders?: Array<{ name: string, value: string }>
  encodedDataLength?: number
  contentLength?: number
  fromCache: boolean
  connectionReused: boolean
  queueAnalysis?: QueueAnalysis
  cdnAnalysis?: CDNAnalysis
  initiator?: RequestInitiator
}