/**
 * Types fuer den Template-Rule-Editor (SDK).
 *
 * Spiegeln die Pydantic-Modelle aus
 * backend-compliance/compliance/schemas/template_rule.py.
 */

export type Classification = 'required' | 'recommended' | 'optional'

export type RuleStatus =
  | 'draft' | 'review' | 'approved' | 'published' | 'archived' | 'rejected'

export type ClauseOperator =
  | 'eq' | 'neq' | 'in' | 'not_in'
  | 'gt' | 'gte' | 'lt' | 'lte'
  | 'exists' | 'truthy' | 'falsy'

export interface RuleClause {
  field: string
  op: ClauseOperator
  value?: unknown
}

export interface RuleCondition {
  kind: 'all' | 'any'
  clauses: RuleClause[]
}

export interface Rule {
  id: string
  rule_key: string
  document_type: string
  title: string
  current_version_id: string | null
  created_at: string
  updated_at: string | null
}

export interface RuleVersion {
  id: string
  rule_id: string
  version_number: number
  status: RuleStatus
  is_live: boolean
  classification: Classification
  conditions: RuleCondition
  source_citation: string
  rationale: string | null
  change_summary: string | null
  created_by: string | null
  submitted_by: string | null
  submitted_at: string | null
  approved_by: string | null
  approved_at: string | null
  published_by: string | null
  published_at: string | null
  rejected_by: string | null
  rejected_at: string | null
  rejection_reason: string | null
  created_at: string
  updated_at: string | null
}

export interface ApprovalHistoryEntry {
  id: string
  version_id: string
  action: string
  approver: string | null
  comment: string | null
  created_at: string
}

export interface TenantRuleOverride {
  id: string
  tenant_id: string
  rule_id: string
  override_classification: Classification | null
  reason: string
  created_by: string | null
  created_at: string
  updated_at: string | null
}

// ---- Profil-Felder fuer Condition-Builder ----

export interface ProfileFieldOption {
  /** Key der im Profil verwendet wird */
  key: string
  /** Label fuer die UI */
  label: string
  /** Kategorie fuer Gruppierung */
  category: 'org' | 'proc' | 'prod' | 'comp' | 'tech' | 'compliance'
  /** Erwarteter Datentyp */
  type: 'string' | 'number' | 'boolean' | 'enum'
  /** Wenn enum: Mögliche Werte mit Label */
  options?: { value: string; label: string }[]
}

/**
 * Die 17 Profil-Felder, die in den 33 Initial-Regeln verwendet werden.
 * Aus templateRecommendations.ts portiert + compliance_depth_level ergaenzt.
 */
export const PROFILE_FIELDS: ProfileFieldOption[] = [
  {
    key: 'compliance_depth_level',
    label: 'Compliance-Tiefe',
    category: 'compliance', type: 'enum',
    options: [
      { value: 'L1', label: 'L1 — Lean Startup' },
      { value: 'L2', label: 'L2 — Standard' },
      { value: 'L3', label: 'L3 — Strict' },
      { value: 'L4', label: 'L4 — Zertifizierungsbereit' },
    ],
  },
  {
    key: 'org_employee_count',
    label: 'Mitarbeiterzahl',
    category: 'org', type: 'enum',
    options: [
      { value: 'none', label: 'Keine' },
      { value: '1_9', label: '1–9' },
      { value: '10_49', label: '10–49' },
      { value: '50_249', label: '50–249' },
      { value: '250_999', label: '250–999' },
      { value: '1000_plus', label: '1000+' },
    ],
  },
  {
    key: 'org_has_employees', label: 'Hat Mitarbeiter', category: 'org', type: 'enum',
    options: [{ value: 'yes', label: 'Ja' }, { value: 'no', label: 'Nein' }],
  },
  {
    key: 'org_business_model', label: 'Geschäftsmodell', category: 'org', type: 'enum',
    options: [
      { value: 'b2b_saas', label: 'B2B SaaS' },
      { value: 'b2c_shop', label: 'B2C Shop' },
      { value: 'platform', label: 'Plattform' },
      { value: 'marketplace', label: 'Marktplatz' },
      { value: 'social', label: 'Social Media' },
      { value: 'saas', label: 'SaaS' },
      { value: 'media', label: 'Media' },
      { value: 'manufacturing', label: 'Maschinenbau' },
      { value: 'other', label: 'Sonstiges' },
    ],
  },
  {
    key: 'org_has_social_media', label: 'Hat Social Media', category: 'org', type: 'enum',
    options: [{ value: 'yes', label: 'Ja' }, { value: 'no', label: 'Nein' }],
  },
  {
    key: 'org_has_video_conferencing', label: 'Hat Video-Konferenzen', category: 'org', type: 'enum',
    options: [{ value: 'yes', label: 'Ja' }, { value: 'no', label: 'Nein' }],
  },
  {
    key: 'org_cert_target', label: 'Zertifizierungsziel', category: 'org', type: 'enum',
    options: [
      { value: 'none', label: 'Keines' },
      { value: 'iso27001', label: 'ISO 27001' },
      { value: 'iso27701', label: 'ISO 27701' },
      { value: 'tisax', label: 'TISAX' },
    ],
  },
  {
    key: 'proc_ai_usage', label: 'KI-Nutzung', category: 'proc', type: 'enum',
    options: [
      { value: 'none', label: 'Keine' },
      { value: 'limited', label: 'Begrenzt' },
      { value: 'extensive', label: 'Umfangreich' },
    ],
  },
  {
    key: 'proc_uses_ai_tools', label: 'Nutzt KI-Tools', category: 'proc', type: 'boolean',
  },
  {
    key: 'proc_byod_allowed', label: 'BYOD erlaubt', category: 'proc', type: 'enum',
    options: [{ value: 'yes', label: 'Ja' }, { value: 'no', label: 'Nein' }],
  },
  {
    key: 'proc_dsfa_required', label: 'DSFA erforderlich', category: 'proc', type: 'enum',
    options: [{ value: 'yes', label: 'Ja' }, { value: 'no', label: 'Nein' }],
  },
  {
    key: 'prod_webshop', label: 'Webshop', category: 'prod', type: 'enum',
    options: [{ value: 'yes', label: 'Ja' }, { value: 'no', label: 'Nein' }],
  },
  {
    key: 'prod_ugc_platform', label: 'UGC-Plattform', category: 'prod', type: 'enum',
    options: [{ value: 'yes', label: 'Ja' }, { value: 'no', label: 'Nein' }],
  },
  {
    key: 'prod_consent_management', label: 'Consent Management', category: 'prod', type: 'enum',
    options: [{ value: 'yes', label: 'Ja' }, { value: 'no', label: 'Nein' }],
  },
  {
    key: 'comp_has_processors', label: 'Auftragsverarbeiter', category: 'comp', type: 'enum',
    options: [{ value: 'yes', label: 'Ja' }, { value: 'no', label: 'Nein' }],
  },
  {
    key: 'comp_vendor_management', label: 'Vendor-Management', category: 'comp', type: 'enum',
    options: [{ value: 'yes', label: 'Ja' }, { value: 'no', label: 'Nein' }],
  },
  {
    key: 'comp_dsfa_processes', label: 'DSFA-Prozesse', category: 'comp', type: 'enum',
    options: [{ value: 'required', label: 'Erforderlich' }, { value: 'optional', label: 'Optional' }],
  },
  {
    key: 'tech_third_country', label: 'Drittland-Transfer', category: 'tech', type: 'enum',
    options: [
      { value: 'no', label: 'Nein' },
      { value: 'us_dpf_only', label: 'Nur US-DPF' },
      { value: 'adequate_only', label: 'Nur Angemessenheitsbeschluss' },
      { value: 'yes_us', label: 'Ja, USA' },
      { value: 'yes_other', label: 'Ja, Sonstige' },
    ],
  },
]


export const OPERATOR_LABELS: Record<ClauseOperator, string> = {
  eq: 'gleich (=)',
  neq: 'ungleich (≠)',
  in: 'in Liste',
  not_in: 'nicht in Liste',
  gt: 'größer (>)',
  gte: 'größer/gleich (≥)',
  lt: 'kleiner (<)',
  lte: 'kleiner/gleich (≤)',
  exists: 'existiert',
  truthy: 'ist gesetzt',
  falsy: 'ist leer',
}

export const CLASSIFICATION_LABELS: Record<Classification, string> = {
  required: 'Pflicht',
  recommended: 'Empfohlen',
  optional: 'Optional',
}

export const STATUS_LABELS: Record<RuleStatus, string> = {
  draft: 'Entwurf',
  review: 'In Prüfung',
  approved: 'Freigegeben',
  published: 'Live',
  archived: 'Archiviert',
  rejected: 'Abgelehnt',
}
