'use client'

import React from 'react'
import type { AnalysisResult } from '../_hooks/useAgentAnalysis'

const DOC_TYPE_LABELS: Record<string, string> = {
  privacy_policy: 'DSE',
  cookie_banner: 'Cookie',
  terms_of_service: 'AGB',
  imprint: 'Impressum',
  dpa: 'AVV',
  other: 'Sonstig',
}

const RISK_DOT: Record<string, string> = {
  low: 'bg-green-500',
  medium: 'bg-yellow-500',
  high: 'bg-orange-500',
  critical: 'bg-red-500',
}

interface Props {
  history: AnalysisResult[]
  onSelect: (result: AnalysisResult) => void
}

export function AnalysisHistory({ history, onSelect }: Props) {
  if (history.length === 0) return null

  return (
    <div>
      <h3 className="text-sm font-medium text-gray-700 mb-3">Letzte Analysen</h3>
      <div className="space-y-2">
        {history.map((item, i) => (
          <button
            key={i}
            onClick={() => onSelect(item)}
            className="w-full text-left p-3 bg-white border border-gray-200 rounded-lg hover:border-purple-300 hover:bg-purple-50 transition-colors"
          >
            <div className="flex items-center gap-3">
              <span className={`w-2.5 h-2.5 rounded-full ${RISK_DOT[item.risk_level] || 'bg-gray-400'}`} />
              <span className="text-xs font-medium text-gray-500 w-16">
                {DOC_TYPE_LABELS[item.classification] || item.classification}
              </span>
              <span className="text-sm text-gray-700 truncate flex-1">
                {new URL(item.url).hostname}
              </span>
              <span className="text-xs text-gray-400">
                {new Date(item.analyzed_at).toLocaleTimeString('de-DE', { hour: '2-digit', minute: '2-digit' })}
              </span>
            </div>
          </button>
        ))}
      </div>
    </div>
  )
}
