{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://daarion.city/schemas/brand/BrandSource.schema.json",
  "title": "BrandSource",
  "type": "object",
  "required": ["id", "created_at", "source_type", "payload", "attribution"],
  "properties": {
    "id": { "type": "string", "description": "ULID/UUID" },
    "created_at": { "type": "string", "format": "date-time" },
    "created_by": { "type": "string", "description": "user_id/service_id" },
    "workspace_id": { "type": "string" },
    "project_id": { "type": "string" },
    "agent_id": { "type": "string" },
    "source_type": {
      "type": "string",
      "enum": ["url", "text", "file", "figma", "drive", "notion"]
    },
    "payload": {
      "type": "object",
      "required": ["raw_ref"],
      "properties": {
        "raw_ref": {
          "type": "string",
          "description": "S3 key або URL (для url/text допускається пряме значення з content-addressed ref)"
        },
        "mime_type": { "type": "string" },
        "filename": { "type": "string" },
        "size_bytes": { "type": "integer", "minimum": 0 },
        "sha256": { "type": "string", "pattern": "^[A-Fa-f0-9]{64}$" },
        "text_excerpt": { "type": "string", "maxLength": 2000 },
        "url": { "type": "string" }
      },
      "additionalProperties": true
    },
    "signals": {
      "type": "object",
      "properties": {
        "domains_found": { "type": "array", "items": { "type": "string" } },
        "aliases_found": { "type": "array", "items": { "type": "string" } },
        "keywords_found": { "type": "array", "items": { "type": "string" } },
        "context": {
          "type": "object",
          "properties": {
            "chat_id": { "type": "string" },
            "thread_id": { "type": "string" },
            "message_id": { "type": "string" }
          },
          "additionalProperties": true
        },
        "attachment_hints": { "type": "array", "items": { "type": "string" } }
      },
      "additionalProperties": false
    },
    "attribution": {
      "type": "object",
      "required": ["status", "candidates"],
      "properties": {
        "status": { "type": "string", "enum": ["attributed", "unattributed", "needs_review"] },
        "brand_id": { "type": "string" },
        "confidence": { "type": "number", "minimum": 0, "maximum": 1 },
        "candidates": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["brand_id", "score"],
            "properties": {
              "brand_id": { "type": "string" },
              "score": { "type": "number", "minimum": 0, "maximum": 1 },
              "reasons": { "type": "array", "items": { "type": "string" } }
            }
          }
        }
      },
      "additionalProperties": false
    },
    "processing": {
      "type": "object",
      "properties": {
        "status": { "type": "string", "enum": ["queued", "processing", "done", "failed"] },
        "error": { "type": "string" },
        "extractor_versions": { "type": "object", "additionalProperties": { "type": "string" } }
      },
      "additionalProperties": false
    },
    "tags": { "type": "array", "items": { "type": "string" } },
    "visibility": { "type": "string", "enum": ["private", "workspace", "public"], "default": "workspace" }
  },
  "additionalProperties": false
}
