{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Commercial Trust Report Specification v1.3",
  "description": "Fly Protocol - CTRS v1.3: Fact-Anchor + Multi-Agent Contribution + Settlement Decoupling",
  "type": "object",
  "required": ["report_id", "schema_version", "type", "created_at", "status", "issuer", "fact_anchor", "claim", "evidence", "rule", "attribution", "settlement"],
  "properties": {
    "report_id": {"type": "string"},
    "schema_version": {"type": "string", "const": "CTRS-v1.3"},
    "type": {"type": "string", "const": "CommercialTrustReport"},
    "created_at": {"type": "string", "format": "date-time"},
    "status": {
      "type": "string",
      "enum": ["draft", "verified", "disputed", "settled", "eligible"]
    },
    "issuer": {
      "type": "object",
      "required": ["id", "name"],
      "properties": {
        "id": {"type": "string"},
        "name": {"type": "string"},
        "did": {"type": "string", "description": "Issuer DID for cross-domain verification"}
      }
    },
    "fact_anchor": {
      "type": "object",
      "description": "v1.3 新增：全链路事实锚点 — 绑定任务发起时的根事实标识",
      "required": ["anchor_id", "task_hash", "participants_hash", "created_at"],
      "properties": {
        "anchor_id": {
          "type": "string",
          "description": "全局唯一事实锚点ID (UUID)"
        },
        "task_hash": {
          "type": "string",
          "description": "SHA-256(任务内容 + 参与方列表 + 预设分账规则)"
        },
        "participants_hash": {
          "type": "string",
          "description": "SHA-256(所有参与方DID列表)"
        },
        "split_rule_hash": {
          "type": "string",
          "description": "SHA-256(预设分账规则JSON)"
        },
        "created_at": {"type": "string", "format": "date-time"},
        "chain_proof": {
          "type": "object",
          "description": "链上存证证明（可选）",
          "properties": {
            "chain": {"type": "string", "enum": ["ethereum", "polygon", "bsc", "solana"]},
            "tx_hash": {"type": "string"},
            "block_number": {"type": "integer"}
          }
        }
      }
    },
    "claim": {
      "type": "object",
      "description": "Layer 1: Claim — What Fly is proving (who did what)",
      "required": ["claim_id", "type", "subject", "description", "timestamp", "parties", "anchor_ref"],
      "properties": {
        "claim_id": {"type": "string"},
        "type": {"type": "string"},
        "subject": {"type": "string"},
        "description": {"type": "string"},
        "timestamp": {"type": "string", "format": "date-time"},
        "anchor_ref": {
          "type": "string",
          "description": "v1.3 新增：关联的fact_anchor.anchor_id"
        },
        "parties": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["id", "role", "did"],
            "properties": {
              "id": {"type": "string"},
              "role": {"type": "string"},
              "name": {"type": "string"},
              "did": {"type": "string", "description": "v1.3 新增：参与方DID，跨主体验证必需"}
            },
            "additionalProperties": true
          }
        },
        "joint_claim": {
          "type": "boolean",
          "description": "v1.3 新增：是否为多Agent联合认领"
        }
      }
    },
    "evidence": {
      "type": "array",
      "description": "Layer 2: Evidence — Supporting data for the claim",
      "items": {
        "type": "object",
        "required": ["evidence_id", "claim_ref", "type", "source", "timestamp", "data", "hash", "agent_signature"],
        "properties": {
          "evidence_id": {"type": "string"},
          "claim_ref": {"type": "string"},
          "type": {
            "type": "string",
            "enum": ["webhook", "conversation", "mcp", "payment", "crm", "stripe", "custom", "task_output"]
          },
          "source": {"type": "string"},
          "timestamp": {"type": "string", "format": "date-time"},
          "data": {"type": "object"},
          "hash": {"type": "string", "description": "SHA-256(data)"},
          "agent_signature": {
            "type": "string",
            "description": "v1.3 新增：生成此证据的Agent签名 (Ed25519)"
          },
          "parent_task_id": {
            "type": "string",
            "description": "v1.3 新增：关联的父任务ID（用于子任务贡献追踪）"
          }
        }
      }
    },
    "rule": {
      "type": "object",
      "description": "Layer 3: Rule — First-Class Object (v1.1+). Identity layer: who defined the rule, what version, what hash.",
      "required": ["rule_id", "issuer", "version", "hash", "definition", "created_at"],
      "properties": {
        "rule_id": {"type": "string"},
        "issuer": {"type": "string"},
        "version": {"type": "string"},
        "hash": {"type": "string"},
        "definition": {"type": "object"},
        "created_at": {"type": "string", "format": "date-time"},
        "attribution_model": {
          "type": "string",
          "enum": ["fixed_ratio", "value_based", "evidence_based", "custom"],
          "description": "v1.3 新增：支持的归因模型类型"
        }
      }
    },
    "attribution": {
      "type": "object",
      "description": "Layer 4: Attribution — Evidence + rule_hash = Attribution. Binds to rule_hash, not rule text.",
      "required": ["attribution_id", "claim_ref", "rule_hash", "method", "confidence", "result", "reasoning", "attributed_at", "contribution_proofs"],
      "properties": {
        "attribution_id": {"type": "string"},
        "claim_ref": {"type": "string"},
        "rule_hash": {"type": "string"},
        "method": {"type": "string"},
        "confidence": {"type": "number", "minimum": 0, "maximum": 1},
        "result": {"type": "object"},
        "reasoning": {"type": "string"},
        "attributed_at": {"type": "string", "format": "date-time"},
        "contribution_proofs": {
          "type": "array",
          "description": "v1.3 新增：每个参与方的贡献证明链",
          "items": {
            "type": "object",
            "required": ["party_id", "party_did", "output_hash", "timestamp", "signature"],
            "properties": {
              "party_id": {"type": "string"},
              "party_did": {"type": "string"},
              "output_hash": {"type": "string", "description": "SHA-256(该Agent的输出内容)"},
              "timestamp": {"type": "string", "format": "date-time"},
              "signature": {"type": "string", "description": "Agent对output_hash的签名"}
            }
          }
        }
      }
    },
    "settlement": {
      "type": "object",
      "description": "Layer 5: Settlement — Payment recommendation based on attribution",
      "required": ["settlement_id", "attribution_ref", "status", "amount", "currency", "split", "eligible_parties", "fact_verification"],
      "properties": {
        "settlement_id": {"type": "string"},
        "attribution_ref": {"type": "string"},
        "status": {
          "type": "string",
          "enum": ["pending_fact_verification", "eligible", "pending", "settled", "disputed"],
          "description": "v1.3 新增：pending_fact_verification — 等待事实验证通过后才能进入eligible"
        },
        "amount": {"type": "string"},
        "currency": {"type": "string"},
        "split": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["party_id", "share_pct", "share_amount"],
            "properties": {
              "party_id": {"type": "string"},
              "share_pct": {"type": "string"},
              "share_amount": {"type": "string"}
            }
          }
        },
        "eligible_parties": {"type": "array", "items": {"type": "string"}},
        "fact_verification": {
          "type": "object",
          "description": "v1.3 新增：事实验证状态 — 结算必须绑定事实验证通过",
          "required": ["anchor_verified", "evidence_chain_valid", "contribution_proofs_valid"],
          "properties": {
            "anchor_verified": {
              "type": "boolean",
              "description": "fact_anchor链上存证是否验证通过"
            },
            "evidence_chain_valid": {
              "type": "boolean",
              "description": "证据哈希链是否完整且有效"
            },
            "contribution_proofs_valid": {
              "type": "boolean",
              "description": "所有参与方贡献证明是否验证通过"
            },
            "verified_at": {"type": "string", "format": "date-time"}
          }
        }
      }
    }
  },
  "_v1.3_changes": {
    "fact_anchor": "新增全链路事实锚点：绑定任务发起时的根事实标识，链上存证",
    "agent_did": "claim.parties新增did字段，跨主体验证必需",
    "joint_claim": "claim新增joint_claim布尔值，支持多Agent联合认领",
    "agent_signature": "evidence新增agent_signature，每个证据必须有生成Agent的签名",
    "parent_task_id": "evidence新增parent_task_id，用于子任务贡献追踪",
    "attribution_model": "rule新增attribution_model枚举，支持多种归因模型",
    "contribution_proofs": "attribution新增contribution_proofs数组，每个参与方的贡献证明链",
    "fact_verification": "settlement新增fact_verification对象，结算必须绑定事实验证通过",
    "pending_fact_verification": "settlement.status新增pending_fact_verification状态"
  }
}
