// =============================================================================
// MAIN DSFA TYPE
// =============================================================================

import type { AIUseCaseModule } from '../ai-use-case-types'
export type { AIUseCaseModule } from '../ai-use-case-types'

import type { DSFAStatus, DSFARiskLevel } from './enums-constants'
import type {
  DSFARisk,
  DSFAMitigation,
  DSFAReviewComment,
  DSFASectionProgress,
  DSFAThresholdAnalysis,
  DSFAStakeholderConsultation,
  DSFAConsultationRequirement,
  DSFAReviewSchedule,
  DSFAReviewTrigger,
} from './sub-types'

export interface DSFA {
  id: string
  tenant_id: string
  namespace_id?: string
  processing_activity_id?: string
  assessment_id?: string
  name: string
  description: string

  // Section 0: Schwellwertanalyse / Vorabprüfung (NEU - Art. 35 Abs. 1)
  threshold_analysis?: DSFAThresholdAnalysis
  wp248_criteria_met?: string[]  // IDs der erfüllten WP248-Kriterien (K1-K9)
  art35_abs3_triggered?: string[]  // IDs der ausgelösten Art. 35 Abs. 3 Fälle

  // Section 1: Systematische Beschreibung (Art. 35 Abs. 7 lit. a)
  processing_description: string
  processing_purpose: string
  data_categories: string[]
  data_subjects: string[]
  recipients: string[]
  legal_basis: string
  legal_basis_details?: string

  // Section 2: Notwendigkeit & Verhältnismäßigkeit (Art. 35 Abs. 7 lit. b)
  necessity_assessment: string
  proportionality_assessment: string
  data_minimization?: string
  alternatives_considered?: string
  retention_justification?: string

  // Section 3: Risikobewertung (Art. 35 Abs. 7 lit. c)
  risks: DSFARisk[]
  overall_risk_level: DSFARiskLevel
  risk_score: number
  affected_rights?: string[]
  triggered_rule_codes?: string[]

  // KI-spezifische Trigger (NEU)
  involves_ai?: boolean
  ai_trigger_ids?: string[]  // IDs der ausgelösten KI-Trigger

  // Section 8: KI-Anwendungsfälle (NEU)
  ai_use_case_modules?: AIUseCaseModule[]

  // Section 4: Abhilfemaßnahmen (Art. 35 Abs. 7 lit. d)
  mitigations: DSFAMitigation[]
  tom_references?: string[]
  residual_risk_level?: DSFARiskLevel  // Restrisiko nach Maßnahmen

  // Section 5: Stellungnahme DSB (Art. 35 Abs. 2 + Art. 36)
  dpo_consulted: boolean
  dpo_consulted_at?: string
  dpo_name?: string
  dpo_opinion?: string
  dpo_approved?: boolean
  authority_consulted: boolean
  authority_consulted_at?: string
  authority_reference?: string
  authority_decision?: string

  // Art. 36 Konsultationspflicht (NEU)
  consultation_requirement?: DSFAConsultationRequirement

  // Betroffenenperspektive (NEU - Art. 35 Abs. 9)
  stakeholder_consultations?: DSFAStakeholderConsultation[]
  stakeholder_consultation_not_appropriate?: boolean
  stakeholder_consultation_not_appropriate_reason?: string

  // Workflow & Approval
  status: DSFAStatus
  submitted_for_review_at?: string
  submitted_by?: string
  conclusion: string
  review_comments?: DSFAReviewComment[]

  // Section Progress Tracking
  section_progress: DSFASectionProgress

  // Fortschreibung / Review (NEU - Art. 35 Abs. 11)
  review_schedule?: DSFAReviewSchedule
  review_triggers?: DSFAReviewTrigger[]
  version: number  // DSFA-Version für Fortschreibung
  previous_version_id?: string

  // Referenzen zu behördlichen Ressourcen
  federal_state?: string  // Bundesland für zuständige Aufsichtsbehörde
  authority_resource_id?: string  // ID aus DSFA_AUTHORITY_RESOURCES

  // Metadata & Audit
  metadata?: Record<string, unknown>
  created_at: string
  updated_at: string
  created_by: string
  approved_by?: string
  approved_at?: string
}
