{
  "openapi": "3.1.1",
  "info": {
    "title": "CmrMasker.Api | v1",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "http://api-cmr-mask.nervalogic.ai/"
    }
  ],
  "paths": {
    "/api/templates": {
      "get": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "List all CMR templates (built-in catalog merged with custom templates and overrides), ordered by priority.",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/CmrTemplate"
                  }
                }
              }
            }
          }
        }
      },
      "post": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "Create a new custom CMR template.",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CmrTemplate"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
    "/api/templates/{id}": {
      "get": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "Get a single CMR template by id.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      },
      "put": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "Create or update a CMR template by id (overrides a built-in template when id matches a built-in).",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CmrTemplate"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      },
      "delete": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "Delete a custom template, or reset a built-in template override back to its built-in default.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
    "/api/templates/reset": {
      "post": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "Remove all custom templates and overrides, restoring the built-in template catalog.",
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
    "/api/template": {
      "get": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "Default box template (backward-compatible alias for the default template in /api/templates).",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CmrTemplate"
                }
              }
            }
          }
        }
      },
      "put": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "Replace the default box template (backward-compatible alias).",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CmrTemplate"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      },
      "delete": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "Reset the default template to its built-in definition (backward-compatible alias).",
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
    "/api/cmr/analyze": {
      "post": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "Detect boxes 16/23 with automatic template recognition (or a specific ?templateId=). Multipart field: photo (image or PDF; ?page= 0-based).",
        "parameters": [
          {
            "name": "rotation",
            "in": "query",
            "schema": {
              "pattern": "^-?(?:0|[1-9]\\d*)$",
              "type": [
                "integer",
                "string"
              ],
              "format": "int32"
            }
          },
          {
            "name": "page",
            "in": "query",
            "schema": {
              "pattern": "^-?(?:0|[1-9]\\d*)$",
              "type": [
                "integer",
                "string"
              ],
              "format": "int32"
            }
          },
          {
            "name": "flat",
            "in": "query",
            "schema": {
              "type": "boolean"
            }
          },
          {
            "name": "templateId",
            "in": "query",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AnalyzeResult"
                }
              }
            }
          }
        }
      }
    },
    "/api/cmr/extract": {
      "post": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "Extract CMR field values as JSON using Tesseract OCR with automatic template recognition (or ?templateId=). Multipart field: photo.",
        "parameters": [
          {
            "name": "rotation",
            "in": "query",
            "schema": {
              "pattern": "^-?(?:0|[1-9]\\d*)$",
              "type": [
                "integer",
                "string"
              ],
              "format": "int32"
            }
          },
          {
            "name": "page",
            "in": "query",
            "schema": {
              "pattern": "^-?(?:0|[1-9]\\d*)$",
              "type": [
                "integer",
                "string"
              ],
              "format": "int32"
            }
          },
          {
            "name": "flat",
            "in": "query",
            "schema": {
              "type": "boolean"
            }
          },
          {
            "name": "templateId",
            "in": "query",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ExtractResult"
                }
              }
            }
          }
        }
      }
    },
    "/api/cmr/rectify": {
      "post": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "Upright, rectified form with detected cells, for template calibration. Multipart field: photo; optional ?templateId=.",
        "parameters": [
          {
            "name": "rotation",
            "in": "query",
            "schema": {
              "pattern": "^-?(?:0|[1-9]\\d*)$",
              "type": [
                "integer",
                "string"
              ],
              "format": "int32"
            }
          },
          {
            "name": "page",
            "in": "query",
            "schema": {
              "pattern": "^-?(?:0|[1-9]\\d*)$",
              "type": [
                "integer",
                "string"
              ],
              "format": "int32"
            }
          },
          {
            "name": "templateId",
            "in": "query",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RectifyResult"
                }
              }
            }
          }
        }
      }
    },
    "/api/cmr/preview": {
      "post": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "A page of an image or PDF as JPEG, in the pixel coordinates used by analyze/render. Multipart field: photo; ?page= 0-based.",
        "parameters": [
          {
            "name": "page",
            "in": "query",
            "schema": {
              "pattern": "^-?(?:0|[1-9]\\d*)$",
              "type": [
                "integer",
                "string"
              ],
              "format": "int32"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
    "/api/cmr/detect-page": {
      "post": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "Detect the 4 A4 paper corners on a photo for interactive flattening. Multipart field: photo.",
        "parameters": [
          {
            "name": "page",
            "in": "query",
            "schema": {
              "pattern": "^-?(?:0|[1-9]\\d*)$",
              "type": [
                "integer",
                "string"
              ],
              "format": "int32"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PageDetectResult"
                }
              }
            }
          }
        }
      }
    },
    "/api/cmr/flatten": {
      "post": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "Perspective-warp a photo's 4 A4 paper corners into a flat, distortion-free portrait A4 JPEG. Multipart fields: photo, optional quad (JSON Point2[4]).",
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
    "/api/cmr/render": {
      "post": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "Replace the boxes on one photo or PDF. Multipart fields: photo, image (all boxes) and/or image_<regionId>, optional regions (JSON RenderRegion[] in page pixels, with page index), options (JSON RenderOptions); optional ?templateId=.",
        "parameters": [
          {
            "name": "rotation",
            "in": "query",
            "schema": {
              "pattern": "^-?(?:0|[1-9]\\d*)$",
              "type": [
                "integer",
                "string"
              ],
              "format": "int32"
            }
          },
          {
            "name": "page",
            "in": "query",
            "schema": {
              "pattern": "^-?(?:0|[1-9]\\d*)$",
              "type": [
                "integer",
                "string"
              ],
              "format": "int32"
            }
          },
          {
            "name": "templateId",
            "in": "query",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
    "/api/cmr/batch": {
      "post": {
        "tags": [
          "CmrMasker.Api"
        ],
        "summary": "Process many photos/PDFs with automatic template recognition (or ?templateId=). Multipart fields: photos (multiple), image and/or image_<regionId>, options. Returns a zip.",
        "parameters": [
          {
            "name": "templateId",
            "in": "query",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "AnalyzeResult": {
        "required": [
          "width",
          "height",
          "frameQuad",
          "pageDetected",
          "frameDetected",
          "rotation",
          "regions"
        ],
        "type": "object",
        "properties": {
          "width": {
            "pattern": "^-?(?:0|[1-9]\\d*)$",
            "type": [
              "integer",
              "string"
            ],
            "format": "int32"
          },
          "height": {
            "pattern": "^-?(?:0|[1-9]\\d*)$",
            "type": [
              "integer",
              "string"
            ],
            "format": "int32"
          },
          "frameQuad": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Point2"
            }
          },
          "pageDetected": {
            "type": "boolean"
          },
          "frameDetected": {
            "type": "boolean"
          },
          "rotation": {
            "pattern": "^-?(?:0|[1-9]\\d*)$",
            "type": [
              "integer",
              "string"
            ],
            "format": "int32"
          },
          "regions": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/RegionResult"
            }
          },
          "page": {
            "pattern": "^-?(?:0|[1-9]\\d*)$",
            "type": [
              "integer",
              "string"
            ],
            "format": "int32",
            "default": 0
          },
          "pageCount": {
            "pattern": "^-?(?:0|[1-9]\\d*)$",
            "type": [
              "integer",
              "string"
            ],
            "format": "int32",
            "default": 1
          },
          "isCmrPage": {
            "type": "boolean",
            "default": true
          },
          "templateId": {
            "type": [
              "null",
              "string"
            ]
          },
          "templateName": {
            "type": [
              "null",
              "string"
            ]
          },
          "templateScore": {
            "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$",
            "type": [
              "number",
              "string"
            ],
            "format": "double",
            "default": 0
          }
        }
      },
      "CmrTemplate": {
        "required": [
          "regions"
        ],
        "type": "object",
        "properties": {
          "regions": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/TemplateRegion"
            }
          },
          "id": {
            "type": "string",
            "default": "avc-2002"
          },
          "name": {
            "type": "string",
            "default": "Standard IRU / AVC 2002"
          },
          "description": {
            "type": [
              "null",
              "string"
            ]
          },
          "isBuiltIn": {
            "type": "boolean",
            "default": false
          },
          "enabled": {
            "type": "boolean",
            "default": true
          },
          "priority": {
            "pattern": "^-?(?:0|[1-9]\\d*)$",
            "type": [
              "integer",
              "string"
            ],
            "format": "int32",
            "default": 100
          },
          "hints": {
            "oneOf": [
              {
                "type": "null"
              },
              {
                "$ref": "#/components/schemas/TemplateMatchingHints"
              }
            ]
          }
        }
      },
      "ExtractedField": {
        "required": [
          "id",
          "label",
          "text",
          "confidence"
        ],
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "label": {
            "type": "string"
          },
          "text": {
            "type": "string"
          },
          "confidence": {
            "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$",
            "type": [
              "number",
              "string"
            ],
            "format": "double"
          }
        }
      },
      "ExtractResult": {
        "required": [
          "page",
          "pageCount",
          "frameDetected",
          "isCmrPage",
          "rotation",
          "fields"
        ],
        "type": "object",
        "properties": {
          "page": {
            "pattern": "^-?(?:0|[1-9]\\d*)$",
            "type": [
              "integer",
              "string"
            ],
            "format": "int32"
          },
          "pageCount": {
            "pattern": "^-?(?:0|[1-9]\\d*)$",
            "type": [
              "integer",
              "string"
            ],
            "format": "int32"
          },
          "frameDetected": {
            "type": "boolean"
          },
          "isCmrPage": {
            "type": "boolean"
          },
          "rotation": {
            "pattern": "^-?(?:0|[1-9]\\d*)$",
            "type": [
              "integer",
              "string"
            ],
            "format": "int32"
          },
          "fields": {
            "type": "object",
            "additionalProperties": {
              "$ref": "#/components/schemas/ExtractedField"
            }
          },
          "templateId": {
            "type": [
              "null",
              "string"
            ]
          },
          "templateName": {
            "type": [
              "null",
              "string"
            ]
          },
          "templateScore": {
            "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$",
            "type": [
              "number",
              "string"
            ],
            "format": "double",
            "default": 0
          }
        }
      },
      "NormRect": {
        "required": [
          "x",
          "y",
          "w",
          "h"
        ],
        "type": "object",
        "properties": {
          "x": {
            "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$",
            "type": [
              "number",
              "string"
            ],
            "format": "double"
          },
          "y": {
            "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$",
            "type": [
              "number",
              "string"
            ],
            "format": "double"
          },
          "w": {
            "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$",
            "type": [
              "number",
              "string"
            ],
            "format": "double"
          },
          "h": {
            "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$",
            "type": [
              "number",
              "string"
            ],
            "format": "double"
          }
        }
      },
      "PageDetectResult": {
        "required": [
          "width",
          "height",
          "quad",
          "pageDetected"
        ],
        "type": "object",
        "properties": {
          "width": {
            "pattern": "^-?(?:0|[1-9]\\d*)$",
            "type": [
              "integer",
              "string"
            ],
            "format": "int32"
          },
          "height": {
            "pattern": "^-?(?:0|[1-9]\\d*)$",
            "type": [
              "integer",
              "string"
            ],
            "format": "int32"
          },
          "quad": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Point2"
            }
          },
          "pageDetected": {
            "type": "boolean"
          }
        }
      },
      "Point2": {
        "required": [
          "x",
          "y"
        ],
        "type": "object",
        "properties": {
          "x": {
            "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$",
            "type": [
              "number",
              "string"
            ],
            "format": "double"
          },
          "y": {
            "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$",
            "type": [
              "number",
              "string"
            ],
            "format": "double"
          }
        }
      },
      "RectifyResult": {
        "required": [
          "width",
          "height",
          "imageBase64Jpeg",
          "pageDetected",
          "frameDetected",
          "cells"
        ],
        "type": "object",
        "properties": {
          "width": {
            "pattern": "^-?(?:0|[1-9]\\d*)$",
            "type": [
              "integer",
              "string"
            ],
            "format": "int32"
          },
          "height": {
            "pattern": "^-?(?:0|[1-9]\\d*)$",
            "type": [
              "integer",
              "string"
            ],
            "format": "int32"
          },
          "imageBase64Jpeg": {
            "type": "string"
          },
          "pageDetected": {
            "type": "boolean"
          },
          "frameDetected": {
            "type": "boolean"
          },
          "cells": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/NormRect"
            }
          }
        }
      },
      "RegionResult": {
        "required": [
          "id",
          "quad",
          "snapped",
          "confidence"
        ],
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "quad": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Point2"
            }
          },
          "snapped": {
            "type": "boolean"
          },
          "confidence": {
            "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$",
            "type": [
              "number",
              "string"
            ],
            "format": "double"
          }
        }
      },
      "TemplateMatchingHints": {
        "type": "object",
        "properties": {
          "anchorCells": {
            "type": [
              "null",
              "array"
            ],
            "items": {
              "$ref": "#/components/schemas/NormRect"
            }
          },
          "signatureRowMinY": {
            "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$",
            "type": [
              "null",
              "number",
              "string"
            ],
            "format": "double"
          },
          "signatureRowMaxY": {
            "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$",
            "type": [
              "null",
              "number",
              "string"
            ],
            "format": "double"
          },
          "headerKeywords": {
            "type": [
              "null",
              "array"
            ],
            "items": {
              "type": "string"
            }
          },
          "expectedAspect": {
            "pattern": "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$",
            "type": [
              "null",
              "number",
              "string"
            ],
            "format": "double"
          }
        }
      },
      "TemplateRegion": {
        "required": [
          "id",
          "label",
          "rect"
        ],
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "label": {
            "type": "string"
          },
          "rect": {
            "$ref": "#/components/schemas/NormRect"
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "CmrMasker.Api"
    }
  ]
}