/**
 * Sage Analysis Utility
 *
 * Generates contextual narrative strings for Sage insight cards based on
 * a numeric score and domain-specific thresholds. Consolidates the repeated
 * getSageAnalysis() pattern found across DetectionInsightsCard,
 * ValidationInsightsCard, and VisibilityInsightsCard in tenant-ui.
 *
 * Usage:
 *   const thresholds: SageAnalysisThresholds = {
 *     excellent: 'Strong detection posture...',
 *     good:      'Moderate coverage with room to improve...',
 *     moderate:  'Gaps exist across several techniques...',
 *     poor:      'Significant exposure requires immediate action...',
 *     critical:  'Critical gaps demand urgent remediation...',
 *   }
 *   const narrative = generateSageAnalysis(score, thresholds, hasCritical)
 */

/** Threshold-based narrative messages for Sage analysis. */
export interface SageAnalysisThresholds {
  /** Narrative when score >= 80 */
  excellent: string
  /** Narrative when score >= 60 */
  good: string
  /** Narrative when score >= 40 */
  moderate: string
  /** Narrative when score < 40 */
  poor: string
  /** Override narrative when critical issues are detected */
  critical?: string
}

/**
 * Generate a Sage analysis narrative string from a numeric score and
 * threshold-based messages.
 *
 * When `hasCriticalIssues` is true and a `critical` threshold message is
 * provided, the critical message takes precedence over score-based tiers.
 *
 * Score tiers:
 * - 80+  -> excellent
 * - 60+  -> good
 * - 40+  -> moderate
 * - <40  -> poor
 *
 * @param score - Numeric score (0-100)
 * @param thresholds - Tier-based narrative messages
 * @param hasCriticalIssues - Whether critical issues override the score tier
 * @returns The narrative string for the matching tier
 */
export function generateSageAnalysis(
  score: number,
  thresholds: SageAnalysisThresholds,
  hasCriticalIssues?: boolean
): string {
  if (hasCriticalIssues && thresholds.critical) return thresholds.critical
  if (score >= 80) return thresholds.excellent
  if (score >= 60) return thresholds.good
  if (score >= 40) return thresholds.moderate
  return thresholds.poor
}
