{
  "openapi": "3.1.0",
  "info": {
    "title": "NooriACS — atualização sequencial Mesh",
    "version": "1.0.0"
  },
  "security": [
    {
      "operatorSession": []
    }
  ],
  "paths": {
    "/api/v1/task-operations/firmware-plan": {
      "post": {
        "description": "Prévia sem criação de tasks. Tenant da sessão; exige tasks:execute e firmware:deploy. Satélites antes da principal. Pacote único compatível por satélite; seleção ambígua ou sem homologação retorna 409. Já atualizado é validado por versão homologada e Inform recente na execução.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "deviceIds",
                  "firmwareId"
                ],
                "properties": {
                  "deviceIds": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": 200,
                    "items": {
                      "type": "string",
                      "format": "uuid"
                    }
                  },
                  "firmwareId": {
                    "type": "string",
                    "format": "uuid"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Plano revisável ou null quando não há descendentes Mesh.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "meshPlan": {
                      "oneOf": [
                        {
                          "$ref": "#/components/schemas/MeshFirmwarePlan"
                        },
                        {
                          "type": "null"
                        }
                      ]
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Seleção inválida ou dispositivo fora do tenant"
          },
          "401": {
            "description": "Sessão ausente"
          },
          "403": {
            "description": "Permissão insuficiente"
          },
          "409": {
            "description": "MESH_FIRMWARE_NOT_READY, MESH_TOPOLOGY_INVALID, MESH_SELECTION_OVERLAP, MESH_FIRMWARE_LIMIT"
          }
        }
      }
    },
    "/api/v1/task-operations": {
      "post": {
        "description": "Extensão aditiva do contrato existente: meshPlanHash obrigatório quando houver rede Mesh. Recalcula plano e rejeita hash divergente (409 MESH_PLAN_CONFIRMATION_REQUIRED). Idempotency-Key obrigatório. Reserva todos os alvos, revalida permissão/pacote/topologia por etapa e interrompe após falha. Satélites usam próximo Inform. Prazo escolhido ou 24h para imediato. Nenhuma atualização automática fora da operação confirmada.",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 8,
              "maxLength": 128
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "allOf": [
                  {
                    "type": "object",
                    "required": [
                      "deviceIds",
                      "firmwareId"
                    ],
                    "properties": {
                      "deviceIds": {
                        "type": "array",
                        "minItems": 1,
                        "maxItems": 200,
                        "items": {
                          "type": "string",
                          "format": "uuid"
                        }
                      },
                      "firmwareId": {
                        "type": "string",
                        "format": "uuid"
                      }
                    }
                  },
                  {
                    "type": "object",
                    "required": [
                      "execution"
                    ],
                    "properties": {
                      "meshPlanHash": {
                        "type": [
                          "string",
                          "null"
                        ]
                      },
                      "execution": {
                        "type": "object",
                        "properties": {
                          "mode": {
                            "enum": [
                              "IMMEDIATE",
                              "SCHEDULED",
                              "NEXT_INFORM"
                            ]
                          },
                          "waitMinutes": {
                            "enum": [
                              15,
                              60,
                              1440
                            ]
                          },
                          "scheduledFor": {
                            "type": "string",
                            "format": "date-time"
                          },
                          "timezone": {
                            "type": "string"
                          }
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        },
        "responses": {
          "202": {
            "description": "Progresso da operação existente acrescido de meshPlan"
          },
          "403": {
            "description": "Permissão insuficiente"
          },
          "409": {
            "description": "Plano divergente, equipamento reservado ou validação falhou"
          }
        }
      }
    },
    "/api/v1/task-operations/{id}/progress": {
      "get": {
        "description": "Exige tasks:read. Resposta de progresso existente inclui meshPlan; etapas aguardando predecessor usam WAITING_DEPENDENCY. SUCCESS exige versão confirmada; alreadyCurrent não cria Download.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Progresso e eventos da operação"
          },
          "404": {
            "description": "Operação inexistente neste tenant"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "operatorSession": {
        "type": "apiKey",
        "in": "cookie",
        "name": "nooriacs_session"
      }
    },
    "schemas": {
      "MeshFirmwareStep": {
        "type": "object",
        "required": [
          "deviceId",
          "parentId",
          "serialNumber",
          "firmwareId",
          "targetVersion",
          "alreadyCurrent"
        ],
        "properties": {
          "deviceId": {
            "type": "string",
            "format": "uuid"
          },
          "parentId": {
            "type": [
              "string",
              "null"
            ],
            "format": "uuid"
          },
          "serialNumber": {
            "type": "string"
          },
          "firmwareId": {
            "type": "string",
            "format": "uuid"
          },
          "targetVersion": {
            "type": "string"
          },
          "alreadyCurrent": {
            "type": "boolean"
          }
        }
      },
      "MeshFirmwarePlan": {
        "type": "object",
        "required": [
          "hash",
          "steps"
        ],
        "properties": {
          "hash": {
            "type": "string",
            "pattern": "^[0-9a-f]{64}$"
          },
          "steps": {
            "type": "array",
            "maxItems": 200,
            "items": {
              "$ref": "#/components/schemas/MeshFirmwareStep"
            }
          }
        }
      }
    }
  }
}
