/**
 * Badge per il Livello di Rischio e la Riservatezza (Feature N3).
 *
 * RiskLevelBadge   – mostra il livello di rischio operativo
 * ConfidentialityBadge – mostra il grado di riservatezza
 */

import { ShieldAlert, Lock } from 'lucide-react'

// ─── Configurazione colori e label ───────────────────────────────────────────

const RISK_CONFIG: Record<string, { label: string; bgClass: string; textClass: string; borderClass: string }> = {
  low: {
    label: 'Basso',
    bgClass: 'bg-green-50',
    textClass: 'text-green-700',
    borderClass: 'border-green-300',
  },
  medium: {
    label: 'Medio',
    bgClass: 'bg-yellow-50',
    textClass: 'text-yellow-700',
    borderClass: 'border-yellow-300',
  },
  high: {
    label: 'Alto',
    bgClass: 'bg-orange-50',
    textClass: 'text-orange-700',
    borderClass: 'border-orange-300',
  },
  critical: {
    label: 'Critico',
    bgClass: 'bg-red-50',
    textClass: 'text-red-700',
    borderClass: 'border-red-300',
  },
}

const CONFIDENTIALITY_CONFIG: Record<string, { label: string; bgClass: string; textClass: string; borderClass: string }> = {
  public: {
    label: 'Pubblico',
    bgClass: 'bg-gray-50',
    textClass: 'text-gray-600',
    borderClass: 'border-gray-300',
  },
  internal: {
    label: 'Interno',
    bgClass: 'bg-blue-50',
    textClass: 'text-blue-700',
    borderClass: 'border-blue-300',
  },
  confidential: {
    label: 'Riservato',
    bgClass: 'bg-purple-50',
    textClass: 'text-purple-700',
    borderClass: 'border-purple-300',
  },
  secret: {
    label: 'Segreto',
    bgClass: 'bg-red-100',
    textClass: 'text-red-800',
    borderClass: 'border-red-400',
  },
}

// ─── Componenti ───────────────────────────────────────────────────────────────

interface RiskLevelBadgeProps {
  level: string
  /** Se true mostra solo il badge piccolo senza testo (per la lista messaggi) */
  compact?: boolean
}

export function RiskLevelBadge({ level, compact = false }: RiskLevelBadgeProps) {
  const config = RISK_CONFIG[level]
  if (!config) return null

  if (compact) {
    return (
      <span
        title={`Rischio: ${config.label}`}
        className={`inline-flex items-center gap-1 px-1.5 py-0.5 rounded text-xs font-medium border ${config.bgClass} ${config.textClass} ${config.borderClass}`}
      >
        <ShieldAlert className="h-3 w-3" />
        {config.label}
      </span>
    )
  }

  return (
    <span
      className={`inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-semibold border ${config.bgClass} ${config.textClass} ${config.borderClass}`}
    >
      <ShieldAlert className="h-3.5 w-3.5" />
      Rischio: {config.label}
    </span>
  )
}

interface ConfidentialityBadgeProps {
  level: string
  compact?: boolean
}

export function ConfidentialityBadge({ level, compact = false }: ConfidentialityBadgeProps) {
  const config = CONFIDENTIALITY_CONFIG[level]
  if (!config) return null

  if (compact) {
    return (
      <span
        title={`Riservatezza: ${config.label}`}
        className={`inline-flex items-center gap-1 px-1.5 py-0.5 rounded text-xs font-medium border ${config.bgClass} ${config.textClass} ${config.borderClass}`}
      >
        <Lock className="h-3 w-3" />
        {config.label}
      </span>
    )
  }

  return (
    <span
      className={`inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-semibold border ${config.bgClass} ${config.textClass} ${config.borderClass}`}
    >
      <Lock className="h-3.5 w-3.5" />
      {config.label}
    </span>
  )
}

// ─── Costanti esportate per i form ────────────────────────────────────────────

export const RISK_LEVEL_OPTIONS = [
  { value: 'low', label: 'Basso' },
  { value: 'medium', label: 'Medio' },
  { value: 'high', label: 'Alto' },
  { value: 'critical', label: 'Critico' },
]

export const CONFIDENTIALITY_OPTIONS = [
  { value: 'public', label: 'Pubblico' },
  { value: 'internal', label: 'Interno' },
  { value: 'confidential', label: 'Riservato' },
  { value: 'secret', label: 'Segreto' },
]
