import { useI18n } from '../i18n/context';
import type { Locale } from '../i18n/types';
import type { RiskClass, Role } from '../engine/types';

/** Kurze UI-Labels für Risikoklassen je Sprache. */
export const RISK_UI: Readonly<Record<Locale, Readonly<Record<RiskClass, string>>>> = {
  de: {
    'out-of-scope': 'Nicht anwendbar',
    prohibited: 'Verboten',
    high: 'Hochrisiko',
    limited: 'Begrenztes Risiko',
    minimal: 'Minimales Risiko',
  },
  en: {
    'out-of-scope': 'Out of scope',
    prohibited: 'Prohibited',
    high: 'High-risk',
    limited: 'Limited risk',
    minimal: 'Minimal risk',
  },
  fr: {
    'out-of-scope': 'Hors champ',
    prohibited: 'Interdit',
    high: 'Haut risque',
    limited: 'Risque limité',
    minimal: 'Risque minimal',
  },
  it: {
    'out-of-scope': 'Fuori ambito',
    prohibited: 'Vietato',
    high: 'Alto rischio',
    limited: 'Rischio limitato',
    minimal: 'Rischio minimo',
  },
  es: {
    'out-of-scope': 'Fuera de ámbito',
    prohibited: 'Prohibido',
    high: 'Alto riesgo',
    limited: 'Riesgo limitado',
    minimal: 'Riesgo mínimo',
  },
  pl: {
    'out-of-scope': 'Poza zakresem',
    prohibited: 'Zakazane',
    high: 'Wysokie ryzyko',
    limited: 'Ograniczone ryzyko',
    minimal: 'Minimalne ryzyko',
  },
  cs: {
    'out-of-scope': 'Mimo působnost',
    prohibited: 'Zakázáno',
    high: 'Vysoké riziko',
    limited: 'Omezené riziko',
    minimal: 'Minimální riziko',
  },
  sk: {
    'out-of-scope': 'Mimo pôsobnosti',
    prohibited: 'Zakázané',
    high: 'Vysoké riziko',
    limited: 'Obmedzené riziko',
    minimal: 'Minimálne riziko',
  },
  ro: {
    'out-of-scope': 'În afara domeniului',
    prohibited: 'Interzis',
    high: 'Risc ridicat',
    limited: 'Risc limitat',
    minimal: 'Risc minim',
  },
};

/** Kurze UI-Labels für Rollen je Sprache. */
export const ROLE_UI: Readonly<Record<Locale, Readonly<Record<Role, string>>>> = {
  de: { provider: 'Anbieter', deployer: 'Betreiber', importer: 'Einführer', distributor: 'Händler' },
  en: { provider: 'Provider', deployer: 'Deployer', importer: 'Importer', distributor: 'Distributor' },
  fr: { provider: 'Fournisseur', deployer: 'Déployeur', importer: 'Importateur', distributor: 'Distributeur' },
  it: { provider: 'Fornitore', deployer: 'Deployer', importer: 'Importatore', distributor: 'Distributore' },
  es: { provider: 'Proveedor', deployer: 'Responsable del despliegue', importer: 'Importador', distributor: 'Distribuidor' },
  pl: { provider: 'Dostawca', deployer: 'Podmiot stosujący', importer: 'Importer', distributor: 'Dystrybutor' },
  cs: { provider: 'Poskytovatel', deployer: 'Zavádějící subjekt', importer: 'Dovozce', distributor: 'Distributor' },
  sk: { provider: 'Poskytovateľ', deployer: 'Nasadzujúci subjekt', importer: 'Dovozca', distributor: 'Distribútor' },
  ro: { provider: 'Furnizor', deployer: 'Implementator', importer: 'Importator', distributor: 'Distribuitor' },
};

/** Farbiges Stempel-Badge für die Risikoklasse eines Systems. */
export function RiskBadge({ risk }: { readonly risk: RiskClass }): JSX.Element {
  const { locale } = useI18n();
  return <span className={`badge ${risk}`}>{RISK_UI[locale][risk]}</span>;
}

/**
 * Löst einen Browser-Download für generierten Text aus.
 *
 * @param filename - Zieldateiname
 * @param content - Dateiinhalt
 * @param mime - MIME-Type (Default: Markdown)
 */
export function downloadText(filename: string, content: string, mime = 'text/markdown'): void {
  const blob = new Blob([content], { type: `${mime};charset=utf-8` });
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = filename;
  a.click();
  URL.revokeObjectURL(url);
}

/** Heutiges Datum im ISO-Format (YYYY-MM-DD). */
export function todayISO(): string {
  return new Date().toISOString().slice(0, 10);
}

/** Formatiert ein ISO-Datum für die UI (DE/PL/CS/SK/RO: TT.MM.JJJJ, FR/IT/ES: TT/MM/JJJJ, EN: ISO). */
export function uiDate(iso: string, locale: Locale): string {
  if (locale === 'en') return iso;
  const dotLocales: readonly Locale[] = ['de', 'pl', 'cs', 'sk', 'ro'];
  const sep = dotLocales.includes(locale) ? '.' : '/';
  return iso.split('-').reverse().join(sep);
}
