// agentsurface 核心数据模型。
// 设计原则:所有分析结果都是显式、可序列化、可追溯的;
// 能力只分「显式 / 推断 / 未知」三态,不把推断包装成事实。

///|
/// Agent 项目中的一个能力类别。
/// `Unknown` 用于无法归类的信号,保留原始文本以便审计。
pub(all) enum Capability {
  FsRead
  FsWrite
  NetEgress
  ShellExec
  SecretsRead
  DbQuery
  BrowserControl
  Clipboard
  ProcessSpawn
  ModelCall
  Unknown(String)
}

///|
/// 能力判定的证据强度。
pub(all) enum Confidence {
  Explicit
  Inferred
  Unknown
}

///|
/// 风险级别。
pub(all) enum Severity {
  Info
  Low
  Medium
  High
  Critical
}

///|
/// Agent 项目中的组件类型。
pub(all) enum ComponentKind {
  McpServer
  Tool
  Skill
  Settings
  Script
}

///|
/// 策略决策。
pub(all) enum Decision {
  Allow
  Deny
  Ask
}

///|
/// 一个被分析的组件(MCP server / tool / skill / settings / script)。
pub(all) struct Component {
  id : String
  kind : ComponentKind
  name : String
  source : String
}

///|
/// 一条能力事实:某个组件具备某个能力,以及证据与置信度。
pub(all) struct CapabilityFact {
  component_id : String
  capability : Capability
  confidence : Confidence
  evidence : String
  source : String
}

///|
/// 一条风险发现。
pub(all) struct Finding {
  rule_id : String
  severity : Severity
  component_id : String
  message : String
  evidence : String
  remediation : String
}

///|
/// 一条最小权限策略。
pub(all) struct PolicyRule {
  capability : Capability
  decision : Decision
  reason : String
}

///|
/// 一次扫描的完整结果。
pub(all) struct Report {
  project_root : String
  components : Array[Component]
  facts : Array[CapabilityFact]
  findings : Array[Finding]
  policy : Array[PolicyRule]
  unknowns : Array[String]
}

///|
pub fn capability_id(capability : Capability) -> String {
  match capability {
    FsRead => "fs.read"
    FsWrite => "fs.write"
    NetEgress => "net.egress"
    ShellExec => "shell.exec"
    SecretsRead => "secrets.read"
    DbQuery => "db.query"
    BrowserControl => "browser.control"
    Clipboard => "clipboard"
    ProcessSpawn => "process.spawn"
    ModelCall => "model.call"
    Unknown(raw) => "unknown:" + raw
  }
}

///|
pub fn confidence_id(confidence : Confidence) -> String {
  match confidence {
    Explicit => "explicit"
    Inferred => "inferred"
    Unknown => "unknown"
  }
}

///|
pub fn severity_id(severity : Severity) -> String {
  match severity {
    Info => "info"
    Low => "low"
    Medium => "medium"
    High => "high"
    Critical => "critical"
  }
}

///|
pub fn severity_rank(severity : Severity) -> Int32 {
  match severity {
    Info => 0
    Low => 1
    Medium => 2
    High => 3
    Critical => 4
  }
}

///|
pub fn component_kind_id(kind : ComponentKind) -> String {
  match kind {
    McpServer => "mcp_server"
    Tool => "tool"
    Skill => "skill"
    Settings => "settings"
    Script => "script"
  }
}

///|
pub fn decision_id(decision : Decision) -> String {
  match decision {
    Allow => "allow"
    Deny => "deny"
    Ask => "ask"
  }
}

///|
/// 判断两个能力是否相同。`Unknown` 按原始文本比较。
pub fn capability_equal(a : Capability, b : Capability) -> Bool {
  match (a, b) {
    (FsRead, FsRead) => true
    (FsWrite, FsWrite) => true
    (NetEgress, NetEgress) => true
    (ShellExec, ShellExec) => true
    (SecretsRead, SecretsRead) => true
    (DbQuery, DbQuery) => true
    (BrowserControl, BrowserControl) => true
    (Clipboard, Clipboard) => true
    (ProcessSpawn, ProcessSpawn) => true
    (ModelCall, ModelCall) => true
    (Unknown(x), Unknown(y)) => x == y
    _ => false
  }
}

///|
/// 在事实列表中查找某组件是否具备某能力。
pub fn has_capability(
  facts : Array[CapabilityFact],
  component_id : String,
  capability : Capability,
) -> Bool {
  for fact in facts {
    if fact.component_id == component_id &&
      capability_equal(fact.capability, capability) {
      return true
    }
  }
  false
}