{
  "openapi": "3.0.3",
  "info": {
    "title": "Chatvia Merchant Integration API",
    "version": "1.1.0",
    "description": "İşletme bazlı merchant anahtarıyla mağaza bağlantısı. Mevcut plan ücretsizdir. Sırlar yalnızca sunucu tarafında kullanılır."
  },
  "servers": [
    {
      "url": "https://chatvia.com.tr/api/v1"
    }
  ],
  "security": [
    {
      "MerchantKey": []
    }
  ],
  "paths": {
    "/merchant": {
      "get": {
        "operationId": "verifyMerchant",
        "summary": "Merchant anahtarını ve işletmeyi doğrula",
        "responses": {
          "200": {
            "description": "Başarılı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Merchant"
                }
              }
            }
          },
          "401": {
            "description": "Geçersiz veya yenilenmiş merchant anahtarı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Kurulum bu işletmede bulunamadı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Mağaza başka işletmeye bağlı veya eşzamanlılık çakışması",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "422": {
            "description": "Alan veya altyapı kodu geçersiz",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Hız sınırı aşıldı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/integrations/{provider}/connect": {
      "post": {
        "operationId": "connectStore",
        "summary": "Mağazayı bağla veya tekrar etkinleştir",
        "parameters": [
          {
            "name": "provider",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[a-z][a-z0-9-]{1,31}$"
            },
            "example": "marvigo"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ConnectRequest"
              },
              "example": {
                "store_id": "marvigo-1042",
                "store_name": "Örnek Mağaza",
                "store_url": "https://magazam.example.com"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Mevcut bağlantı / yeniden etkinleştirme",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ConnectResponse"
                }
              }
            }
          },
          "201": {
            "description": "Yeni bağlantı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ConnectResponse"
                }
              }
            }
          },
          "401": {
            "description": "Geçersiz veya yenilenmiş merchant anahtarı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Kurulum bu işletmede bulunamadı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Mağaza başka işletmeye bağlı veya eşzamanlılık çakışması",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "422": {
            "description": "Alan veya altyapı kodu geçersiz",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Hız sınırı aşıldı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/integrations/{provider}/stores/{storeId}": {
      "get": {
        "operationId": "getStoreConnection",
        "summary": "İşletmenin kendi mağazasının bağlantı durumunu oku",
        "parameters": [
          {
            "name": "provider",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[a-z][a-z0-9-]{1,31}$"
            },
            "example": "marvigo"
          },
          {
            "name": "storeId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$"
            },
            "example": "marvigo-1042"
          }
        ],
        "responses": {
          "200": {
            "description": "Başarılı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Installation"
                }
              }
            }
          },
          "401": {
            "description": "Geçersiz veya yenilenmiş merchant anahtarı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Kurulum bu işletmede bulunamadı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Mağaza başka işletmeye bağlı veya eşzamanlılık çakışması",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "422": {
            "description": "Alan veya altyapı kodu geçersiz",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Hız sınırı aşıldı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "delete": {
        "operationId": "disconnectStore",
        "summary": "Bağlantıyı kes; başka aktif bağlantı kalmadıysa sitenin widgetını durdur",
        "parameters": [
          {
            "name": "provider",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[a-z][a-z0-9-]{1,31}$"
            },
            "example": "marvigo"
          },
          {
            "name": "storeId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$"
            },
            "example": "marvigo-1042"
          }
        ],
        "responses": {
          "200": {
            "description": "Başarılı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Disconnected"
                }
              }
            }
          },
          "401": {
            "description": "Geçersiz veya yenilenmiş merchant anahtarı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Kurulum bu işletmede bulunamadı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Mağaza başka işletmeye bağlı veya eşzamanlılık çakışması",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "422": {
            "description": "Alan veya altyapı kodu geçersiz",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Hız sınırı aşıldı",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "MerchantKey": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "cv_live_ + 64 hexadecimal characters",
        "description": "Chatvia panelinden alınan gizli merchant anahtarı. URL veya ziyaretçi HTML/JS kodunda kullanmayın."
      }
    },
    "schemas": {
      "ConnectRequest": {
        "type": "object",
        "required": [
          "store_id",
          "store_name",
          "store_url"
        ],
        "properties": {
          "store_id": {
            "type": "string",
            "maxLength": 128,
            "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$"
          },
          "store_name": {
            "type": "string",
            "maxLength": 160
          },
          "store_url": {
            "type": "string",
            "format": "uri",
            "maxLength": 255,
            "description": "HTTPS kök adresi; yol, sorgu, fragment ve kullanıcı bilgisi olmadan."
          }
        }
      },
      "Merchant": {
        "type": "object",
        "required": [
          "merchant",
          "plan",
          "api_version"
        ],
        "properties": {
          "merchant": {
            "type": "object",
            "required": [
              "id",
              "name"
            ],
            "properties": {
              "id": {
                "type": "string"
              },
              "name": {
                "type": "string"
              }
            }
          },
          "plan": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "enum": [
                  "free"
                ]
              },
              "name": {
                "type": "string",
                "example": "Ücretsiz"
              },
              "price": {
                "type": "integer",
                "enum": [
                  0
                ]
              },
              "currency": {
                "type": "string",
                "example": "TRY"
              }
            }
          },
          "api_version": {
            "type": "string",
            "example": "v1"
          }
        }
      },
      "Website": {
        "type": "object",
        "required": [
          "id",
          "name",
          "domain",
          "enabled",
          "public_key"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "domain": {
            "type": "string"
          },
          "enabled": {
            "type": "boolean"
          },
          "public_key": {
            "type": "string",
            "pattern": "^site_[A-Za-z0-9]{32}$"
          }
        }
      },
      "Widget": {
        "type": "object",
        "required": [
          "script_url",
          "website_id",
          "embed_html"
        ],
        "properties": {
          "script_url": {
            "type": "string",
            "format": "uri",
            "example": "https://chatvia.com.tr/embed.js?v=1.1.0"
          },
          "website_id": {
            "type": "string",
            "pattern": "^site_[A-Za-z0-9]{32}$"
          },
          "embed_html": {
            "type": "string",
            "description": "Yalnızca açık site kodu içerir; merchant anahtarı içermez."
          }
        }
      },
      "Installation": {
        "type": "object",
        "required": [
          "installation_id",
          "provider",
          "store_id",
          "status",
          "website",
          "widget",
          "dashboard_url"
        ],
        "properties": {
          "installation_id": {
            "type": "string"
          },
          "provider": {
            "type": "string"
          },
          "store_id": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "connected",
              "disconnected"
            ]
          },
          "website": {
            "$ref": "#/components/schemas/Website"
          },
          "widget": {
            "$ref": "#/components/schemas/Widget"
          },
          "dashboard_url": {
            "type": "string",
            "format": "uri"
          }
        }
      },
      "ConnectResponse": {
        "allOf": [
          {
            "$ref": "#/components/schemas/Installation"
          },
          {
            "type": "object",
            "required": [
              "already_connected"
            ],
            "properties": {
              "already_connected": {
                "type": "boolean",
                "description": "Bağlantı kaydı daha önce varsa true."
              }
            }
          }
        ]
      },
      "Disconnected": {
        "type": "object",
        "required": [
          "installation_id",
          "status"
        ],
        "properties": {
          "installation_id": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "disconnected"
            ]
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "message": {
            "type": "string"
          },
          "errors": {
            "type": "object",
            "additionalProperties": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          },
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              }
            }
          }
        },
        "description": "401 için error.code/error.message; doğrulama ve diğer hatalar için message ve gerektiğinde errors. 429 yanıtında Retry-After başlığını kullanın."
      }
    }
  }
}