{
  "openapi": "3.1.0",
  "info": {
    "title": "GeoNet Red CORS — API v1",
    "version": "1.0.0",
    "description": "API REST para provisioning de credenciales NTRIP a escala, consulta de estaciones, salud de red, selección VRS y gestión de webhooks. Autenticación: `Authorization: Bearer gnk_…` (tokens con scopes `read`, `provision`, `revoke`, gestionados en Admin → Plataforma). Rate limit: 240 req/min por IP."
  },
  "servers": [
    {
      "url": "/api/v1"
    }
  ],
  "components": {
    "securitySchemes": {
      "bearer": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "gnk_token"
      }
    },
    "schemas": {
      "Station": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "codigo": {
            "type": "string",
            "examples": [
              "CMXC_GAM_01"
            ]
          },
          "nombre": {
            "type": "string"
          },
          "estado_mx": {
            "type": "string"
          },
          "lat": {
            "type": "number"
          },
          "lng": {
            "type": "number"
          },
          "radio_km": {
            "type": "number"
          },
          "status": {
            "type": "string",
            "enum": [
              "online",
              "degraded",
              "offline",
              "maintenance"
            ]
          },
          "constelaciones": {
            "type": "string"
          },
          "telemetria": {
            "type": "boolean",
            "description": "true si la estación tiene al menos un registro real en station_health. Si es false, salud_pct/latencia_ms/sats son sólo los defaults del esquema (99.9/45/28), no una medición del caster: no los presentes como dato en vivo."
          },
          "salud_pct": {
            "type": "number"
          },
          "latencia_ms": {
            "type": "integer"
          },
          "sats": {
            "type": "integer"
          }
        }
      },
      "Credential": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "tipo": {
            "type": "string"
          },
          "usuario": {
            "type": "string"
          },
          "password": {
            "type": "string"
          },
          "mountpoint": {
            "type": "string"
          },
          "caster": {
            "type": "string"
          },
          "puerto": {
            "type": "integer"
          },
          "formato": {
            "type": "string"
          },
          "datum": {
            "type": "string"
          },
          "emitida_at": {
            "type": "string",
            "format": "date-time"
          },
          "expira_at": {
            "type": "string",
            "format": "date-time"
          },
          "estado": {
            "type": "string",
            "enum": [
              "activa",
              "expirada",
              "revocada"
            ]
          },
          "url": {
            "type": "string"
          },
          "pdf": {
            "type": "string"
          },
          "inicia_at": {
            "type": "string",
            "format": "date-time",
            "description": "Cuándo empieza a servir."
          },
          "vigente": {
            "type": "boolean",
            "description": "true sólo si ya empezó y no ha expirado. Una credencial programada tiene estado \"activa\" pero vigente false, y el caster aún no la acepta."
          }
        }
      }
    }
  },
  "security": [
    {
      "bearer": []
    }
  ],
  "paths": {
    "/stations": {
      "get": {
        "summary": "Lista las estaciones CORS activas con estado en vivo",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "stations": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Station"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/stations/{id}/health": {
      "get": {
        "summary": "Salud e historial de uptime (60 días) de una estación",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          },
          "404": {
            "description": "No encontrada"
          }
        }
      }
    },
    "/vrs": {
      "get": {
        "summary": "Selección de mountpoint inteligente (Network RTK/VRS o base única) para una posición",
        "parameters": [
          {
            "name": "lat",
            "in": "query",
            "required": true,
            "schema": {
              "type": "number"
            }
          },
          {
            "name": "lng",
            "in": "query",
            "required": true,
            "schema": {
              "type": "number"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          },
          "400": {
            "description": "lat/lng inválidos"
          }
        }
      }
    },
    "/credentials": {
      "get": {
        "summary": "Lista credenciales (filtro opcional ?estado=activa|expirada|revocada)",
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      },
      "post": {
        "summary": "Provisiona una credencial NTRIP (scope: provision)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "segundos"
                ],
                "properties": {
                  "station_id": {
                    "type": "integer"
                  },
                  "station_codigo": {
                    "type": "string",
                    "description": "Alternativa a station_id"
                  },
                  "segundos": {
                    "type": "integer",
                    "minimum": 3600,
                    "maximum": 31536000
                  },
                  "label": {
                    "type": "string"
                  },
                  "cliente_nombre": {
                    "type": "string"
                  },
                  "cliente_email": {
                    "type": "string",
                    "format": "email"
                  },
                  "enviar_email": {
                    "type": "boolean",
                    "default": false
                  },
                  "inicia_at": {
                    "type": "string",
                    "description": "Cuándo empieza a servir: YYYY-MM-DD (00:00 hora del centro de México) o ISO 8601. Sin este campo empieza al emitirse. Máximo 180 días de anticipación. La vigencia se cuenta desde aquí, no desde la emisión.",
                    "example": "2026-10-01"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Credencial creada",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "credential": {
                      "$ref": "#/components/schemas/Credential"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Solicitud inválida"
          }
        }
      }
    },
    "/credentials/{id}": {
      "get": {
        "summary": "Detalle de una credencial",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          },
          "404": {
            "description": "No encontrada"
          }
        }
      },
      "delete": {
        "summary": "Revoca una credencial (scope: revoke) — emite webhook credential.revoked",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Revocada"
          },
          "404": {
            "description": "No encontrada"
          }
        }
      }
    },
    "/webhooks": {
      "get": {
        "summary": "Lista los webhooks del dueño del token",
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      },
      "post": {
        "summary": "Crea un webhook saliente firmado (HMAC-SHA256, header X-GeoNet-Signature)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "url"
                ],
                "properties": {
                  "url": {
                    "type": "string",
                    "format": "uri"
                  },
                  "eventos": {
                    "type": "string",
                    "default": "order.paid,credential.issued,credential.expired"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Creado (incluye el secreto HMAC, se muestra una sola vez)"
          }
        }
      }
    },
    "/webhooks/{id}": {
      "delete": {
        "summary": "Desactiva un webhook",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }
  }
}
