/**
 * Catalog Registry — DSFA & AI Act Catalogs
 */

import type { CatalogId, CatalogMeta } from '../types'
import { RISK_CATALOG } from '../../dsfa/risk-catalog'
import { MITIGATION_LIBRARY } from '../../dsfa/mitigation-library'
import { AI_RISK_CATALOG } from '../../dsfa/ai-risk-catalog'
import { AI_MITIGATION_LIBRARY } from '../../dsfa/ai-mitigation-library'
import { PROHIBITED_AI_PRACTICES } from '../../dsfa/prohibited-ai-practices'
import { WP248_CRITERIA, SDM_GOALS, DSFA_AUTHORITY_RESOURCES } from '../../dsfa/types'

export const SDM_GOALS_ENTRIES = Object.entries(SDM_GOALS).map(([key, val]) => ({
  id: key,
  name: val.name,
  description: val.description,
  article: val.article,
}))

export const DSFA_AI_CATALOG_META: Partial<Record<CatalogId, CatalogMeta>> = {
  'dsfa-risks': {
    id: 'dsfa-risks',
    name: 'DSFA Risikokatalog',
    description: 'Standardrisiken fuer Datenschutz-Folgenabschaetzungen',
    module: 'dsfa',
    icon: 'ShieldAlert',
    systemCount: RISK_CATALOG.length,
    allowCustom: true,
    idField: 'id',
    nameField: 'title',
    descriptionField: 'description',
    categoryField: 'category',
    fields: [
      { key: 'id', label: 'Risiko-ID', type: 'text', required: true, placeholder: 'R-XXX-01' },
      { key: 'title', label: 'Titel', type: 'text', required: true },
      { key: 'description', label: 'Beschreibung', type: 'textarea', required: true },
      { key: 'category', label: 'Kategorie', type: 'select', required: true, options: [
        { value: 'confidentiality', label: 'Vertraulichkeit' },
        { value: 'integrity', label: 'Integritaet' },
        { value: 'availability', label: 'Verfuegbarkeit' },
        { value: 'rights_freedoms', label: 'Rechte & Freiheiten' },
      ]},
      { key: 'typicalLikelihood', label: 'Typische Eintrittswahrscheinlichkeit', type: 'select', required: false, options: [
        { value: 'low', label: 'Niedrig' },
        { value: 'medium', label: 'Mittel' },
        { value: 'high', label: 'Hoch' },
      ]},
      { key: 'typicalImpact', label: 'Typische Auswirkung', type: 'select', required: false, options: [
        { value: 'low', label: 'Niedrig' },
        { value: 'medium', label: 'Mittel' },
        { value: 'high', label: 'Hoch' },
      ]},
    ],
    searchableFields: ['id', 'title', 'description', 'category'],
  },

  'dsfa-mitigations': {
    id: 'dsfa-mitigations',
    name: 'DSFA Massnahmenbibliothek',
    description: 'Technische und organisatorische Massnahmen fuer DSFAs',
    module: 'dsfa',
    icon: 'Shield',
    systemCount: MITIGATION_LIBRARY.length,
    allowCustom: true,
    idField: 'id',
    nameField: 'title',
    descriptionField: 'description',
    categoryField: 'type',
    fields: [
      { key: 'id', label: 'Massnahmen-ID', type: 'text', required: true, placeholder: 'M-XXX-01' },
      { key: 'title', label: 'Titel', type: 'text', required: true },
      { key: 'description', label: 'Beschreibung', type: 'textarea', required: true },
      { key: 'type', label: 'Typ', type: 'select', required: true, options: [
        { value: 'technical', label: 'Technisch' },
        { value: 'organizational', label: 'Organisatorisch' },
        { value: 'legal', label: 'Rechtlich' },
      ]},
      { key: 'effectiveness', label: 'Wirksamkeit', type: 'select', required: false, options: [
        { value: 'low', label: 'Niedrig' },
        { value: 'medium', label: 'Mittel' },
        { value: 'high', label: 'Hoch' },
      ]},
      { key: 'legalBasis', label: 'Rechtsgrundlage', type: 'text', required: false },
    ],
    searchableFields: ['id', 'title', 'description', 'type', 'legalBasis'],
  },

  'ai-risks': {
    id: 'ai-risks',
    name: 'KI-Risikokatalog',
    description: 'Spezifische Risiken fuer KI-Systeme',
    module: 'ai_act',
    icon: 'Bot',
    systemCount: AI_RISK_CATALOG.length,
    allowCustom: true,
    idField: 'id',
    nameField: 'title',
    descriptionField: 'description',
    categoryField: 'category',
    fields: [
      { key: 'id', label: 'Risiko-ID', type: 'text', required: true, placeholder: 'R-AI-XXX-01' },
      { key: 'title', label: 'Titel', type: 'text', required: true },
      { key: 'description', label: 'Beschreibung', type: 'textarea', required: true },
      { key: 'category', label: 'Kategorie', type: 'select', required: true, options: [
        { value: 'confidentiality', label: 'Vertraulichkeit' },
        { value: 'integrity', label: 'Integritaet' },
        { value: 'availability', label: 'Verfuegbarkeit' },
        { value: 'rights_freedoms', label: 'Rechte & Freiheiten' },
      ]},
      { key: 'typicalLikelihood', label: 'Eintrittswahrscheinlichkeit', type: 'select', required: false, options: [
        { value: 'low', label: 'Niedrig' },
        { value: 'medium', label: 'Mittel' },
        { value: 'high', label: 'Hoch' },
      ]},
      { key: 'typicalImpact', label: 'Auswirkung', type: 'select', required: false, options: [
        { value: 'low', label: 'Niedrig' },
        { value: 'medium', label: 'Mittel' },
        { value: 'high', label: 'Hoch' },
      ]},
    ],
    searchableFields: ['id', 'title', 'description', 'category'],
  },

  'ai-mitigations': {
    id: 'ai-mitigations',
    name: 'KI-Massnahmenbibliothek',
    description: 'Massnahmen fuer KI-spezifische Risiken',
    module: 'ai_act',
    icon: 'ShieldCheck',
    systemCount: AI_MITIGATION_LIBRARY.length,
    allowCustom: true,
    idField: 'id',
    nameField: 'title',
    descriptionField: 'description',
    categoryField: 'type',
    fields: [
      { key: 'id', label: 'Massnahmen-ID', type: 'text', required: true },
      { key: 'title', label: 'Titel', type: 'text', required: true },
      { key: 'description', label: 'Beschreibung', type: 'textarea', required: true },
      { key: 'type', label: 'Typ', type: 'select', required: true, options: [
        { value: 'technical', label: 'Technisch' },
        { value: 'organizational', label: 'Organisatorisch' },
        { value: 'legal', label: 'Rechtlich' },
      ]},
      { key: 'effectiveness', label: 'Wirksamkeit', type: 'select', required: false, options: [
        { value: 'low', label: 'Niedrig' },
        { value: 'medium', label: 'Mittel' },
        { value: 'high', label: 'Hoch' },
      ]},
    ],
    searchableFields: ['id', 'title', 'description', 'type'],
  },

  'prohibited-ai-practices': {
    id: 'prohibited-ai-practices',
    name: 'Verbotene KI-Praktiken',
    description: 'Absolut und bedingt verbotene KI-Anwendungen nach AI Act',
    module: 'ai_act',
    icon: 'Ban',
    systemCount: PROHIBITED_AI_PRACTICES.length,
    allowCustom: false,
    idField: 'id',
    nameField: 'title',
    descriptionField: 'description',
    categoryField: 'severity',
    fields: [
      { key: 'id', label: 'ID', type: 'text', required: true },
      { key: 'title', label: 'Titel', type: 'text', required: true },
      { key: 'description', label: 'Beschreibung', type: 'textarea', required: true },
      { key: 'severity', label: 'Schwere', type: 'select', required: true, options: [
        { value: 'absolute', label: 'Absolutes Verbot' },
        { value: 'conditional', label: 'Bedingtes Verbot' },
      ]},
      { key: 'legalBasis', label: 'Rechtsgrundlage', type: 'text', required: false },
    ],
    searchableFields: ['id', 'title', 'description', 'severity', 'legalBasis'],
  },

  'wp248-criteria': {
    id: 'wp248-criteria',
    name: 'WP248 Kriterien',
    description: 'Kriterien zur DSFA-Pflichtpruefung nach WP248',
    module: 'dsfa',
    icon: 'ClipboardCheck',
    systemCount: WP248_CRITERIA.length,
    allowCustom: false,
    idField: 'code',
    nameField: 'title',
    descriptionField: 'description',
    fields: [
      { key: 'code', label: 'Code', type: 'text', required: true },
      { key: 'title', label: 'Titel', type: 'text', required: true },
      { key: 'description', label: 'Beschreibung', type: 'textarea', required: true },
      { key: 'gdprRef', label: 'DSGVO-Referenz', type: 'text', required: false },
    ],
    searchableFields: ['code', 'title', 'description', 'gdprRef'],
  },

  'sdm-goals': {
    id: 'sdm-goals',
    name: 'SDM Gewaehrleistungsziele',
    description: 'Standard-Datenschutzmodell Gewaehrleistungsziele',
    module: 'dsfa',
    icon: 'Target',
    systemCount: SDM_GOALS_ENTRIES.length,
    allowCustom: false,
    idField: 'id',
    nameField: 'name',
    descriptionField: 'description',
    fields: [
      { key: 'id', label: 'ID', type: 'text', required: true },
      { key: 'name', label: 'Name', type: 'text', required: true },
      { key: 'description', label: 'Beschreibung', type: 'textarea', required: true },
      { key: 'article', label: 'DSGVO-Artikel', type: 'text', required: false },
    ],
    searchableFields: ['id', 'name', 'description', 'article'],
  },

  'dsfa-authority-resources': {
    id: 'dsfa-authority-resources',
    name: 'Aufsichtsbehoerden-Ressourcen',
    description: 'DSFA-Ressourcen der deutschen Aufsichtsbehoerden',
    module: 'dsfa',
    icon: 'Building',
    systemCount: DSFA_AUTHORITY_RESOURCES.length,
    allowCustom: false,
    idField: 'id',
    nameField: 'shortName',
    descriptionField: 'name',
    categoryField: 'state',
    fields: [
      { key: 'id', label: 'ID', type: 'text', required: true },
      { key: 'shortName', label: 'Kurzname', type: 'text', required: true },
      { key: 'name', label: 'Voller Name', type: 'text', required: true },
      { key: 'state', label: 'Bundesland', type: 'text', required: true },
    ],
    searchableFields: ['id', 'shortName', 'name', 'state'],
  },
}

export const DSFA_AI_SYSTEM_ENTRIES: Partial<Record<CatalogId, Record<string, unknown>[]>> = {
  'dsfa-risks': RISK_CATALOG as unknown as Record<string, unknown>[],
  'dsfa-mitigations': MITIGATION_LIBRARY as unknown as Record<string, unknown>[],
  'ai-risks': AI_RISK_CATALOG as unknown as Record<string, unknown>[],
  'ai-mitigations': AI_MITIGATION_LIBRARY as unknown as Record<string, unknown>[],
  'prohibited-ai-practices': PROHIBITED_AI_PRACTICES as unknown as Record<string, unknown>[],
  'wp248-criteria': WP248_CRITERIA as unknown as Record<string, unknown>[],
  'sdm-goals': SDM_GOALS_ENTRIES as unknown as Record<string, unknown>[],
  'dsfa-authority-resources': DSFA_AUTHORITY_RESOURCES as unknown as Record<string, unknown>[],
}
