{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://awooop.platform/contracts/v1/channel_event",
  "title": "Channel Event Contract",
  "description": "AwoooP Channel Event 合約 — 定義來自外部 channel 的事件結構（冪等性 + 去重）",
  "type": "object",
  "required": ["event_id", "channel_type", "event_type", "project_id", "payload", "received_at"],
  "additionalProperties": false,
  "properties": {
    "event_id": {
      "type": "string",
      "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
      "description": "Platform 生成的事件 UUID"
    },
    "project_id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9_-]{1,63}$",
      "description": "所屬租戶"
    },
    "channel_type": {
      "type": "string",
      "enum": ["telegram", "slack", "webhook", "api"],
      "description": "Channel 類型"
    },
    "event_type": {
      "type": "string",
      "enum": [
        "message_received",
        "callback_query",
        "command_invoked",
        "webhook_post",
        "api_request",
        "approval_response"
      ],
      "description": "事件類型"
    },
    "provider_event_id": {
      "type": "string",
      "maxLength": 256,
      "description": "原始 provider 的事件 ID（去重用）"
    },
    "user_id": {
      "type": "string",
      "description": "發送事件的使用者識別符（platform_subject_id）"
    },
    "chat_id": {
      "type": ["string", "null"],
      "description": "Telegram chat_id 或 Slack channel_id"
    },
    "payload": {
      "type": "object",
      "description": "原始 channel payload（channel-specific 結構）",
      "minProperties": 1
    },
    "text": {
      "type": ["string", "null"],
      "maxLength": 4096,
      "description": "訊息文字（方便直接讀取，不需 parse payload）"
    },
    "attachments": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["attachment_type", "file_id"],
        "additionalProperties": false,
        "properties": {
          "attachment_type": {"type": "string", "enum": ["photo", "document", "audio", "video"]},
          "file_id": {"type": "string"},
          "sha256": {
            "type": ["string", "null"],
            "pattern": "^[0-9a-f]{64}$",
            "description": "下載後的附件 SHA-256"
          }
        }
      },
      "description": "附件清單"
    },
    "run_id": {
      "type": ["string", "null"],
      "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
      "description": "觸發的 run UUID（事件處理後填入）"
    },
    "is_duplicate": {
      "type": "boolean",
      "default": false,
      "description": "此事件是否為重複（去重後設為 true）"
    },
    "received_at": {
      "type": "string",
      "format": "date-time",
      "description": "Platform 收到事件的時間（ISO 8601）"
    }
  }
}
