{
  "openapi": "3.0.3",
  "info": {
    "title": "Defect Report Backend Prototype API",
    "version": "0.1.0",
    "description": "API for Android defect report workflow: mechanic, shop chief, advisor, warranty engineer, parts department and admin."
  },
  "servers": [
    {
      "url": "http://localhost:8787"
    }
  ],
  "paths": {
    "/health": {
      "get": {
        "summary": "Health check",
        "responses": {
          "200": {
            "description": "Server is running"
          }
        }
      }
    },
    "/api/auth/login": {
      "post": {
        "summary": "Mock login by role and PIN",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "example": {
                "role": "mechanic",
                "pin": "1111"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Bearer token and public user"
          }
        }
      }
    },
    "/api/meta": {
      "get": {
        "summary": "Roles, statuses and defect nodes",
        "responses": {
          "200": {
            "description": "Metadata"
          }
        }
      }
    },
    "/api/reports": {
      "get": {
        "summary": "List defect reports",
        "parameters": [
          {
            "name": "status",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "assigneeRole",
            "in": "query",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Report summaries"
          }
        }
      },
      "post": {
        "summary": "Create a defect report",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "example": {
                "orderNumber": "ЗН-24891",
                "vehicle": {
                  "vin": "XTA00000000000001",
                  "model": "Demo car",
                  "mileage": 125000
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created report"
          }
        }
      }
    },
    "/api/reports/{reportId}": {
      "get": {
        "summary": "Get full defect report",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/reportId"
          }
        ],
        "responses": {
          "200": {
            "description": "Full report"
          }
        }
      },
      "patch": {
        "summary": "Update report header",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/reportId"
          }
        ],
        "responses": {
          "200": {
            "description": "Updated report"
          }
        }
      }
    },
    "/api/reports/{reportId}/defects": {
      "post": {
        "summary": "Add defect to report",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/reportId"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "example": {
                "text": "течь амортизатора заднего правого",
                "material": "1 шт.",
                "recommendation": "Замена"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created defect"
          }
        }
      }
    },
    "/api/reports/{reportId}/defects/{defectId}": {
      "patch": {
        "summary": "Update defect",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/reportId"
          },
          {
            "$ref": "#/components/parameters/defectId"
          }
        ],
        "responses": {
          "200": {
            "description": "Updated defect"
          }
        }
      }
    },
    "/api/reports/{reportId}/defects/{defectId}/media": {
      "post": {
        "summary": "Attach media to defect",
        "description": "Prototype endpoint uses JSON + base64. Production should use multipart or direct S3/MinIO upload.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/reportId"
          },
          {
            "$ref": "#/components/parameters/defectId"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "example": {
                "fileName": "amortizator.jpg",
                "contentType": "image/jpeg",
                "base64": "..."
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Media metadata"
          }
        }
      }
    },
    "/api/reports/{reportId}/actions/{action}": {
      "post": {
        "summary": "Run workflow action",
        "description": "Actions: submit, handoff, return, approve, assign, route, close.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/reportId"
          },
          {
            "name": "action",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "example": {
                "comment": "Добавить фото течи",
                "route": "warranty",
                "assigneeUserId": "user-mechanic-1"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Updated report"
          }
        }
      }
    },
    "/api/admin/dashboard": {
      "get": {
        "summary": "Admin realtime dashboard snapshot",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "Counters, report summaries, recent history and notifications"
          }
        }
      }
    },
    "/api/admin/history": {
      "get": {
        "summary": "Status and handoff history",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "Workflow history"
          }
        }
      }
    },
    "/api/events": {
      "get": {
        "summary": "Server-Sent Events stream for realtime admin updates",
        "responses": {
          "200": {
            "description": "SSE stream"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer"
      }
    },
    "parameters": {
      "reportId": {
        "name": "reportId",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string"
        }
      },
      "defectId": {
        "name": "defectId",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string"
        }
      }
    }
  }
}
