{
  "openapi": "3.1.0",
  "info": {
    "title": "iTrail API Gateway",
    "version": "1.0",
    "description": "API Gateway Xác thực & Thao tác Zalo Core dành cho các Hệ thống bên ngoài tích hợp."
  },
  "servers": [
    {
      "url": "https://sms-service.talab.io.vn/api/gateway/v1.0",
      "description": "Máy chủ Production"
    }
  ],
  "security": [
    {
      "ApiKeyAuth": []
    }
  ],
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "x-api-key",
        "description": "Khóa bí mật (Secret Key) được cung cấp trong Bảng điều khiển ZaloLite API."
      }
    }
  },
  "paths": {
    "/bots": {
      "get": {
        "tags": ["Bots"],
        "summary": "Danh sách Bot được phép truy cập",
        "description": "Lấy danh sách tất cả các Bot Zalo mà API Key hiện tại có quyền truy cập.",
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/json": {
                "example": {
                  "success": true,
                  "data": [
                    {
                      "id": "bot-uuid",
                      "name": "Tên Bot",
                      "avatar": "https://url-to-avatar.com",
                      "status": { "state": "LISTENING", "message": "Đang lắng nghe (Realtime ON)" },
                      "is_active": true,
                      "last_active_at": "2026-03-24T00:00:00.000Z"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/bots/{bot_id}": {
      "get": {
        "tags": ["Bots"],
        "summary": "Thông tin cá nhân của Bot",
        "description": "Lấy thông tin chi tiết về một bot cụ thể, bao gồm ảnh đại diện, tên hiển thị, Zalo Global UID và Số điện thoại liên kết (nếu có).",
        "parameters": [
          {
            "name": "bot_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "UUID của Bot"
          }
        ],
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/json": {
                "example": {
                  "success": true,
                  "data": {
                    "id": "bot-uuid",
                    "name": "Tên Bot",
                    "avatar": "https://url-to-avatar.com",
                    "phone": "84901234567",
                    "zalo_global_uid": "1234567890123456789",
                    "is_active": true,
                    "status": { "state": "LISTENING" },
                    "last_active_at": "2026-03-24T00:00:00.000Z",
                    "raw_profile": {}
                  }
                }
              }
            }
          }
        }
      }
    },
    "/bots/{bot_id}/labels": {
      "get": {
        "tags": ["Labels"],
        "summary": "Danh sách Nhãn của Bot",
        "parameters": [
          { "name": "bot_id", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/json": {
                "example": {
                  "success": true,
                  "data": [{ "id": "uuid-...", "zalo_label_id": 1, "name": "VIP", "color": "#ff0000" }]
                }
              }
            }
          }
        }
      },
      "post": {
        "tags": ["Labels"],
        "summary": "Tạo Nhãn mới",
        "parameters": [
          { "name": "bot_id", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["name", "color"],
                "properties": { "name": { "type": "string" }, "color": { "type": "string" } }
              }
            }
          }
        },
        "responses": { "201": { "description": "Tạo thành công" } }
      }
    },
    "/bots/{bot_id}/labels/{id}": {
      "patch": {
        "tags": ["Labels"],
        "summary": "Cập nhật Nhãn",
        "parameters": [
          { "name": "bot_id", "in": "path", "required": true, "schema": { "type": "string" } },
          { "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } }
        ],
        "requestBody": {
          "content": {
            "application/json": { "schema": { "type": "object", "properties": { "name": { "type": "string" }, "color": { "type": "string" } } } }
          }
        },
        "responses": { "200": { "description": "Thành công" } }
      },
      "delete": {
        "tags": ["Labels"],
        "summary": "Xóa Nhãn",
        "parameters": [
          { "name": "bot_id", "in": "path", "required": true, "schema": { "type": "string" } },
          { "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } }
        ],
        "responses": { "200": { "description": "Thành công" } }
      }
    },
    "/bots/{bot_id}/labels/sync": {
      "post": {
        "tags": ["Labels"],
        "summary": "Đồng bộ Nhãn",
        "parameters": [
          { "name": "bot_id", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": { "200": { "description": "Đồng bộ hoàn tất" } }
      }
    },
    "/bots/{bot_id}/conversations/{id}/labels": {
      "get": {
        "tags": ["Labels"],
        "summary": "Nhãn hiện tại của hội thoại",
        "parameters": [
          { "name": "bot_id", "in": "path", "required": true, "schema": { "type": "string" } },
          { "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": { "200": { "description": "Thành công" } }
      },
      "post": {
        "tags": ["Labels"],
        "summary": "Gán/Gỡ Nhãn cho Hội thoại",
        "parameters": [
          { "name": "bot_id", "in": "path", "required": true, "schema": { "type": "string" } },
          { "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["zalo_label_id", "action"],
                "properties": { "zalo_label_id": { "type": "integer" }, "action": { "type": "string", "enum": ["add", "remove"] } }
              }
            }
          }
        },
        "responses": { "200": { "description": "Thành công" } }
      }
    },
    "/bots/{bot_id}/friends": {
      "get": {
        "tags": ["Friends"],
        "summary": "Danh sách Bạn bè",
        "parameters": [
          { "name": "bot_id", "in": "path", "required": true, "schema": { "type": "string" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 50 } },
          { "name": "offset", "in": "query", "schema": { "type": "integer", "default": 0 } }
        ],
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/json": { "example": { "success": true, "data": [] } }
            }
          }
        }
      }
    },
    "/bots/{bot_id}/friends/{uid}": {
      "delete": {
        "tags": ["Friends"],
        "summary": "Hủy Kết bạn",
        "parameters": [
          { "name": "bot_id", "in": "path", "required": true, "schema": { "type": "string" } },
          { "name": "uid", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": { "200": { "description": "Thành công" } }
      }
    },
    "/bots/{bot_id}/friends/aliases": {
      "post": {
        "tags": ["Friends"],
        "summary": "Cập nhật hoặc xóa Bí danh của Bạn bè",
        "description": "Đặt tên gợi nhớ (Alias) cho bạn bè. Truyền chuỗi rỗng để xóa bí danh.",
        "parameters": [
          { "name": "bot_id", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["target_zalo_id"],
                "properties": {
                  "target_zalo_id": { "type": "string" },
                  "alias": { "type": "string", "description": "Tên Bí danh mới" }
                }
              }
            }
          }
        },
        "responses": { "200": { "description": "Cập nhật thành công" } }
      }
    },
    "/bots/{bot_id}/friends/requests/send-batch": {
      "post": {
        "tags": ["Friends"],
        "summary": "Gửi Kết bạn Batch cho nhiều người lạ",
        "description": "Gửi lời mời kết bạn cho nhiều người lạ trong 1 lần gọi API. Tự động tra cứu SĐT. Hỗ trợ phân phối qua nhiều bot.\n\n**Giới hạn:** Tối đa **5 recipients/lần gọi**.\n\n**Cơ chế xử lý:**\n- Batch ≤5: Trả kết quả đồng bộ (chi tiết từng recipient)\n\nDùng `GET /bots/{bot_id}/campaigns/{campaign_id}` để theo dõi tiến độ nếu cần.",
        "parameters": [
          {
            "name": "bot_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "UUID của Bot chính"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["recipients"],
                "properties": {
                  "recipients": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": 5,
                    "description": "Danh sách người muốn kết bạn (tối đa 5)",
                    "items": {
                      "type": "object",
                      "minProperties": 1,
                      "properties": {
                        "phone": { "type": "string", "description": "Số điện thoại (tự động resolve → UID)" },
                        "uid": { "type": "string", "description": "Zalo UID (dạng số)" }
                      },
                      "example": { "phone": "0901234567" }
                    }
                  },
                  "message": {
                    "type": "string",
                    "default": "Kết bạn nhé!",
                    "description": "Lời nhắn gửi kèm yêu cầu kết bạn."
                  },
                  "bot_ids": {
                    "type": "array",
                    "items": { "type": "string" },
                    "description": "Danh sách bot_id bổ sung để phân phối recipients."
                  },
                  "mode": {
                    "type": "string",
                    "enum": ["safe", "normal", "turbo"],
                    "default": "safe",
                    "description": "Chế độ rate limit. safe=3 req/min, normal=5, turbo=10."
                  },
                  "alias_prefix": {
                    "type": "string",
                    "description": "Tiền tố tự động đổi tên sau khi gửi kết bạn (VD: 'KH_' → 'KH_Nguyen Van A')."
                  }
                }
              },
              "examples": {
                "basic": {
                  "summary": "Gửi kết bạn cho 2 người",
                  "value": {
                    "recipients": [
                      { "phone": "0901234567" },
                      { "uid": "9186543210987654" }
                    ],
                    "message": "Chào bạn, kết bạn nhé!",
                    "mode": "safe"
                  }
                },
                "with-alias": {
                  "summary": "Kết bạn + tự động đổi tên",
                  "value": {
                    "recipients": [
                      { "phone": "0901234567" }
                    ],
                    "message": "Chào bạn!",
                    "alias_prefix": "KH_",
                    "mode": "safe"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/json": {
                "example": {
                  "success": true,
                  "campaign_id": "uuid-...",
                  "total": 2,
                  "accepted": 1,
                  "status": "completed",
                  "results": [
                    { "phone": "0901234567", "status": "success" },
                    { "uid": "91865...", "status": "failed", "error": "Không tìm thấy người dùng" }
                  ]
                }
              }
            }
          },
          "400": { "description": "Lỗi validate" }
        }
      }
    },
    "/bots/{bot_id}/friends/requests": {
      "post": {
        "tags": ["Friends"],
        "summary": "Xử lý Lời mời Kết bạn",
        "parameters": [
          { "name": "bot_id", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["action"],
                "properties": {
                  "action": { "type": "string", "enum": ["send", "accept", "reject"] },
                  "phone": { "type": "string" },
                  "target_zalo_id": { "type": "string" },
                  "message": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": { "200": { "description": "Thành công" } }
      }
    },
    "/bots/{bot_id}/friends/requests/pending": {
      "get": {
        "tags": ["Friends"],
        "summary": "Danh sách Lời mời đang chờ",
        "parameters": [
          { "name": "bot_id", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/json": { "example": { "success": true, "data": [] } }
            }
          }
        }
      }
    },
    "/bots/{bot_id}/campaigns/{campaign_id}": {
      "get": {
        "tags": ["Campaigns"],
        "summary": "Trạng thái Batch Campaign",
        "description": "Lấy trạng thái hiện tại của một batch campaign (được tạo từ send-batch hoặc friend-request-send-batch). Dùng để poll tiến độ khi batch chạy async.",
        "parameters": [
          {
            "name": "bot_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "UUID của Bot"
          },
          {
            "name": "campaign_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" },
            "description": "UUID của Campaign (lấy từ response của send-batch)"
          }
        ],
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/json": {
                "example": {
                  "success": true,
                  "data": {
                    "id": "uuid-...",
                    "type": "MESSAGE",
                    "status": "COMPLETED",
                    "total_targets": 10,
                    "success_count": 8,
                    "failed_count": 2,
                    "created_at": "2026-06-04T12:00:00Z",
                    "updated_at": "2026-06-04T12:05:00Z",
                    "source": "gateway_api",
                    "logs": [
                      { "bot_id": "bot-uuid", "external_uid": "91865...", "status": "SUCCESS" },
                      { "bot_id": "bot-uuid", "external_uid": "91866...", "status": "FAILED", "error_message": "User not found" }
                    ]
                  }
                }
              }
            }
          },
          "404": { "description": "Campaign không tồn tại hoặc không có quyền truy cập" }
        }
      }
    },
    "/bots/{bot_id}/conversations": {
      "get": {
        "tags": ["Conversations"],
        "summary": "Danh sách Hội thoại",
        "description": "Lấy danh sách các hội thoại đang hoạt động của Bot, sắp xếp theo thời gian hoạt động gần nhất.",
        "parameters": [
          {
            "name": "bot_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "UUID của Bot"
          },
          {
            "name": "limit",
            "in": "query",
            "schema": { "type": "integer", "default": 20 },
            "description": "Số hội thoại mỗi trang"
          },
          {
            "name": "offset",
            "in": "query",
            "schema": { "type": "integer", "default": 0 }
          }
        ],
        "responses": {
          "200": {
            "description": "Danh sách hội thoại",
            "content": {
               "application/json": {
                 "example": {
                   "success": true,
                   "data": [
                     {
                       "conversation_id": "e8a9d...-...",
                       "customer_identity_id": "75f2b...-...",
                       "name": "Nguyễn Văn Khách",
                       "avatar": "https://avatar-url...",
                       "phone": "84901234567",
                       "last_activity_at": "2026-03-24T12:05:00Z",
                       "last_message": { "type": "text", "data": { "text": "Vâng, xin cảm ơn" } }
                     }
                   ],
                   "pagination": { "limit": 20, "offset": 0, "count": 1 }
                 }
               }
            }
          }
        }
      }
    },
    "/bots/{bot_id}/conversations/{conversation_id}/messages": {
      "get": {
        "tags": ["Messages"],
        "summary": "Lịch sử Tin nhắn của Hội thoại",
        "description": "Lấy danh sách tin nhắn trong một hội thoại cụ thể, kết quả trả về được sắp xếp giảm dần theo thời gian (tin mới nhất xếp trước).",
        "parameters": [
          {
            "name": "bot_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "UUID của Bot"
          },
          {
            "name": "conversation_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "UUID của Hội thoại"
          },
          {
            "name": "limit",
            "in": "query",
            "schema": { "type": "integer", "default": 50, "maximum": 100 },
            "description": "Số tin nhắn mỗi trang (tối đa 100)"
          },
          {
            "name": "offset",
            "in": "query",
            "schema": { "type": "integer", "default": 0 }
          }
        ],
        "responses": {
          "200": {
            "description": "Mảng tin nhắn (sắp xếp giảm dần theo thời gian gửi).",
            "content": {
              "application/json": {
                "example": {
                  "success": true,
                  "data": [
                    {
                      "message_id": "uuid-...",
                      "zalo_msg_id": "1234567890",
                      "is_bot": false,
                      "api_client_id": null,
                      "sent_at": "2026-03-24T12:00:00Z",
                      "content": { "type": "text", "data": { "text": "Tin nhắn văn bản" } }
                    }
                  ],
                  "pagination": { "limit": 50, "offset": 0, "count": 1 }
                }
              }
            }
          }
        }
      },
      "post": {
        "tags": ["Messages"],
        "summary": "Gửi Tin nhắn vào Hội thoại",
        "description": "Gửi tin nhắn từ Bot đến một hội thoại theo chuẩn RESTful. **Conversation ID nằm trên URL path**. Hỗ trợ: **text**, **image**, **video**, **audio/voice**, **sticker**. Với các loại media, truyền URL công khai vào `attachment_url`.",
        "parameters": [
          {
            "name": "bot_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "UUID của Bot"
          },
          {
            "name": "conversation_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "UUID của Hội thoại"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["content"],
                "properties": {
                  "attachment_url": {
                    "type": "string",
                    "description": "URL công khai trỏ đến tài nguyên media. Bắt buộc với type=image/video/audio/voice."
                  },
                  "content": {
                    "type": "object",
                    "required": ["type", "data"],
                    "properties": {
                      "type": {
                        "type": "string",
                        "enum": ["text", "image", "video", "audio", "voice"],
                        "description": "Loại tin nhắn."
                      },
                      "data": {
                        "type": "object",
                        "description": "Dữ liệu theo từng loại tin nhắn."
                      }
                    }
                  }
                }
              },
              "examples": {
                "text": {
                  "summary": "Gửi văn bản",
                  "value": {
                    "content": { "type": "text", "data": { "text": "Xin chào! Mình có thể giúp gì cho bạn?" } }
                  }
                },
                "image": {
                  "summary": "Gửi ảnh qua URL",
                  "value": {
                    "content": { "type": "image", "data": { "caption": "Hình ảnh sản phẩm", "fileName": "product.jpg" } },
                    "attachment_url": "https://example.com/product.jpg"
                  }
                },
                "video": {
                  "summary": "Gửi video qua URL",
                  "value": {
                    "content": { "type": "video", "data": { "caption": "Video demo", "duration": 15, "width": 1280, "height": 720, "thumbnail_url": "https://example.com/thumb.jpg" } },
                    "attachment_url": "https://example.com/demo.mp4"
                  }
                },
                "voice": {
                  "summary": "Gửi tin nhắn thoại qua URL",
                  "value": {
                    "content": { "type": "voice", "data": {} },
                    "attachment_url": "https://example.com/audio.mp3"
                  }
                },
                "sticker": {
                  "summary": "Gửi sticker",
                  "value": {
                    "content": { "type": "sticker", "data": { "stickerId": 123, "cateId": 0 } }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Gửi thành công",
            "content": {
              "application/json": {
                "example": { "success": true, "message_id": "1234567890" }
              }
            }
          },
          "400": { "description": "Thiếu trường bắt buộc hoặc type không hợp lệ" },
          "404": { "description": "Hội thoại không tồn tại hoặc bot không có quyền truy cập" },
          "503": { "description": "Bot đang offline" }
        }
      }
    },
    "/bots/{bot_id}/messages/send-batch": {
      "post": {
        "tags": ["Messages"],
        "summary": "Gửi Tin nhắn Batch cho nhiều người lạ",
        "description": "Gửi tin nhắn cho nhiều người lạ trong 1 lần gọi API. Tự động tra cứu SĐT, đồng bộ danh tính, tạo hội thoại và gửi tin nhắn. Hỗ trợ phân phối qua nhiều bot.\n\n**Giới hạn:** Tối đa **10 recipients/lần gọi**.\n\n**Cơ chế xử lý:**\n- Batch ≤5: Trả kết quả đồng bộ (chi tiết từng recipient)\n- Batch 6-10: Trả campaign_id để poll trạng thái (async)\n\nDùng `GET /bots/{bot_id}/campaigns/{campaign_id}` để theo dõi tiến độ.",
        "parameters": [
          {
            "name": "bot_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "UUID của Bot chính"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["recipients", "content"],
                "properties": {
                  "recipients": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": 10,
                    "description": "Danh sách người nhận (tối đa 10)",
                    "items": {
                      "type": "object",
                      "minProperties": 1,
                      "properties": {
                        "phone": { "type": "string", "description": "Số điện thoại (tự động resolve → UID)" },
                        "uid": { "type": "string", "description": "Zalo UID (dạng số)" }
                      },
                      "example": { "phone": "0901234567" }
                    }
                  },
                  "content": {
                    "type": "object",
                    "required": ["type", "data"],
                    "properties": {
                      "type": { "type": "string", "enum": ["text", "image", "video", "audio", "voice", "sticker"], "description": "Loại tin nhắn." },
                      "data": { "type": "object", "description": "Dữ liệu theo từng loại tin nhắn." }
                    }
                  },
                  "attachment_url": {
                    "type": "string",
                    "description": "URL công khai trỏ đến tài nguyên media. Bắt buộc với type=image/video/audio/voice."
                  },
                  "bot_ids": {
                    "type": "array",
                    "items": { "type": "string" },
                    "description": "Danh sách bot_id bổ sung để phân phối recipients (server tự động chia đều)."
                  },
                  "mode": {
                    "type": "string",
                    "enum": ["safe", "normal", "turbo"],
                    "default": "safe",
                    "description": "Chế độ rate limit. safe=5 req/min, normal=10, turbo=20."
                  }
                }
              },
              "examples": {
                "text-batch": {
                  "summary": "Gửi văn bản cho 2 người",
                  "value": {
                    "recipients": [
                      { "phone": "0901234567" },
                      { "phone": "0901234568" }
                    ],
                    "content": { "type": "text", "data": { "text": "Xin chào! Đây là tin nhắn tự động." } },
                    "mode": "safe"
                  }
                },
                "image-batch": {
                  "summary": "Gửi ảnh cho nhiều người",
                  "value": {
                    "recipients": [
                      { "uid": "9186543210987654" }
                    ],
                    "content": { "type": "image", "data": { "caption": "Hình ảnh sản phẩm" } },
                    "attachment_url": "https://example.com/product.jpg"
                  }
                },
                "multi-bot": {
                  "summary": "Phân phối qua 2 bot",
                  "value": {
                    "recipients": [
                      { "phone": "0901234567" },
                      { "phone": "0901234568" },
                      { "phone": "0901234569" },
                      { "phone": "0901234570" }
                    ],
                    "content": { "type": "text", "data": { "text": "Xin chào!" } },
                    "bot_ids": ["bot-uuid-2"],
                    "mode": "safe"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Thành công (sync)",
            "content": {
              "application/json": {
                "example": {
                  "success": true,
                  "campaign_id": "uuid-...",
                  "total": 2,
                  "accepted": 2,
                  "status": "completed",
                  "results": [
                    { "phone": "0901234567", "status": "success", "conversation_id": "uuid-...", "message_id": "1234567890" },
                    { "phone": "0901234568", "status": "success", "conversation_id": "uuid-...", "message_id": "1234567891" }
                  ]
                }
              }
            }
          },
          "202": {
            "description": "Đã chấp nhận (async)",
            "content": {
              "application/json": {
                "example": {
                  "success": true,
                  "campaign_id": "uuid-...",
                  "total": 10,
                  "accepted": 10,
                  "status": "processing",
                  "message": "Batch đang được xử lý nền. Dùng campaign_id để theo dõi tiến độ."
                }
              }
            }
          },
          "400": { "description": "Lỗi validate: thiếu trường, vượt quá giới hạn, type không hợp lệ" }
        }
      }
    },
    "/bots/{bot_id}/messages/send-by-phone": {
      "post": {
        "tags": ["Messages"],
        "summary": "Gửi Tin nhắn qua Số điện thoại",
        "description": "Tự động tra cứu số điện thoại Zalo, đồng bộ danh tính, khởi tạo hội thoại và gửi tin nhắn. Hỗ trợ đầy đủ các định dạng văn bản, hình ảnh, video, âm thanh.",
        "parameters": [
          {
            "name": "bot_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "UUID của Bot"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["phone", "content"],
                "properties": {
                  "phone": {
                    "type": "string",
                    "description": "Số điện thoại của khách hàng (có thể sử dụng định dạng E.164)"
                  },
                  "attachment_url": {
                    "type": "string",
                    "description": "URL công khai trỏ đến tài nguyên media. Bắt buộc với type=image/video/audio/voice."
                  },
                  "content": {
                    "type": "object",
                    "required": ["type", "data"],
                    "properties": {
                      "type": {
                        "type": "string",
                        "enum": ["text", "image", "video", "audio", "voice"],
                        "description": "Loại tin nhắn."
                      },
                      "data": {
                        "type": "object",
                        "description": "Dữ liệu theo từng loại tin nhắn."
                      }
                    }
                  }
                }
              },
              "examples": {
                "text": {
                  "summary": "Gửi văn bản",
                  "value": {
                    "phone": "0901234567",
                    "content": { "type": "text", "data": { "text": "Xin chào! Mình có thể giúp gì cho bạn?" } }
                  }
                },
                "image": {
                  "summary": "Gửi ảnh qua URL",
                  "value": {
                    "phone": "0901234567",
                    "content": { "type": "image", "data": { "caption": "Hình ảnh sản phẩm", "fileName": "product.jpg" } },
                    "attachment_url": "https://example.com/product.jpg"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Gửi thành công",
            "content": {
              "application/json": {
                "example": { "success": true, "message_id": "1234567890", "conversation_id": "uuid-...", "external_uid": "9186..." }
              }
            }
          },
          "400": { "description": "Thiếu trường bắt buộc hoặc type không hợp lệ" },
          "404": { "description": "Không tìm thấy người dùng Zalo qua Số điện thoại" },
          "503": { "description": "Bot đang offline" }
        }
      }
    },
    "/bots/{bot_id}/customers/{zalo_id}": {
      "get": {
        "tags": ["CRM"],
        "summary": "Lấy thông tin Khách hàng trực tiếp (Live)",
        "description": "Lấy thông tin live của một khách hàng theo `identity_id`.",
        "parameters": [
          {
            "name": "bot_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "UUID của Bot"
          },
          {
            "name": "zalo_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" },
            "description": "UUID của identity (identity_id)."
          }
        ],
        "responses": {
          "200": {
            "description": "Thông tin khách hàng",
            "content": {
              "application/json": {
                "example": {
                  "success": true,
                  "data": {
                    "identity_id": "uuid-identity",
                    "external_uid": "9186543210987654",
                    "live_profile": { "displayName": "Nguyễn Văn A" },
                    "connection": { "nickname": "Anh A VIP", "is_friend": true }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/bots/{bot_id}/customers/by-uid/{uid}": {
      "get": {
        "tags": ["CRM"],
        "summary": "Tra cứu Khách hàng theo UID Zalo",
        "parameters": [
          { "name": "bot_id", "in": "path", "required": true, "schema": { "type": "string" } },
          { "name": "uid", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": { "200": { "description": "Thành công" } }
      }
    },
    "/bots/{bot_id}/customers/by-phone": {
      "post": {
        "tags": ["CRM"],
        "summary": "Tra cứu Khách hàng theo Số điện thoại",
        "parameters": [
          { "name": "bot_id", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "type": "object", "required": ["phone"], "properties": { "phone": { "type": "string" } } }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/json": {
                "example": {
                  "success": true,
                  "data": {
                    "phone": "0901234567",
                    "external_uid": "9186543210987654",
                    "identity_id": "uuid-...",
                    "live_profile": { "displayName": "Nguyễn Văn A" },
                    "connection": { "nickname": "Anh A VIP", "is_friend": true }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/webhooks": {
      "get": {
        "tags": ["Webhooks"],
        "summary": "Danh sách Webhooks",
        "responses": {
          "200": {
            "description": "Thành công",
            "content": {
              "application/json": { "example": { "success": true, "data": [] } }
            }
          }
        }
      },
      "post": {
        "tags": ["Webhooks"],
        "summary": "Đăng ký Webhook mới",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "type": "object", "required": ["hook_url"], "properties": { "hook_url": { "type": "string" }, "events": { "type": "array", "items": { "type": "string" } } } }
            }
          }
        },
        "responses": { "201": { "description": "Thành công" } }
      }
    },
    "/webhooks/{webhook_id}": {
      "put": {
        "tags": ["Webhooks"],
        "summary": "Cập nhật Webhook",
        "parameters": [
          { "name": "webhook_id", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": { "200": { "description": "Thành công" } }
      },
      "delete": {
        "tags": ["Webhooks"],
        "summary": "Xóa Webhook",
        "parameters": [
          { "name": "webhook_id", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": { "200": { "description": "Thành công" } }
      }
    }
  },
  "webhooks": {
    "message.new": {
      "post": {
        "summary": "Sự kiện Tin nhắn mới",
        "responses": { "200": { "description": "OK" } }
      }
    },
    "friend.request.received": {
      "post": {
        "summary": "Sự kiện Nhận lời mời kết bạn",
        "responses": { "200": { "description": "OK" } }
      }
    },
    "friend.request.accepted": {
      "post": {
        "summary": "Sự kiện Chấp nhận kết bạn",
        "responses": { "200": { "description": "OK" } }
      }
    }
  }
}
