import type { UserChecklistProgress, Vulnerability, MonitoredPlugin } from '@cra-compliance/types'; interface ScoreInput { checklistProgress: UserChecklistProgress[]; totalChecklistItems: number; vulnerabilities: Vulnerability[]; plugins: MonitoredPlugin[]; hasSecurityPolicy: boolean; hasIncidentResponsePlan: boolean; hasSBOM: boolean; } export function calculateComplianceScore(input: ScoreInput): number { const weights = { checklist: 30, vulnerabilities: 25, documentation: 25, monitoring: 20, }; // Checklist completion (30%) const completedItems = input.checklistProgress.filter(p => p.completed).length; const checklistScore = input.totalChecklistItems > 0 ? (completedItems / input.totalChecklistItems) * weights.checklist : 0; // Vulnerability management (25%) - penalize open vulnerabilities const openVulns = input.vulnerabilities.filter( v => v.status === 'open' || v.status === 'acknowledged' ); const criticalOpen = openVulns.filter(v => v.severity === 'critical').length; const highOpen = openVulns.filter(v => v.severity === 'high').length; const penalty = Math.min(criticalOpen * 10 + highOpen * 5, weights.vulnerabilities); const vulnScore = Math.max(0, weights.vulnerabilities - penalty); // Documentation (25%) let docScore = 0; if (input.hasSecurityPolicy) docScore += weights.documentation * 0.4; if (input.hasIncidentResponsePlan) docScore += weights.documentation * 0.4; if (input.hasSBOM) docScore += weights.documentation * 0.2; // Active monitoring (20%) const monitoredPlugins = input.plugins.filter(p => p.last_scanned_at !== null); const monitoringScore = input.plugins.length > 0 ? (monitoredPlugins.length / input.plugins.length) * weights.monitoring : 0; return Math.round(checklistScore + vulnScore + docScore + monitoringScore); }