{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "niho://host-script/script.schema.json",
  "title": "NIHO ホストスクリプト script.json（host-script-sdk-v0.4）",
  "description": "scripts/<key>/script.json の形。フォルダ名 <key> が stableKey（小文字・数字・ハイフン、64 文字以下）になります。",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "name",
    "description",
    "trigger"
  ],
  "allOf": [
    {
      "if": {
        "required": [
          "trigger"
        ],
        "properties": {
          "trigger": {
            "type": "object",
            "required": [
              "kind"
            ],
            "properties": {
              "kind": {
                "const": "GCAL_FORMAT"
              }
            }
          }
        }
      },
      "then": {
        "required": [
          "permissions"
        ],
        "properties": {
          "permissions": {
            "type": "array",
            "contains": {
              "const": "gcal.format"
            }
          }
        }
      }
    }
  ],
  "properties": {
    "$schema": {
      "type": "string"
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 80,
      "description": "スクリプトの名前（前後の空白は除かれます）。"
    },
    "description": {
      "type": "string",
      "minLength": 1,
      "maxLength": 500,
      "description": "何をするスクリプトか。公開前に人が読みます。"
    },
    "trigger": {
      "description": "起動条件。SCHEDULE は定期実行、STUDIO_EVENT は Studio の契機、GCAL_FORMAT は Google 予定の出力。",
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "kind",
            "intervalMinutes"
          ],
          "properties": {
            "kind": {
              "const": "SCHEDULE"
            },
            "intervalMinutes": {
              "type": "integer",
              "minimum": 5,
              "maximum": 43200,
              "description": "実行の間隔（分）。"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "kind"
          ],
          "properties": {
            "kind": {
              "const": "STUDIO_EVENT"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "kind"
          ],
          "properties": {
            "kind": {
              "const": "GCAL_FORMAT"
            }
          }
        }
      ]
    },
    "permissions": {
      "type": "array",
      "maxItems": 6,
      "uniqueItems": true,
      "items": {
        "enum": [
          "devices.execute",
          "fetch",
          "storage",
          "drive",
          "bookings.read",
          "gcal.format"
        ]
      },
      "default": [],
      "description": "公開する版が使う権限。使う機能に対応する権限を宣言し、公開前に人が確認します。"
    },
    "configSchema": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "properties"
      ],
      "description": "設定画面に出す項目の宣言。",
      "properties": {
        "type": {
          "const": "object"
        },
        "properties": {
          "type": "object",
          "maxProperties": 30,
          "propertyNames": {
            "pattern": "^[a-z][a-zA-Z0-9_]{0,63}$"
          },
          "additionalProperties": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "type",
              "title"
            ],
            "properties": {
              "type": {
                "enum": [
                  "string",
                  "number",
                  "integer",
                  "boolean"
                ]
              },
              "title": {
                "type": "string",
                "minLength": 1,
                "maxLength": 80,
                "description": "設定画面・Studio に出す名前。前後の空白は除かれます。"
              },
              "description": {
                "type": "string",
                "maxLength": 240
              },
              "default": {
                "type": [
                  "string",
                  "number",
                  "boolean"
                ]
              },
              "enum": {
                "type": "array",
                "minItems": 1,
                "maxItems": 50,
                "items": {
                  "type": [
                    "string",
                    "number",
                    "boolean"
                  ]
                }
              },
              "minimum": {
                "type": "number"
              },
              "maximum": {
                "type": "number"
              }
            }
          }
        },
        "required": {
          "type": "array",
          "maxItems": 30,
          "uniqueItems": true,
          "items": {
            "type": "string"
          },
          "description": "必須にする項目のキー（properties で宣言したものだけ）。"
        }
      }
    },
    "configValues": {
      "type": "object",
      "additionalProperties": {
        "type": [
          "string",
          "number",
          "boolean"
        ]
      },
      "description": "設定の値。configSchema で宣言した項目だけ。全体で 8KiB まで、文字列は 1024 文字まで。"
    },
    "outputSchema": {
      "type": [
        "object",
        "null"
      ],
      "additionalProperties": false,
      "required": [
        "type",
        "properties"
      ],
      "description": "STUDIO_EVENT の戻り値の契約（任意）。全体で 8KiB まで。数値・文字列の項目には enum が必要です。",
      "properties": {
        "type": {
          "const": "object"
        },
        "properties": {
          "type": "object",
          "maxProperties": 20,
          "propertyNames": {
            "pattern": "^[a-z][a-zA-Z0-9_]{0,63}$"
          },
          "additionalProperties": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "type",
              "title"
            ],
            "properties": {
              "type": {
                "enum": [
                  "string",
                  "number",
                  "integer",
                  "boolean"
                ]
              },
              "title": {
                "type": "string",
                "minLength": 1,
                "maxLength": 80,
                "description": "設定画面・Studio に出す名前。前後の空白は除かれます。"
              },
              "description": {
                "type": "string",
                "maxLength": 240
              },
              "enum": {
                "type": "array",
                "minItems": 1,
                "maxItems": 50,
                "items": {
                  "anyOf": [
                    {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 64,
                      "pattern": "^[^\\u0000-\\u001f\\u007f@]*$",
                      "not": {
                        "pattern": "://"
                      }
                    },
                    {
                      "type": "number"
                    },
                    {
                      "type": "boolean"
                    }
                  ]
                },
                "description": "選択肢。文字列は 1〜64 文字で、制御文字・@・:// を含められません。"
              }
            }
          }
        },
        "required": {
          "type": "array",
          "maxItems": 20,
          "uniqueItems": true,
          "items": {
            "type": "string"
          },
          "description": "必須にする項目のキー（properties で宣言したものだけ）。"
        }
      }
    }
  }
}
