{
  "openapi": "3.1.0",
  "info": {
    "title": "TCARGO Public API",
    "version": "1.0.0",
    "description": "API pública de TCARGO para publicar, consultar y cancelar cargas (fletes FTL) desde un ERP o un agente de IA.",
    "contact": { "name": "Soporte TCARGO", "email": "soporte@tcargo.app", "url": "https://tcargo.app/contacto" }
  },
  "servers": [
    { "url": "https://opbmzqdepnxjvvthsink.supabase.co/functions/v1/public-api-v1", "description": "Producción" }
  ],
  "security": [{ "bearerAuth": [] }],
  "components": {
    "securitySchemes": {
      "bearerAuth": { "type": "http", "scheme": "bearer", "description": "API key del dador o access token OAuth 2.1." }
    },
    "schemas": {
      "CargoInput": {
        "type": "object",
        "required": ["origin", "destination", "pickup_date"],
        "properties": {
          "external_id": { "type": "string", "description": "Identificador de la carga en el ERP del dador." },
          "origin": { "type": "string", "description": "Dirección o localidad de origen." },
          "destination": { "type": "string", "description": "Dirección o localidad de destino." },
          "pickup_date": { "type": "string", "format": "date" },
          "merchandise_type": { "type": "string", "description": "Tipo de mercadería." },
          "packaging": { "type": "string", "description": "Tipo de embalaje." },
          "weight_kg": { "type": "number" },
          "truck_type": { "type": "string" },
          "notes": { "type": "string" }
        }
      },
      "Cargo": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "display_id": { "type": "string", "description": "Identificador visible del embarque, ej. #2122." },
          "status": { "type": "string", "enum": ["pending", "offered", "assigned", "loading", "in_transit", "delivered", "completed", "unfulfilled", "cancelled"] },
          "origin": { "type": "string" },
          "destination": { "type": "string" },
          "pickup_date": { "type": "string", "format": "date" },
          "tracking_url": { "type": "string", "format": "uri" }
        }
      }
    }
  },
  "paths": {
    "/health": {
      "get": {
        "summary": "Health check",
        "security": [],
        "responses": { "200": { "description": "Servicio operativo" } }
      }
    },
    "/v1/cargos": {
      "get": {
        "summary": "Listar cargas del dador",
        "parameters": [
          { "name": "since", "in": "query", "schema": { "type": "string", "format": "date-time" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "maximum": 200, "default": 50 } }
        ],
        "responses": {
          "200": {
            "description": "Listado de cargas",
            "content": { "application/json": { "schema": { "type": "array", "items": { "$ref": "#/components/schemas/Cargo" } } } }
          }
        }
      },
      "post": {
        "summary": "Publicar una carga (1 ID = 1 camión)",
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CargoInput" } } }
        },
        "responses": {
          "201": { "description": "Carga creada", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Cargo" } } } }
        }
      }
    },
    "/v1/cargos/{id}": {
      "get": {
        "summary": "Obtener una carga por ID",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }],
        "responses": {
          "200": { "description": "Carga", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Cargo" } } } },
          "404": { "description": "No encontrada" }
        }
      }
    },
    "/v1/cargos/{id}/cancel": {
      "post": {
        "summary": "Cancelar una carga",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }],
        "responses": { "200": { "description": "Carga cancelada" } }
      }
    }
  }
}
