{
  "openapi": "3.0.1",
  "info": {
    "title": "ChatDash Webhook API Documentation",
    "description": "API Documentation for the ChatDash Webhook Subscription System",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://api.chat-dash.com/v1/public"
    }
  ],
  "paths": {
    "/webhooks/subscriptions": {
      "post": {
        "tags": ["Webhooks"],
        "summary": "Create Webhook Subscription",
        "description": "Create a new webhook subscription for receiving event notifications",
        "parameters": [
          {
            "name": "Authorization",
            "in": "header",
            "required": true,
            "description": "Client-specific API key for authentication",
            "schema": {
              "type": "string",
              "pattern": "^CD\\.CL\\."
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "clientId": {
                    "type": "string",
                    "description": "The client ID associated with the subscription"
                  },
                  "agentIds": {
                    "type": "array",
                    "description": "The agent IDs associated with the subscription",
                    "items": {
                      "type": "string"
                    },
                    "minItems": 1
                  },
                  "webhookUrl": {
                    "type": "string",
                    "description": "The URL where event notifications will be sent",
                    "format": "uri"
                  },
                  "events": {
                    "type": "array",
                    "description": "List of events to subscribe to",
                    "items": {
                      "type": "string",
                      "enum": [
                        "call.started",
                        "call.missed",
                        "call.ended",
                        "call.inbound"
                      ]
                    },
                    "minItems": 1
                  }
                },
                "required": ["clientId", "agentIds", "webhookUrl", "events"]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Subscription created successfully",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WebhookSubscription"
                  }
                }
              }
            }
          },
          "400": {
            "description": "Invalid request parameters",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Internal server error",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      },
      "get": {
        "tags": ["Webhooks"],
        "summary": "List Webhook Subscriptions",
        "description": "Get all webhook subscriptions for a client",
        "parameters": [
          {
            "name": "Authorization",
            "in": "header",
            "required": true,
            "description": "Client-specific API key for authentication",
            "schema": {
              "type": "string",
              "pattern": "^CD\\.CL\\."
            }
          },
          {
            "name": "clientId",
            "in": "query",
            "required": true,
            "description": "Filter subscriptions by client ID",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "List of webhook subscriptions",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WebhookSubscription"
                  }
                }
              }
            }
          }
        }
      }
    },
    "/webhooks/subscriptions/{subscriptionId}": {
      "get": {
        "tags": ["Webhooks"],
        "summary": "Get Webhook Subscription",
        "description": "Get a specific webhook subscription by ID",
        "parameters": [
          {
            "name": "Authorization",
            "in": "header",
            "required": true,
            "description": "Client-specific API key for authentication",
            "schema": {
              "type": "string",
              "pattern": "^CD\\.CL\\."
            }
          },
          {
            "name": "subscriptionId",
            "in": "path",
            "required": true,
            "description": "Webhook subscription ID",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Webhook subscription details",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WebhookSubscription"
                }
              }
            }
          },
          "404": {
            "description": "Subscription not found"
          }
        }
      },
      "delete": {
        "tags": ["Webhooks"],
        "summary": "Delete Webhook Subscription",
        "description": "Delete a specific webhook subscription",
        "parameters": [
          {
            "name": "Authorization",
            "in": "header",
            "required": true,
            "description": "Client-specific API key for authentication",
            "schema": {
              "type": "string",
              "pattern": "^CD\\.CL\\."
            }
          },
          {
            "name": "subscriptionId",
            "in": "path",
            "required": true,
            "description": "Webhook subscription ID",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "204": {
            "description": "Subscription deleted successfully"
          },
          "404": {
            "description": "Subscription not found"
          }
        }
      }
    },
    "/webhooks/subscriptions/{subscriptionId}/events": {
      "put": {
        "tags": ["Webhooks"],
        "summary": "Update Subscription Events",
        "description": "Update events for a specific webhook subscription",
        "parameters": [
          {
            "name": "Authorization",
            "in": "header",
            "required": true,
            "description": "Client-specific API key for authentication",
            "schema": {
              "type": "string",
              "pattern": "^CD\\.CL\\."
            }
          },
          {
            "name": "subscriptionId",
            "in": "path",
            "required": true,
            "description": "Webhook subscription ID",
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "add": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "enum": [
                        "call.started",
                        "call.missed",
                        "call.ended",
                        "call.inbound"
                      ]
                    }
                  },
                  "remove": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "enum": [
                        "call.started",
                        "call.missed",
                        "call.ended",
                        "call.inbound"
                      ]
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Events updated successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WebhookSubscription"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "WebhookSubscription": {
        "type": "object",
        "properties": {
          "_id": {
            "type": "string",
            "description": "Unique identifier for the subscription"
          },
          "clientId": {
            "type": "string",
            "description": "The client associated with the subscription"
          },
          "agentId": {
            "type": "string",
            "description": "The agent associated with the subscription"
          },
          "webhookUrl": {
            "type": "string",
            "description": "The URL where event notifications will be sent"
          },
          "events": {
            "type": "array",
            "description": "List of events that trigger notifications",
            "items": {
              "type": "string",
              "enum": ["call.started", "call.missed", "call.ended", "call.inbound"]
            }
          },
          "createdAt": {
            "type": "string",
            "format": "date-time",
            "description": "When the subscription was created"
          },
          "updatedAt": {
            "type": "string",
            "format": "date-time",
            "description": "When the subscription was last updated"
          }
        }
      }
    }
  }
}
