export interface LLMLogEntry {
  id: string
  user_id: string
  namespace: string
  model: string
  provider: string
  prompt_tokens: number
  completion_tokens: number
  total_tokens: number
  pii_detected: boolean
  pii_categories: string[]
  redacted: boolean
  duration_ms: number
  status: string
  created_at: string
}

export interface UsageStats {
  total_requests: number
  total_tokens: number
  total_prompt_tokens: number
  total_completion_tokens: number
  models_used: Record<string, number>
  providers_used: Record<string, number>
  avg_duration_ms: number
  pii_detection_rate: number
  period_start: string
  period_end: string
}

export interface ComplianceReport {
  total_requests: number
  pii_incidents: number
  pii_rate: number
  redaction_rate: number
  policy_violations: number
  top_pii_categories: Record<string, number>
  namespace_breakdown: Record<string, { requests: number; pii_incidents: number }>
  user_breakdown: Record<string, { requests: number; pii_incidents: number }>
  period_start: string
  period_end: string
}

export type TabId = 'llm-log' | 'usage' | 'compliance'

export const API_BASE = '/api/sdk/v1/audit-llm'

export function formatDate(iso: string): string {
  return new Date(iso).toLocaleString('de-DE', {
    day: '2-digit', month: '2-digit', year: 'numeric',
    hour: '2-digit', minute: '2-digit',
  })
}

export function formatNumber(n: number): string {
  return n.toLocaleString('de-DE')
}

export function formatDuration(ms: number): string {
  if (ms < 1000) return `${ms}ms`
  return `${(ms / 1000).toFixed(1)}s`
}

export function getDateRange(period: string): { from: string; to: string } {
  const now = new Date()
  const to = now.toISOString().slice(0, 10)
  const from = new Date(now)
  switch (period) {
    case '7d': from.setDate(from.getDate() - 7); break
    case '30d': from.setDate(from.getDate() - 30); break
    case '90d': from.setDate(from.getDate() - 90); break
    default: from.setDate(from.getDate() - 7)
  }
  return { from: from.toISOString().slice(0, 10), to }
}
