{
  "openapi": "3.1.0",
  "info": {
    "title": "AKITECTURE Estate Analyst API",
    "version": "0.1.0",
    "description": "不動産投資の決定論的収支計算、Google Maps周辺情報、国土交通省不動産情報ライブラリ、共有Drive内の正規エクスポート売買事例を統合し、出典台帳付きレポートを生成します。"
  },
  "servers": [{ "url": "https://akitecture-estate-analyst.pages.dev" }],
  "security": [{ "ApiKeyAuth": [] }],
  "paths": {
    "/api/health": {
      "get": {
        "operationId": "getServiceHealth",
        "summary": "サービスと外部データ設定の状態を確認する",
        "security": [],
        "x-openai-isConsequential": false,
        "responses": { "200": { "description": "Health status" } }
      }
    },
    "/api/v1/calculate": {
      "post": {
        "operationId": "calculateInvestment",
        "summary": "外部APIを使わず不動産投資KPIと感応度を計算する",
        "description": "物件価格、賃料、経費、融資条件からNOI、DSCR、LTV、税引前CF、自己資金利回りなどを再現可能な式で計算します。",
        "x-openai-isConsequential": false,
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/InvestmentRequest" } } } },
        "responses": { "200": { "description": "Calculation result" }, "400": { "description": "Invalid input" } }
      }
    },
    "/api/v1/context": {
      "post": {
        "operationId": "fetchMarketContext",
        "summary": "Google Maps、国交省API、共有Driveから市場コンテキストを一度に取得する",
        "description": "同じ条件で重複呼び出しせず、この操作を一回だけ使って周辺施設、公的取引情報、正規に共有された売買事例を取得してください。取得失敗も結果に明示されます。",
        "x-openai-isConsequential": false,
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/MarketRequest" } } } },
        "responses": { "200": { "description": "Market context with source ledger" }, "400": { "description": "Invalid input" } }
      }
    },
    "/api/v1/report": {
      "post": {
        "operationId": "buildInvestmentReport",
        "summary": "計算結果と取得済みデータから標準Markdownレポートを生成する",
        "description": "既に取得・計算したデータを標準フォーマットへ整形します。外部データは再取得しません。",
        "x-openai-isConsequential": false,
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "type": "object", "additionalProperties": true } } } },
        "responses": { "200": { "description": "Evidence-led Markdown report" } }
      }
    },
    "/api/v1/analyze": {
      "post": {
        "operationId": "analyzeInvestment",
        "summary": "収支計算、外部データ取得、標準レポート生成を一回で実行する",
        "description": "通常はこちらを最優先で一回だけ呼び出します。物件・融資条件、取得範囲、市区町村コード等を渡すと、計算・外部データ・出典台帳・Markdownレポートをまとめて返します。",
        "x-openai-isConsequential": false,
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/AnalyzeRequest" } } } },
        "responses": { "200": { "description": "Complete analysis and report" }, "400": { "description": "Invalid input" } }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": { "type": "apiKey", "in": "header", "name": "X-AKITECTURE-KEY" }
    },
    "schemas": {
      "Property": {
        "type": "object",
        "required": ["asking_price_yen"],
        "properties": {
          "address": { "type": "string", "description": "日本国内の物件所在地" },
          "property_type": { "type": "string" },
          "asking_price_yen": { "type": "number", "exclusiveMinimum": 0 },
          "acquisition_costs_yen": { "type": "number", "minimum": 0 },
          "renovation_cost_yen": { "type": "number", "minimum": 0 },
          "monthly_rent_yen": { "type": "number", "minimum": 0 },
          "annual_other_income_yen": { "type": "number", "minimum": 0 },
          "occupancy_rate": { "type": "number", "description": "0.95または95のいずれでも可" },
          "annual_operating_expenses_yen": { "type": "number", "minimum": 0 },
          "operating_expense_ratio": { "type": "number", "description": "明示経費がない場合のみ利用" },
          "land_area_sqm": { "type": "number" },
          "floor_area_sqm": { "type": "number" },
          "building_year": { "type": ["string", "number"] },
          "latitude": { "type": "number" },
          "longitude": { "type": "number" }
        }
      },
      "Financing": {
        "type": "object",
        "properties": {
          "loan_amount_yen": { "type": "number", "minimum": 0 },
          "annual_interest_rate": { "type": "number", "description": "0.02または2のいずれでも可" },
          "term_years": { "type": "number", "minimum": 0 }
        }
      },
      "InvestmentRequest": {
        "type": "object",
        "required": ["property"],
        "properties": {
          "property": { "$ref": "#/components/schemas/Property" },
          "financing": { "$ref": "#/components/schemas/Financing" },
          "targets": { "type": "object", "additionalProperties": true }
        }
      },
      "MarketRequest": {
        "type": "object",
        "properties": {
          "address": { "type": "string" },
          "latitude": { "type": "number" },
          "longitude": { "type": "number" },
          "prefecture_code": { "type": "string", "pattern": "^\\d{2}$" },
          "city_code": { "type": "string", "pattern": "^\\d{5}$" },
          "years": { "type": "array", "maxItems": 5, "items": { "type": "integer" } },
          "quarters": { "type": "array", "items": { "type": "integer", "minimum": 1, "maximum": 4 } },
          "price_classification": { "type": "string", "enum": ["01", "02", "both"] },
          "radius_m": { "type": "number", "minimum": 100, "maximum": 50000 },
          "place_types": { "type": "array", "items": { "type": "string" } },
          "drive_query": { "type": "string", "description": "共有フォルダ内のファイル名検索語。住所・市区町村名など。" },
          "include": {
            "type": "object",
            "properties": {
              "google_maps": { "type": "boolean" },
              "reinfolib": { "type": "boolean" },
              "drive_comparables": { "type": "boolean" }
            }
          }
        }
      },
      "AnalyzeRequest": {
        "allOf": [
          { "$ref": "#/components/schemas/InvestmentRequest" },
          {
            "type": "object",
            "properties": {
              "market": { "$ref": "#/components/schemas/MarketRequest" },
              "sources": {
                "type": "object",
                "properties": {
                  "google_maps": { "type": "boolean" },
                  "reinfolib": { "type": "boolean" },
                  "drive_comparables": { "type": "boolean" }
                }
              },
              "report_options": { "type": "object", "additionalProperties": true }
            }
          }
        ]
      }
    }
  }
}
