{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kaikei.keydrop.net/schema/0.1/attachments.schema.json",
  "title": "証憑の一覧",
  "description": "attachments/index.json（10-evidence.md）",
  "type": "object",
  "required": [
    "attachments"
  ],
  "properties": {
    "attachments": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "id",
          "kind",
          "channel"
        ],
        "if": {
          "properties": {
            "channel": {
              "enum": [
                "paper_ref",
                "external_ref"
              ]
            }
          }
        },
        "then": {
          "required": [
            "location"
          ]
        },
        "else": {
          "required": [
            "path",
            "sha256",
            "mediaType"
          ]
        },
        "properties": {
          "id": {
            "$ref": "common.schema.json#/$defs/ulid"
          },
          "path": {
            "type": "string",
            "pattern": "^attachments/(?!.*\\.\\.)[A-Za-z0-9._/-]+$"
          },
          "sha256": {
            "$ref": "common.schema.json#/$defs/sha256"
          },
          "mediaType": {
            "type": "string",
            "pattern": "^[a-z]+/[a-z0-9.+-]+$"
          },
          "fileName": {
            "type": "string"
          },
          "kind": {
            "enum": [
              "receipt",
              "invoice",
              "quotation",
              "delivery_note",
              "contract",
              "statement",
              "other"
            ]
          },
          "channel": {
            "enum": [
              "electronic",
              "scanned",
              "paper_ref",
              "external_ref"
            ]
          },
          "receivedAt": {
            "$ref": "common.schema.json#/$defs/datetime"
          },
          "searchKeys": {
            "type": "object",
            "properties": {
              "transactionDate": {
                "$ref": "common.schema.json#/$defs/date"
              },
              "amount": {
                "$ref": "common.schema.json#/$defs/amount"
              },
              "partner": {
                "$ref": "common.schema.json#/$defs/identifier"
              },
              "partnerName": {
                "type": "string"
              }
            },
            "additionalProperties": false
          },
          "invoice": {
            "type": "object",
            "properties": {
              "registrationNumber": {
                "$ref": "common.schema.json#/$defs/invoiceRegistrationNumber"
              },
              "issuedOn": {
                "$ref": "common.schema.json#/$defs/date"
              },
              "simplified": {
                "type": "boolean",
                "description": "適格簡易請求書"
              },
              "totals": {
                "type": "array",
                "items": {
                  "type": "object",
                  "required": [
                    "rate",
                    "amountIncludingTax",
                    "taxAmount"
                  ],
                  "properties": {
                    "rate": {
                      "$ref": "common.schema.json#/$defs/rate"
                    },
                    "reduced": {
                      "type": "boolean"
                    },
                    "amountIncludingTax": {
                      "$ref": "common.schema.json#/$defs/amount"
                    },
                    "taxAmount": {
                      "$ref": "common.schema.json#/$defs/amount"
                    }
                  },
                  "additionalProperties": false
                }
              }
            },
            "additionalProperties": false
          },
          "extracted": {
            "type": "object",
            "required": [
              "by"
            ],
            "properties": {
              "by": {
                "enum": [
                  "ocr",
                  "llm",
                  "pdf_text",
                  "human"
                ]
              },
              "model": {
                "type": "string"
              },
              "confidence": {
                "$ref": "common.schema.json#/$defs/ratio"
              }
            },
            "additionalProperties": false
          },
          "location": {
            "type": "string",
            "minLength": 1,
            "description": "保管場所（paper_ref: 紙の保管場所、external_ref: 帳簿の外に置いた電子データの場所）"
          },
          "storage": {
            "type": "object",
            "description": "帳簿の外に置いたファイルの場所（external_ref の証憑。10-evidence.md の「帳簿の外のファイル」）",
            "required": [
              "provider",
              "fileId"
            ],
            "properties": {
              "provider": {
                "type": "string",
                "pattern": "^[a-z][a-z0-9_]*$",
                "description": "保管先のサービス（google_drive など）"
              },
              "fileId": {
                "type": "string",
                "minLength": 1,
                "description": "保管先のサービスでのファイルの ID"
              },
              "url": {
                "type": "string",
                "pattern": "^https://",
                "description": "ブラウザで開くための URL"
              },
              "path": {
                "type": "string",
                "minLength": 1,
                "description": "保管先のフォルダの中での場所（例: 2026/05/20260515_invoice.pdf）"
              }
            },
            "additionalProperties": false
          },
          "extensions": {
            "$ref": "common.schema.json#/$defs/extensions"
          },
          "supersedes": {
            "$ref": "common.schema.json#/$defs/ulid",
            "description": "差し替える前の証憑の ID"
          },
          "withdrawn": {
            "type": "object",
            "required": [
              "at",
              "reason"
            ],
            "properties": {
              "at": {
                "$ref": "common.schema.json#/$defs/datetime"
              },
              "reason": {
                "type": "string"
              }
            },
            "additionalProperties": false,
            "description": "誤って登録した証憑を無効にした記録（ファイルと一覧の行は消さない）"
          },
          "timestamp": {
            "type": "object",
            "required": [
              "issuedAt"
            ],
            "properties": {
              "issuedAt": {
                "$ref": "common.schema.json#/$defs/datetime"
              },
              "tsa": {
                "type": "string"
              },
              "token": {
                "type": "string",
                "description": "タイムスタンプトークン（Base64）またはそのファイルのパス"
              }
            },
            "additionalProperties": false
          },
          "scan": {
            "type": "object",
            "properties": {
              "scannedAt": {
                "$ref": "common.schema.json#/$defs/datetime"
              },
              "dpi": {
                "type": "integer",
                "minimum": 1
              },
              "colorDepth": {
                "type": "integer",
                "minimum": 1
              }
            },
            "additionalProperties": false
          }
        },
        "additionalProperties": false
      }
    },
    "defaultStorage": {
      "type": "object",
      "description": "新しい証憑のファイルを置く、帳簿の外のフォルダ（10-evidence.md の「帳簿の外のファイル」）",
      "required": [
        "provider",
        "folderId"
      ],
      "properties": {
        "provider": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]*$"
        },
        "folderId": {
          "type": "string",
          "minLength": 1
        },
        "url": {
          "type": "string",
          "pattern": "^https://"
        },
        "name": {
          "type": "string",
          "description": "フォルダの名前（表示用）"
        }
      },
      "additionalProperties": false
    },
    "extensions": {
      "$ref": "common.schema.json#/$defs/extensions"
    }
  },
  "additionalProperties": false
}
