import { normalizeText } from './auditViewModel.js'

function resolveRiskScoreCardColor(level) {
  const normalized = normalizeText(level).toLowerCase()
  if (['critical', 'high'].includes(normalized)) return '#ef4444'
  if (['medium', 'warning'].includes(normalized)) return '#f59e0b'
  if (['low', 'success'].includes(normalized)) return 'var(--success)'
  return 'var(--theme-primary)'
}

function resolveStateColor(tone, fallback = 'var(--theme-primary)') {
  const normalized = normalizeText(tone).toLowerCase()
  if (['active', 'success', 'online'].includes(normalized)) return 'var(--success)'
  if (['disabled', 'offline', 'draft'].includes(normalized)) return '#64748b'
  if (['failed', 'danger', 'critical', 'high'].includes(normalized)) return '#ef4444'
  if (['review', 'warning', 'medium'].includes(normalized)) return '#f59e0b'
  if (['generating', 'info'].includes(normalized)) return 'var(--theme-primary)'
  return fallback
}

export function buildAuditDetailTopBar({
  skill,
  usesJsonRiskRule = false
} = {}) {
  if (!skill) return null

  const title = normalizeText(skill.name) || '规则中心详情'
  const desc =
    normalizeText(skill.riskRuleSubtitle) ||
    normalizeText(skill.summary) ||
    normalizeText(skill.configDesc) ||
    '查看规则配置、版本审核、测试结果与上线状态。'
  const kpis = []

  if (usesJsonRiskRule) {
    const scoreLevel = skill.riskRuleScoreLevel || skill.riskRuleSeverity
    const score = skill.riskRuleScore ?? '--'
    kpis.push({
      label: '风险分',
      value: String(score),
      unit: score === '--' ? '' : '分',
      meta: normalizeText(skill.riskRuleScoreLabel || skill.riskRuleSeverityLabel) || '待评估',
      trend: ['critical', 'high', 'medium', 'warning'].includes(normalizeText(scoreLevel).toLowerCase())
        ? 'down'
        : 'up',
      color: resolveRiskScoreCardColor(scoreLevel)
    })
    kpis.push({
      label: '风险等级',
      value: normalizeText(skill.riskRuleSeverityLabel) || '待评估',
      unit: '',
      meta: normalizeText(skill.riskRuleScoreLabel) || '评分模型',
      trend: ['critical', 'high', 'medium'].includes(normalizeText(scoreLevel).toLowerCase()) ? 'down' : 'up',
      color: resolveRiskScoreCardColor(scoreLevel)
    })
    kpis.push({
      label: '规则状态',
      value: normalizeText(skill.status) || '待上线',
      unit: '',
      meta: normalizeText(skill.displayVersion) || '工作版本',
      trend: '',
      color: resolveStateColor(skill.statusTone)
    })
    kpis.push({
      label: '上线状态',
      value: normalizeText(skill.isOnlineLabel) || '待上线',
      unit: '',
      meta: normalizeText(skill.publishedAt) && skill.publishedAt !== '-' ? skill.publishedAt : '未发布',
      trend: skill.isOnlineValue ? 'up' : '',
      color: resolveStateColor(skill.isOnlineTone)
    })
    kpis.push({
      label: '启用状态',
      value: normalizeText(skill.isEnabledLabel) || '否',
      unit: '',
      meta: skill.isEnabledValue ? '参与扫描' : '不参与扫描',
      trend: skill.isEnabledValue ? 'up' : '',
      color: resolveStateColor(skill.isEnabledTone)
    })
  }

  return {
    view: { title, desc },
    kpis
  }
}
