{
  "openapi": "3.0.3",
  "info": {
    "title": "Sparehawk Supplier API",
    "version": "1.0.0",
    "description": "The supplier-facing REST API (Wave 9 item 5): push offers, read RFQs routed to you, and submit quotes — from your own systems, without anyone logging into the cabinet by hand. Authenticate with an API key created under Cabinet > Developer > API keys."
  },
  "servers": [{ "url": "https://api.sparehawk.com" }],
  "security": [{ "ApiKeyAuth": [] }],
  "tags": [
    { "name": "Offers", "description": "Bulk upsert and patch your catalogue." },
    { "name": "RFQs", "description": "RFQs routed to your supplier account." },
    { "name": "Quotes", "description": "Submit a quote against a routed RFQ." }
  ],
  "paths": {
    "/v1/supplier-api/offers/bulk": {
      "post": {
        "tags": ["Offers"],
        "summary": "Bulk upsert offers",
        "description": "Upserts up to 1000 offers per call, matched by MPN + condition. An MPN not yet in the catalogue becomes a needs_review part stub rather than an error. Supports an Idempotency-Key header: the same key replays the first response instead of running the mutation again.",
        "security": [{ "ApiKeyAuth": ["offers:write"] }],
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "schema": { "type": "string" },
            "description": "A key you generate per attempt. A retry with the same key replays the cached response."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/BulkOffersRequest" },
              "example": {
                "offers": [
                  {
                    "mpn": "6ES7315-2EH14-0AB0",
                    "manufacturerName": "Siemens",
                    "condition": "new_oem",
                    "quantity": 5,
                    "price": 420.0,
                    "currency": "EUR",
                    "leadTimeDays": 3
                  }
                ]
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Per-line results — a line can succeed even if another in the same call failed.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BulkOffersResponse" }
              }
            }
          },
          "400": { "description": "Validation error (e.g. more than 1000 offers in one call)." },
          "401": { "description": "Missing or invalid API key." },
          "403": { "description": "The key is missing the offers:write scope." },
          "429": { "description": "This key has exceeded its rate limit." }
        }
      }
    },
    "/v1/supplier-api/offers/{id}": {
      "patch": {
        "tags": ["Offers"],
        "summary": "Patch one offer's stock/price/lead time",
        "security": [{ "ApiKeyAuth": ["offers:write"] }],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": { "schema": { "$ref": "#/components/schemas/OfferPatchRequest" } }
          }
        },
        "responses": {
          "200": { "description": "Updated." },
          "404": { "description": "No such offer for this supplier." },
          "401": { "description": "Missing or invalid API key." },
          "403": { "description": "The key is missing the offers:write scope." }
        }
      }
    },
    "/v1/supplier-api/rfqs": {
      "get": {
        "tags": ["RFQs"],
        "summary": "List RFQs routed to you",
        "security": [{ "ApiKeyAuth": ["rfqs:read"] }],
        "responses": {
          "200": {
            "description": "Up to the 100 most recent, newest first.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": { "$ref": "#/components/schemas/RfqListItem" }
                }
              }
            }
          },
          "401": { "description": "Missing or invalid API key." },
          "403": { "description": "The key is missing the rfqs:read scope." }
        }
      }
    },
    "/v1/supplier-api/rfqs/{id}": {
      "get": {
        "tags": ["RFQs"],
        "summary": "Fetch one RFQ's detail, including its lines",
        "security": [{ "ApiKeyAuth": ["rfqs:read"] }],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" }
          }
        ],
        "responses": {
          "200": {
            "description": "Found.",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/RfqDetail" } }
            }
          },
          "404": { "description": "Not routed to this supplier." },
          "401": { "description": "Missing or invalid API key." },
          "403": { "description": "The key is missing the rfqs:read scope." }
        }
      }
    },
    "/v1/supplier-api/quotes": {
      "post": {
        "tags": ["Quotes"],
        "summary": "Submit a quote against a routed RFQ",
        "security": [{ "ApiKeyAuth": ["quotes:write"] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": { "schema": { "$ref": "#/components/schemas/CreateQuoteRequest" } }
          }
        },
        "responses": {
          "201": {
            "description": "Created.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CreateQuoteResponse" }
              }
            }
          },
          "404": { "description": "The RFQ (or one of its lines) is not routed to this supplier." },
          "401": { "description": "Missing or invalid API key." },
          "403": { "description": "The key is missing the quotes:write scope." }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "sk_live_...",
        "description": "Authorization: Bearer sk_live_... — created under Cabinet > Developer > API keys, shown once at creation."
      }
    },
    "schemas": {
      "OfferUpsert": {
        "type": "object",
        "required": ["mpn", "condition"],
        "properties": {
          "mpn": { "type": "string", "maxLength": 120 },
          "manufacturerName": {
            "type": "string",
            "maxLength": 120,
            "description": "Required only when this MPN is not already in the catalogue."
          },
          "condition": {
            "type": "string",
            "enum": ["new_oem", "new_surplus", "refurbished", "used"]
          },
          "quantity": { "type": "integer", "minimum": 0 },
          "price": { "type": "number", "minimum": 0, "maximum": 10000000 },
          "currency": { "type": "string", "minLength": 3, "maxLength": 3 },
          "leadTimeDays": { "type": "integer", "minimum": 0, "maximum": 365 }
        }
      },
      "BulkOffersRequest": {
        "type": "object",
        "required": ["offers"],
        "properties": {
          "offers": {
            "type": "array",
            "minItems": 1,
            "maxItems": 1000,
            "items": { "$ref": "#/components/schemas/OfferUpsert" }
          }
        }
      },
      "BulkOfferResult": {
        "type": "object",
        "properties": {
          "mpn": { "type": "string" },
          "offerId": { "type": "string", "format": "uuid", "nullable": true },
          "status": { "type": "string", "enum": ["created", "updated", "error"] },
          "error": { "type": "string", "nullable": true }
        }
      },
      "BulkOffersResponse": {
        "type": "object",
        "properties": {
          "results": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/BulkOfferResult" }
          },
          "replayed": {
            "type": "boolean",
            "description": "true when this is a cached response replayed via Idempotency-Key."
          }
        }
      },
      "OfferPatchRequest": {
        "type": "object",
        "properties": {
          "quantity": { "type": "integer", "minimum": 0 },
          "price": { "type": "number", "minimum": 0, "maximum": 10000000, "nullable": true },
          "leadTimeDays": { "type": "integer", "minimum": 0, "maximum": 365, "nullable": true }
        }
      },
      "RfqListItem": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "publicRef": { "type": "string" },
          "status": { "type": "string" },
          "deliveryCountry": { "type": "string" },
          "lineCount": { "type": "integer" },
          "sentAt": { "type": "string", "format": "date-time", "nullable": true }
        }
      },
      "RfqLine": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "mpn": { "type": "string", "nullable": true },
          "quantity": { "type": "integer" }
        }
      },
      "RfqDetail": {
        "allOf": [
          { "$ref": "#/components/schemas/RfqListItem" },
          {
            "type": "object",
            "properties": {
              "lines": { "type": "array", "items": { "$ref": "#/components/schemas/RfqLine" } }
            }
          }
        ]
      },
      "CreateQuoteLineRequest": {
        "type": "object",
        "required": ["rfqLineId", "price", "quantity", "condition"],
        "properties": {
          "rfqLineId": { "type": "string", "format": "uuid" },
          "price": { "type": "number", "exclusiveMinimum": 0 },
          "quantity": { "type": "integer", "minimum": 1 },
          "condition": {
            "type": "string",
            "enum": ["new_oem", "new_surplus", "refurbished", "used"]
          },
          "leadTimeDays": { "type": "integer", "minimum": 0 }
        }
      },
      "CreateQuoteRequest": {
        "type": "object",
        "required": ["rfqId", "lines"],
        "properties": {
          "rfqId": { "type": "string", "format": "uuid" },
          "currency": { "type": "string", "minLength": 3, "maxLength": 3, "default": "EUR" },
          "lines": {
            "type": "array",
            "minItems": 1,
            "items": { "$ref": "#/components/schemas/CreateQuoteLineRequest" }
          }
        }
      },
      "CreateQuoteResponse": {
        "type": "object",
        "properties": { "quoteId": { "type": "string", "format": "uuid" } }
      }
    }
  }
}
