{
  "openapi": "3.1.0",
  "info": {
    "title": "MITATE 連携 API",
    "version": "1.0.0",
    "description": "外部のシステムや AI から、MITATE の効果・判定を読み、訴求案の下書きを作る API（ADR 0016）。伴走プランの組織だけが使えます。公開は管理画面で人が承認します。"
  },
  "servers": [{ "url": "https://mitate-app.cytra.jp/ext/v1" }],
  "security": [{ "bearerAuth": [] }],
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "組織の API キー（mk_live_…）。Authorization: Bearer <key>。管理画面の『組織の設定 → API・MCP』で発行します。『読む』キーは読む 4 つ、『読む＋下書き』キーは下書きも使えます。"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": { "error": { "type": "string" }, "message": { "type": "string" } }
      },
      "Effect": {
        "type": "object",
        "properties": {
          "test": {
            "type": "object",
            "properties": {
              "visits": { "type": "integer" },
              "conversions": { "type": "integer" },
              "rate": { "type": ["number", "null"] }
            }
          },
          "control": {
            "type": "object",
            "properties": {
              "visits": { "type": "integer" },
              "conversions": { "type": "integer" },
              "rate": { "type": ["number", "null"] }
            }
          },
          "diff": { "type": "number" },
          "verdict": { "type": "string", "enum": ["better", "worse", "unclear", "few", "none"] }
        }
      }
    }
  },
  "paths": {
    "/sites": {
      "get": {
        "summary": "サイト一覧",
        "description": "鍵の組織のサイトを返す。",
        "responses": {
          "200": {
            "description": "サイトの配列",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "sites": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": { "type": "string" },
                          "name": { "type": "string" },
                          "url": { "type": "string" },
                          "enabled": { "type": "boolean" },
                          "created_at": { "type": "string", "format": "date-time" }
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "鍵がない／無効",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
          },
          "403": {
            "description": "この組織では連携 API を使えません",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
          },
          "429": { "description": "レート上限（組織ごと 読む 120/分）" }
        }
      }
    },
    "/sites/{id}/effect": {
      "get": {
        "summary": "効果の概要",
        "description": "直近 30 日・7 日の、出し分けた人（test）と比べる群（control）の訪問・申し込み・率・差の見立て。関心ごとの申し込み率も返す。",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": {
          "200": {
            "description": "効果",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "site_id": { "type": "string" },
                    "last_30_days": { "$ref": "#/components/schemas/Effect" },
                    "last_7_days": { "$ref": "#/components/schemas/Effect" },
                    "by_concern": { "type": "array", "items": { "type": "object" } }
                  }
                }
              }
            }
          },
          "404": { "description": "見つからない" }
        }
      }
    },
    "/sites/{id}/judgments": {
      "get": {
        "summary": "判定ログ（要約）",
        "description": "判定の要約を新しい順に返す。訪問者の相談の文面は含まない。ページングは before（この時刻より前）と limit（最大 200）。",
        "parameters": [
          { "name": "id", "in": "path", "required": true, "schema": { "type": "string" } },
          {
            "name": "before",
            "in": "query",
            "schema": { "type": "integer" },
            "description": "この時刻（ミリ秒）より前の分を返す"
          },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 50, "maximum": 200 } }
        ],
        "responses": {
          "200": {
            "description": "判定の要約",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "judgments": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": { "type": "string" },
                          "at": { "type": "string", "format": "date-time" },
                          "trigger": { "type": "string", "enum": ["action", "chat"] },
                          "concern": { "type": ["string", "null"] },
                          "changed": { "type": "boolean" },
                          "ok": { "type": "boolean" }
                        }
                      }
                    },
                    "next": { "type": ["integer", "null"], "description": "次のページの before。null なら最後" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/sites/{id}/usage": {
      "get": {
        "summary": "使用量",
        "description": "今月の判定の回数と上限、戻る日。",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": {
          "200": {
            "description": "使用量",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "site_id": { "type": "string" },
                    "month_start": { "type": "string" },
                    "resets_at": { "type": "string", "format": "date-time" },
                    "judgments": {
                      "type": "object",
                      "properties": { "used": { "type": "integer" }, "limit": { "type": ["integer", "null"] } }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/sites/{id}/drafts": {
      "post": {
        "summary": "訴求案の下書きを作る",
        "description": "関心を指定すると、AI が見出し・説明などの案を作り、承認待ちとして入れる。公開は管理画面で人が承認する。『読む＋下書き』のキーが要る。AI の下書きの月の上限に数える。",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "concerns": {
                    "type": "array",
                    "items": { "type": "string" },
                    "description": "関心の key（省略なら全部）"
                  },
                  "count": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 3,
                    "default": 2,
                    "description": "関心ごとの案の数"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "承認待ちの案が入った",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "drafts": { "type": "array", "items": { "type": "object" } },
                    "message": { "type": "string" }
                  }
                }
              }
            }
          },
          "403": {
            "description": "『読む』キーでは下書きを作れない",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
          },
          "429": { "description": "レート上限（下書き 20/分）か、月の AI の上限" }
        }
      }
    }
  }
}
