{
  "openapi": "3.0.3",
  "info": {
    "title": "MiDireccionEuropea API",
    "version": "1.8.2"
  },
  "security": [{ "bearerAuth": [] }],
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      }
    },
    "schemas": {
      "Parcel": {
        "type": "object",
        "properties": {
          "width": { "type": "number" },
          "long": { "type": "number" },
          "height": { "type": "number" },
          "weight": { "type": "number" }
        },
        "required": ["width", "long", "height", "weight"],
        "additionalProperties": true
      },
      "ErrorResponse": {
        "type": "object",
        "properties": {
          "status": { "type": "string", "example": "error" },
          "message": { "type": "string" },
          "error": { "type": "string" }
        },
        "additionalProperties": true
      },
      "AuthResponse": {
        "type": "object",
        "properties": {
          "token": { "type": "string" }
        },
        "required": ["token"],
        "additionalProperties": true
      },
      "PieceResponse": {
        "type": "object",
        "properties": {
          "status": { "type": "string", "example": "ok" },
          "piece": { "type": "object", "additionalProperties": true }
        },
        "required": ["status", "piece"]
      }
    }
  },
  "paths": {
    "/api/v1/auth": {
      "post": {
        "summary": "Emitir JWT a partir de API Key/Secret",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "apiKey": { "type": "string" },
                  "apiSecret": { "type": "string" }
                },
                "required": ["apiKey", "apiSecret"]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "JWT emitido",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/AuthResponse" }
              }
            }
          },
          "401": {
            "description": "No autorizado",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/api/v1/quote": {
      "post": {
        "summary": "Cotizar envíos",
        "security": [{ "bearerAuth": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "countryFrom": { "type": "string", "example": "ES" },
                  "countryTo": { "type": "string", "example": "MX" },
                  "limit": {
                    "oneOf": [
                      { "type": "integer" },
                      { "type": "string", "pattern": "^[0-9]+$" }
                    ],
                    "example": 10
                  },
                  "sort": { "type": "string", "example": "cheap", "description": "Orden: cheap|fast (según implementación)" },
                  "mode": { "type": "integer", "example": 1 },
                  "areaFrom": { "type": "string" },
                  "postalCodeFrom": { "type": "string" },
                  "areaTo": { "type": "string" },
                  "postalCodeTo": { "type": "string" },
                  "declaredValue": { "type": "number", "example": 0 },
                  "parcels": {
                    "type": "array",
                    "items": { "$ref": "#/components/schemas/Parcel" },
                    "minItems": 1
                  }
                },
                "required": ["countryTo", "parcels"]
              },
              "example": {
                "countryFrom": "ES",
                "countryTo": "MX",
                "limit": 10,
                "parcels": [
                  { "width": 10, "long": 15, "height": 10, "weight": 1 }
                ]
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Cotización generada" },
          "400": {
            "description": "Parámetros inválidos",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" }
              }
            }
          },
          "401": {
            "description": "No autorizado",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/api/v1/pieces/{id}": {
      "get": {
        "summary": "Obtener una pieza (JSON)",
        "security": [{ "bearerAuth": [] }],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Pieza encontrada",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/PieceResponse" }
              }
            }
          },
          "400": {
            "description": "ID inválido",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" }
              }
            }
          },
          "401": {
            "description": "No autorizado",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" }
              }
            }
          },
          "404": {
            "description": "No encontrada",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" }
              }
            }
          }
        }
      }
    }
  }
}
