{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "pdf-positioner-config",
  "title": "PDF Positioner Config",
  "description": "positioner.html が出力し pdf-positioner.js が読み込むマッピング設定",
  "type": "object",
  "properties": {
    "pdfPositioner": {
      "oneOf": [
        {
          "$ref": "#/$defs/configV1"
        },
        {
          "$ref": "#/$defs/configV2"
        }
      ]
    }
  },
  "required": [
    "pdfPositioner"
  ],
  "$defs": {
    "configV1": {
      "type": "object",
      "description": "v1 フラット構造 (version プロパティなし)",
      "properties": {
        "features": {
          "$ref": "#/$defs/features"
        },
        "detail": {
          "$ref": "#/$defs/modeConfig"
        },
        "index": {
          "$ref": "#/$defs/modeConfigWithRows"
        },
        "combined": {
          "$ref": "#/$defs/modeConfigCombined"
        }
      },
      "not": {
        "required": [
          "version"
        ]
      },
      "additionalProperties": false
    },
    "configV2": {
      "type": "object",
      "description": "v2 テンプレート構造。layouts 配下に複数テンプレートを定義",
      "properties": {
        "version": {
          "type": "integer",
          "const": 2
        },
        "features": {
          "$ref": "#/$defs/features"
        },
        "layouts": {
          "type": "object",
          "description": "テンプレートキーをキーとするレイアウト定義",
          "additionalProperties": {
            "$ref": "#/$defs/layoutEntry"
          },
          "minProperties": 1
        },
        "defaultFont": {
          "type": "object",
          "properties": {
            "url": {
              "type": "string",
              "description": "フォントファイルの絶対 https URL。"
            }
          },
          "required": [
            "url"
          ],
          "description": "views[].fontUrl が無い場合に使われるフォント。additionalProperties:false のため以前は指定できなかった。"
        }
      },
      "required": [
        "version",
        "layouts"
      ],
      "additionalProperties": false
    },
    "layoutEntry": {
      "type": "object",
      "description": "1 テンプレートの定義。label + 各モードの views を持つ",
      "properties": {
        "label": {
          "$ref": "#/$defs/templateLabel"
        },
        "views": {
          "type": "object",
          "properties": {
            "detail": {
              "$ref": "#/$defs/modeConfig"
            },
            "index": {
              "$ref": "#/$defs/modeConfigWithRows"
            },
            "combined": {
              "$ref": "#/$defs/modeConfigCombined"
            }
          },
          "additionalProperties": false
        }
      },
      "required": [
        "views"
      ]
    },
    "templateLabel": {
      "type": "object",
      "description": "多言語ラベル",
      "properties": {
        "ja": {
          "type": "string",
          "description": "日本語ラベル"
        },
        "en": {
          "type": "string",
          "description": "英語ラベル"
        }
      }
    },
    "features": {
      "type": "object",
      "properties": {
        "detail": {
          "type": "boolean",
          "default": true
        },
        "index": {
          "type": "boolean",
          "default": false
        },
        "batchExport": {
          "type": "boolean",
          "default": false
        },
        "combined": {
          "type": "boolean",
          "default": false
        }
      },
      "additionalProperties": false
    },
    "modeConfig": {
      "type": "object",
      "properties": {
        "templateUrl": {
          "type": "string",
          "description": "PDF テンプレート URL。__blank__, __blank_1__, __blank_2__ で空白 PDF 生成"
        },
        "fontUrl": {
          "type": "string",
          "description": "フォント URL (TTF)"
        },
        "outputFileName": {
          "type": "string",
          "default": "output_{レコード番号}.pdf",
          "description": "{レコード番号} トークンがレコード ID に置換される"
        },
        "mappings": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/mapping"
          },
          "default": []
        },
        "subtables": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/subtable"
          },
          "default": []
        },
        "relatedRecords": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/relatedRecord"
          },
          "default": []
        },
        "staticTexts": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/staticText"
          },
          "default": []
        },
        "imageMappings": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/imageMapping"
          },
          "default": []
        },
        "flowBoxes": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/flowBox"
          },
          "default": [],
          "description": "動的Y座標アンカー配置ボックス"
        }
      },
      "required": [
        "templateUrl",
        "fontUrl"
      ]
    },
    "modeConfigWithRows": {
      "allOf": [
        {
          "$ref": "#/$defs/modeConfig"
        },
        {
          "type": "object",
          "properties": {
            "rowPitch": {
              "type": "number",
              "default": 10,
              "description": "行間隔 (mm)"
            },
            "rowCount": {
              "type": "integer",
              "default": 0,
              "description": "ページあたり最大行数 (0=無制限)"
            },
            "pageRowSettings": {
              "$ref": "#/$defs/pageRowSettings"
            }
          }
        }
      ],
      "properties": {
        "columnCount": {
          "type": "integer",
          "minimum": 1,
          "description": "1ページあたりの列数。既定 1。"
        },
        "columnPitch": {
          "type": "number",
          "description": "列間の水平ピッチ(mm)。columnCount > 1 のとき使う。"
        }
      }
    },
    "modeConfigCombined": {
      "allOf": [
        {
          "$ref": "#/$defs/modeConfigWithRows"
        },
        {
          "type": "object",
          "properties": {
            "indexMappings": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/mapping"
              },
              "default": [],
              "description": "行フィールドマッピング (rowPitch で繰り返される)"
            }
          }
        }
      ]
    },
    "mapping": {
      "type": "object",
      "properties": {
        "fieldCode": {
          "type": "string",
          "description": "kintone フィールドコード"
        },
        "page": {
          "type": "integer",
          "default": 0,
          "description": "0-indexed ページ番号"
        },
        "x": {
          "type": "number",
          "description": "X 位置 (mm)"
        },
        "y": {
          "type": "number",
          "description": "Y 位置 (mm)"
        },
        "fontSize": {
          "type": "number",
          "default": 10,
          "description": "フォントサイズ (pt)"
        },
        "align": {
          "type": "string",
          "enum": [
            "left",
            "center",
            "right"
          ],
          "default": "left"
        },
        "maxWidth": {
          "type": "number",
          "description": "最大幅 (mm)"
        },
        "maxHeight": {
          "type": "number",
          "description": "最大高さ (mm)"
        },
        "overflow": {
          "type": "string",
          "enum": [
            "hidden",
            "wrap",
            "shrink"
          ],
          "default": "hidden"
        },
        "format": {
          "type": "string",
          "description": "日付フォーマット等"
        },
        "prefix": {
          "type": "string",
          "default": "",
          "description": "前置テキスト"
        },
        "postfix": {
          "type": "string",
          "default": "",
          "description": "後置テキスト"
        },
        "color": {
          "type": "string",
          "default": "#000000",
          "pattern": "^#[0-9a-fA-F]{6}$",
          "description": "テキスト色 (hex)"
        },
        "vAlign": {
          "$ref": "#/$defs/vAlign"
        },
        "bold": {
          "type": "boolean"
        },
        "italic": {
          "type": "boolean"
        },
        "underline": {
          "type": "boolean"
        },
        "strikethrough": {
          "type": "boolean"
        }
      },
      "required": [
        "fieldCode",
        "x",
        "y"
      ]
    },
    "column": {
      "type": "object",
      "properties": {
        "fieldCode": {
          "type": "string",
          "description": "テーブル内フィールドコード"
        },
        "x": {
          "type": "number",
          "description": "X 位置 (mm)"
        },
        "startY": {
          "type": "number",
          "description": "Y 開始位置 (mm)。省略時は親から継承"
        },
        "rowHeight": {
          "type": "number",
          "description": "行高さ (mm)。省略時は親から継承"
        },
        "maxRows": {
          "type": "integer",
          "description": "最大行数。省略時は親から継承"
        },
        "fontSize": {
          "type": "number",
          "default": 10
        },
        "align": {
          "type": "string",
          "enum": [
            "left",
            "center",
            "right"
          ],
          "default": "left"
        },
        "maxWidth": {
          "type": "number"
        },
        "maxHeight": {
          "type": "number"
        },
        "overflow": {
          "type": "string",
          "enum": [
            "hidden",
            "wrap",
            "shrink"
          ],
          "default": "hidden"
        },
        "format": {
          "type": "string"
        },
        "prefix": {
          "type": "string",
          "default": ""
        },
        "postfix": {
          "type": "string",
          "default": ""
        },
        "color": {
          "type": "string",
          "default": "#000000",
          "pattern": "^#[0-9a-fA-F]{6}$"
        },
        "vAlign": {
          "$ref": "#/$defs/vAlign"
        },
        "bold": {
          "type": "boolean"
        },
        "italic": {
          "type": "boolean"
        },
        "underline": {
          "type": "boolean"
        },
        "strikethrough": {
          "type": "boolean"
        }
      },
      "required": [
        "fieldCode",
        "x"
      ]
    },
    "subtable": {
      "type": "object",
      "properties": {
        "tableFieldCode": {
          "type": "string",
          "description": "サブテーブルフィールドコード"
        },
        "page": {
          "type": "integer",
          "default": 0
        },
        "startY": {
          "type": "number",
          "description": "開始 Y 位置 (mm)"
        },
        "rowHeight": {
          "type": "number",
          "description": "行高さ (mm)"
        },
        "maxRows": {
          "type": "integer",
          "default": 15,
          "description": "最大表示行数"
        },
        "columns": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/column"
          }
        }
      },
      "required": [
        "tableFieldCode",
        "startY",
        "rowHeight",
        "columns"
      ]
    },
    "relatedRecord": {
      "type": "object",
      "properties": {
        "referenceTableFieldCode": {
          "type": "string",
          "description": "関連レコードフィールドコード"
        },
        "page": {
          "type": "integer",
          "default": 0
        },
        "startY": {
          "type": "number",
          "description": "開始 Y 位置 (mm)"
        },
        "rowHeight": {
          "type": "number",
          "description": "行高さ (mm)"
        },
        "maxRows": {
          "type": "integer",
          "default": 10,
          "description": "最大表示行数"
        },
        "columns": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/column"
          }
        }
      },
      "required": [
        "referenceTableFieldCode",
        "startY",
        "rowHeight",
        "columns"
      ]
    },
    "staticText": {
      "type": "object",
      "properties": {
        "text": {
          "type": "string",
          "description": "固定テキスト内容 (\\n 改行対応)"
        },
        "page": {
          "type": "integer",
          "default": 0
        },
        "x": {
          "type": "number",
          "description": "X 位置 (mm)"
        },
        "y": {
          "type": "number",
          "description": "Y 位置 (mm)"
        },
        "fontSize": {
          "type": "number",
          "default": 10
        },
        "align": {
          "type": "string",
          "enum": [
            "left",
            "center",
            "right"
          ],
          "default": "left"
        },
        "maxWidth": {
          "type": "number"
        },
        "maxHeight": {
          "type": "number"
        },
        "overflow": {
          "type": "string",
          "enum": [
            "hidden",
            "wrap"
          ],
          "default": "hidden",
          "description": "shrink は静的テキスト非対応"
        },
        "color": {
          "type": "string",
          "default": "#000000",
          "pattern": "^#[0-9a-fA-F]{6}$"
        },
        "bold": {
          "type": "boolean",
          "default": false,
          "description": "擬似太字 (フォント非依存)"
        },
        "italic": {
          "type": "boolean",
          "default": false,
          "description": "擬似斜体 (~12°)"
        },
        "underline": {
          "type": "boolean",
          "default": false,
          "description": "下線"
        },
        "strikethrough": {
          "type": "boolean",
          "default": false,
          "description": "取消線"
        },
        "vAlign": {
          "$ref": "#/$defs/vAlign"
        }
      },
      "required": [
        "text",
        "x",
        "y"
      ]
    },
    "imageMapping": {
      "type": "object",
      "properties": {
        "fieldCode": {
          "type": "string",
          "description": "添付ファイルフィールドコード"
        },
        "page": {
          "type": "integer",
          "default": 0
        },
        "x": {
          "type": "number",
          "description": "X 位置 (mm)"
        },
        "y": {
          "type": "number",
          "description": "Y 位置 (mm)"
        },
        "width": {
          "type": "number",
          "default": 30,
          "description": "表示幅 (mm)"
        },
        "height": {
          "type": "number",
          "default": 30,
          "description": "表示高さ (mm)"
        },
        "objectFit": {
          "type": "string",
          "enum": [
            "contain",
            "fill",
            "cover"
          ],
          "default": "contain"
        }
      },
      "required": [
        "fieldCode",
        "x",
        "y"
      ]
    },
    "pageRowSettings": {
      "type": "object",
      "description": "ページ別オーバーライド。キーはページ番号文字列 (例: \"0\", \"1\")",
      "additionalProperties": {
        "type": "object",
        "properties": {
          "rowPitch": {
            "type": "number",
            "description": "そのページの行間隔 (mm)"
          },
          "rowCount": {
            "type": "integer",
            "description": "そのページの最大行数"
          },
          "restart": {
            "type": "boolean",
            "default": false,
            "description": "true ならレコードオフセットを 0 にリセット"
          }
        },
        "required": [
          "rowPitch",
          "rowCount"
        ]
      }
    },
    "flowBox": {
      "type": "object",
      "description": "動的Y座標アンカー配置ボックス。items を上から順に配置し、高さに応じてY位置が動的に決まる。",
      "properties": {
        "id": {
          "type": "string",
          "description": "識別用ID（任意）"
        },
        "page": {
          "type": "integer",
          "default": 0,
          "description": "開始ページ (0始まり)"
        },
        "x": {
          "type": "number",
          "description": "ボックス左端X (mm)"
        },
        "y": {
          "type": "number",
          "description": "ボックス上端Y (mm)"
        },
        "width": {
          "type": "number",
          "description": "ボックス幅 (mm)"
        },
        "maxHeight": {
          "type": "number",
          "description": "1ページあたり最大高さ (mm)"
        },
        "pageBreak": {
          "$ref": "#/$defs/flowBoxPageBreak"
        },
        "items": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/flowBoxItem"
          }
        }
      },
      "required": [
        "x",
        "y",
        "width",
        "items"
      ]
    },
    "flowBoxPageBreak": {
      "type": "object",
      "description": "FlowBox のページ跨ぎ設定",
      "properties": {
        "enabled": {
          "type": "boolean",
          "default": true,
          "description": "ページ跨ぎを有効にする"
        },
        "nextPageTemplate": {
          "type": "string",
          "enum": [
            "same",
            "blank"
          ],
          "default": "same",
          "description": "次ページのテンプレート"
        },
        "restartX": {
          "type": "number",
          "description": "次ページでのX開始位置 (mm)。省略時は1ページ目と同じ"
        },
        "restartY": {
          "type": "number",
          "description": "次ページでのY開始位置 (mm)。省略時は1ページ目と同じ"
        },
        "restartWidth": {
          "type": "number",
          "description": "次ページでのボックス幅 (mm)。省略時は1ページ目と同じ"
        },
        "restartMaxHeight": {
          "type": "number",
          "description": "次ページでの最大高さ (mm)。省略時は1ページ目と同じ"
        }
      }
    },
    "flowBoxItem": {
      "type": "object",
      "description": "FlowBox 内の配置アイテム",
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "mapping",
            "subtable",
            "staticText"
          ],
          "description": "アイテム種別"
        },
        "marginBottom": {
          "type": "number",
          "default": 0,
          "description": "下マージン (mm)"
        },
        "offsetX": {
          "type": "number",
          "default": 0,
          "description": "ボックスX基準からの相対位置 (mm)"
        },
        "fieldCode": {
          "type": "string",
          "description": "kintoneフィールドコード (type=mapping時)"
        },
        "tableFieldCode": {
          "type": "string",
          "description": "サブテーブルフィールドコード (type=subtable時)"
        },
        "text": {
          "type": "string",
          "description": "固定テキスト (type=staticText時)"
        },
        "fontSize": {
          "type": "number",
          "default": 10,
          "description": "フォントサイズ (pt)"
        },
        "align": {
          "type": "string",
          "enum": [
            "left",
            "center",
            "right"
          ],
          "default": "left"
        },
        "maxWidth": {
          "type": "number",
          "description": "最大幅 (mm)"
        },
        "overflow": {
          "type": "string",
          "enum": [
            "hidden",
            "wrap"
          ],
          "default": "hidden"
        },
        "format": {
          "type": "string",
          "description": "値フォーマット"
        },
        "prefix": {
          "type": "string",
          "default": "",
          "description": "前置テキスト"
        },
        "postfix": {
          "type": "string",
          "default": "",
          "description": "後置テキスト"
        },
        "color": {
          "type": "string",
          "default": "#000000",
          "pattern": "^#[0-9a-fA-F]{6}$",
          "description": "テキスト色 (hex)"
        },
        "bold": {
          "type": "boolean",
          "default": false,
          "description": "擬似太字 (type=staticText時)"
        },
        "italic": {
          "type": "boolean",
          "default": false,
          "description": "擬似斜体 (type=staticText時)"
        },
        "underline": {
          "type": "boolean",
          "default": false,
          "description": "下線 (type=staticText時)"
        },
        "strikethrough": {
          "type": "boolean",
          "default": false,
          "description": "取消線 (type=staticText時)"
        },
        "rowHeight": {
          "type": "number",
          "description": "行高さ (mm) (type=subtable時)"
        },
        "columns": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/flowBoxColumn"
          },
          "description": "列定義 (type=subtable時)"
        }
      },
      "required": [
        "type"
      ]
    },
    "flowBoxColumn": {
      "type": "object",
      "description": "FlowBox サブテーブルの列定義",
      "properties": {
        "fieldCode": {
          "type": "string",
          "description": "テーブル内フィールドコード"
        },
        "offsetX": {
          "type": "number",
          "default": 0,
          "description": "ボックスX基準からの相対位置 (mm)"
        },
        "maxWidth": {
          "type": "number",
          "description": "最大幅 (mm)"
        },
        "fontSize": {
          "type": "number",
          "default": 10
        },
        "align": {
          "type": "string",
          "enum": [
            "left",
            "center",
            "right"
          ],
          "default": "left"
        },
        "format": {
          "type": "string"
        },
        "prefix": {
          "type": "string",
          "default": ""
        },
        "postfix": {
          "type": "string",
          "default": ""
        },
        "color": {
          "type": "string",
          "default": "#000000",
          "pattern": "^#[0-9a-fA-F]{6}$"
        },
        "vAlign": {
          "$ref": "#/$defs/vAlign"
        },
        "bold": {
          "type": "boolean"
        },
        "italic": {
          "type": "boolean"
        },
        "underline": {
          "type": "boolean"
        },
        "strikethrough": {
          "type": "boolean"
        }
      },
      "required": [
        "fieldCode"
      ]
    },
    "vAlign": {
      "type": "string",
      "enum": [
        "top",
        "middle",
        "bottom"
      ],
      "description": "縦方向の揃え位置。maxHeight と併用する。"
    }
  }
}
