// =============================================================================
// TYPES
// =============================================================================

export interface OpenAnchor {
  framework: string
  ref: string
  url: string
}

export interface EvidenceItem {
  type: string
  description: string
}

export interface CanonicalControl {
  id: string
  framework_id: string
  control_id: string
  title: string
  objective: string
  rationale: string
  scope: {
    platforms?: string[]
    components?: string[]
    data_classes?: string[]
  }
  requirements: string[]
  test_procedure: string[]
  evidence: EvidenceItem[]
  severity: string
  risk_score: number | null
  implementation_effort: string | null
  evidence_confidence: number | null
  open_anchors: OpenAnchor[]
  release_state: string
  tags: string[]
  license_rule?: number | null
  source_original_text?: string | null
  source_citation?: Record<string, string> | null
  customer_visible?: boolean
  generation_metadata?: Record<string, unknown> | null
  created_at: string
  updated_at: string
}

export interface Framework {
  id: string
  framework_id: string
  name: string
  version: string
  description: string
  release_state: string
}

export const EFFORT_LABELS: Record<string, string> = {
  s: 'Klein (S)',
  m: 'Mittel (M)',
  l: 'Gross (L)',
  xl: 'Sehr gross (XL)',
}

export const BACKEND_URL = '/api/sdk/v1/canonical'

export const EMPTY_CONTROL = {
  framework_id: 'bp_security_v1',
  control_id: '',
  title: '',
  objective: '',
  rationale: '',
  scope: { platforms: [] as string[], components: [] as string[], data_classes: [] as string[] },
  requirements: [''],
  test_procedure: [''],
  evidence: [{ type: '', description: '' }],
  severity: 'medium',
  risk_score: null as number | null,
  implementation_effort: 'm' as string | null,
  open_anchors: [{ framework: '', ref: '', url: '' }],
  release_state: 'draft',
  tags: [] as string[],
}

export type ControlFormData = typeof EMPTY_CONTROL

export function getDomain(controlId: string): string {
  return controlId.split('-')[0] || ''
}
