/**
 * Contract Review Checklists
 *
 * DSGVO Art. 28 compliance checklists for contract reviews.
 * Checklist data arrays live in checklists-data.ts.
 */

import { LocalizedText, FindingCategory } from '../types'

// Re-export data arrays and item type
export type { ChecklistItem } from './checklists-data'
export {
  AVV_CHECKLIST,
  INCIDENT_CHECKLIST,
  TRANSFER_CHECKLIST,
  SLA_LIABILITY_CHECKLIST,
} from './checklists-data'

import type { ChecklistItem } from './checklists-data'
import {
  AVV_CHECKLIST,
  INCIDENT_CHECKLIST,
  TRANSFER_CHECKLIST,
  SLA_LIABILITY_CHECKLIST,
} from './checklists-data'

// ==========================================
// CHECKLIST GROUP TYPE
// ==========================================

export interface ChecklistGroup {
  id: string
  name: LocalizedText
  description: LocalizedText
  items: ChecklistItem[]
}

// ==========================================
// GROUPED CHECKLISTS
// ==========================================

export const CHECKLIST_GROUPS: ChecklistGroup[] = [
  {
    id: 'avv',
    name: { de: 'Art. 28 DSGVO - AVV Pflichtinhalte', en: 'Art. 28 GDPR - DPA Mandatory Content' },
    description: {
      de: 'Prüfung der Pflichtinhalte eines Auftragsverarbeitungsvertrags',
      en: 'Review of mandatory content of a Data Processing Agreement',
    },
    items: AVV_CHECKLIST,
  },
  {
    id: 'incident',
    name: { de: 'Incident Response', en: 'Incident Response' },
    description: {
      de: 'Prüfung der Regelungen zu Datenschutzverletzungen',
      en: 'Review of data breach provisions',
    },
    items: INCIDENT_CHECKLIST,
  },
  {
    id: 'transfer',
    name: { de: 'Drittlandtransfer', en: 'Third Country Transfer' },
    description: {
      de: 'Prüfung der Regelungen zu Drittlandtransfers',
      en: 'Review of third country transfer provisions',
    },
    items: TRANSFER_CHECKLIST,
  },
  {
    id: 'sla_liability',
    name: { de: 'SLA & Haftung', en: 'SLA & Liability' },
    description: {
      de: 'Prüfung von Service Levels und Haftungsregelungen',
      en: 'Review of service levels and liability provisions',
    },
    items: SLA_LIABILITY_CHECKLIST,
  },
]

// ==========================================
// HELPER FUNCTIONS
// ==========================================

/**
 * Get all required checklist items
 */
export function getRequiredChecklistItems(): ChecklistItem[] {
  return [
    ...AVV_CHECKLIST,
    ...INCIDENT_CHECKLIST,
    ...TRANSFER_CHECKLIST,
  ].filter((item) => item.isRequired)
}

/**
 * Get checklist items by category
 */
export function getChecklistItemsByCategory(category: FindingCategory): ChecklistItem[] {
  return [
    ...AVV_CHECKLIST,
    ...INCIDENT_CHECKLIST,
    ...TRANSFER_CHECKLIST,
    ...SLA_LIABILITY_CHECKLIST,
  ].filter((item) => item.category === category)
}

/**
 * Get checklist item by ID
 */
export function getChecklistItemById(id: string): ChecklistItem | undefined {
  return [
    ...AVV_CHECKLIST,
    ...INCIDENT_CHECKLIST,
    ...TRANSFER_CHECKLIST,
    ...SLA_LIABILITY_CHECKLIST,
  ].find((item) => item.id === id)
}

/**
 * Calculate compliance score based on checklist results
 */
export function calculateChecklistComplianceScore(
  results: Map<string, 'PASS' | 'PARTIAL' | 'FAIL' | 'NOT_CHECKED'>
): number {
  const allItems = [
    ...AVV_CHECKLIST,
    ...INCIDENT_CHECKLIST,
    ...TRANSFER_CHECKLIST,
  ]

  let totalWeight = 0
  let earnedScore = 0

  for (const item of allItems) {
    const weight = item.severity === 'CRITICAL' ? 3 : item.severity === 'HIGH' ? 2 : 1
    const result = results.get(item.id) || 'NOT_CHECKED'

    totalWeight += weight

    switch (result) {
      case 'PASS':
        earnedScore += weight
        break
      case 'PARTIAL':
        earnedScore += weight * 0.5
        break
      case 'FAIL':
      case 'NOT_CHECKED':
        // No score
        break
    }
  }

  return totalWeight > 0 ? Math.round((earnedScore / totalWeight) * 100) : 0
}
