import { normalizeText, readConfigJson, resolveRuleTemplateLabel } from './auditViewModel.js'

export function incrementVersion(version) {
  const normalized = normalizeText(version).replace(/^v/i, '')
  const match = normalized.match(/^(\d+)\.(\d+)\.(\d+)$/)

  if (!match) {
    return 'v1.0.0'
  }

  const major = Number(match[1])
  const minor = Number(match[2])
  const patch = Number(match[3]) + 1
  return `v${major}.${minor}.${patch}`
}

export function buildReviewNote(status) {
  if (status === 'approved') {
    return '通过规则中心审核。'
  }
  if (status === 'rejected') {
    return '在规则中心驳回当前版本。'
  }
  return '提交规则中心待审核。'
}

export function buildRuleConfigPayload(asset, runtimeRule) {
  const configJson = {
    ...readConfigJson(asset),
    runtime_kind: normalizeText(runtimeRule?.kind) || asset.runtimeKind || 'policy_rule_draft',
    runtime_rule: runtimeRule
  }
  const templateKey = normalizeText(runtimeRule?.template_key) || asset.ruleTemplateKey
  if (templateKey) {
    configJson.rule_template_key = templateKey
    configJson.rule_template_label = resolveRuleTemplateLabel(templateKey)
  }
  return configJson
}

export function buildSpreadsheetChangeRecordKey(records = []) {
  const latest = records.find((item) => item?.changed_at)
  if (!latest) {
    return ''
  }
  const previewSignature = Array.isArray(latest.cell_changes)
    ? latest.cell_changes
        .slice(0, 8)
        .map((item) =>
          [
            item?.sheet_name,
            item?.cell,
            item?.change_type,
            item?.before_value,
            item?.after_value
          ]
            .map((value) => normalizeText(value))
            .join(':')
        )
        .join('|')
    : ''
  const sheetSignature = Array.isArray(latest.sheet_changes)
    ? latest.sheet_changes
        .map((item) =>
          [item?.sheet_name, item?.change_type]
            .map((value) => normalizeText(value))
            .join(':')
        )
        .join('|')
    : ''
  return [
    latest.id,
    latest.changed_at,
    latest.actor,
    latest.summary,
    latest.changed_sheet_count,
    latest.changed_cell_count,
    sheetSignature,
    previewSignature
  ]
    .map((value) => normalizeText(value))
    .join('-')
}

export function filterAuditAssets(assets = [], filters = {}) {
  const normalizedKeyword = normalizeText(filters.keyword).toLowerCase()
  const hasKeyword = Boolean(normalizedKeyword)
  const hasDomain = Boolean(filters.selectedDomain)
  const hasOwner = Boolean(filters.selectedOwner)
  const hasRiskLevel = Boolean(filters.selectedRiskLevel)
  const hasStatus = Boolean(filters.showStatusFilter && filters.selectedStatus)
  const hasRiskScenario = Boolean(filters.showRiskScenarioFilter && filters.selectedRiskScenario)
  const hasOnline = Boolean(filters.showOnlineFilter && filters.selectedOnlineState)
  const hasEnabled = Boolean(filters.showEnabledFilter && filters.selectedEnabledState)

  return assets.filter((item) => {
    if (hasKeyword) {
      const searchText = item.searchText || [item.name, item.code, item.summary, item.owner, item.scope, item.riskLevelLabel]
        .map((value) => normalizeText(value).toLowerCase())
        .filter(Boolean)
        .join(' ')
      if (!searchText.includes(normalizedKeyword)) {
        return false
      }
    }
    if (hasDomain && item.domainValue !== filters.selectedDomain) {
      return false
    }
    if (hasOwner && item.owner !== filters.selectedOwner) {
      return false
    }
    if (hasRiskLevel && item.riskLevelValue !== filters.selectedRiskLevel) {
      return false
    }
    if (hasStatus && item.statusValue !== filters.selectedStatus) {
      return false
    }
    if (hasRiskScenario) {
      const hasScenarioList = Array.isArray(item.scenarioList) && item.scenarioList.length
      const matched = hasScenarioList
        ? item.scenarioList.includes(filters.selectedRiskScenario)
        : item.riskCategory === filters.selectedRiskScenario
      if (!matched) {
        return false
      }
    }
    if (hasOnline && (filters.selectedOnlineState === 'online') !== Boolean(item.isOnlineValue)) {
      return false
    }
    if (hasEnabled && (filters.selectedEnabledState === 'enabled') !== Boolean(item.isEnabledValue)) {
      return false
    }

    return true
  })
}
