{
  "openapi": "3.1.0",
  "info": {
    "title": "Shop API",
    "version": "1.0",
    "description": "A small e-commerce flow: browse products, build a cart, apply coupons and check out. Out-of-stock items return 409, invalid coupons 422, expired coupons 410, and checkout honors Idempotency-Key so retries never double-charge."
  },
  "servers": [
    {
      "url": "https://funapi.dev/api/shop/v1"
    }
  ],
  "tags": [
    {
      "name": "products",
      "description": "things you do not need"
    },
    {
      "name": "carts",
      "description": "add, remove, regret"
    },
    {
      "name": "checkout",
      "description": "where the money pretends to move"
    }
  ],
  "paths": {
    "/products": {
      "get": {
        "tags": [
          "products"
        ],
        "summary": "List products, filter by category",
        "operationId": "sh-products",
        "responses": {
          "200": {
            "description": "Product list with live stock counts",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "category",
            "in": "query",
            "required": false,
            "description": "office, fashion, food or decor.",
            "schema": {
              "type": "string"
            }
          }
        ]
      }
    },
    "/products/{id}": {
      "get": {
        "tags": [
          "products"
        ],
        "summary": "Get one product",
        "operationId": "sh-product",
        "responses": {
          "200": {
            "description": "The product",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          },
          "404": {
            "description": "Unknown id",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Product id 1–6. Product 3 is permanently sold out.",
            "schema": {
              "type": "integer",
              "format": "int32"
            },
            "example": 1
          }
        ]
      }
    },
    "/coupons/{code}": {
      "get": {
        "tags": [
          "checkout"
        ],
        "summary": "Validate a coupon (410 when expired)",
        "operationId": "sh-coupon",
        "responses": {
          "200": {
            "description": "Coupon is valid",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          },
          "404": {
            "description": "Never existed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "410": {
            "description": "Existed once, expired since",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "code",
            "in": "path",
            "required": true,
            "description": "WELCOME10 works, EXPIRED50 is gone, anything else → 404.",
            "schema": {
              "type": "string"
            },
            "example": "WELCOME10"
          }
        ]
      }
    },
    "/carts": {
      "post": {
        "tags": [
          "carts"
        ],
        "summary": "Create an empty cart",
        "operationId": "sh-cart-create",
        "responses": {
          "201": {
            "description": "Cart created",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            },
            "headers": {
              "Location": {
                "schema": {
                  "type": "string",
                  "format": "uri-reference"
                }
              }
            }
          },
          "400": {
            "description": "Missing customer",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "customer": {
                    "type": "string"
                  }
                }
              },
              "example": {
                "customer": "Rachel Green"
              }
            }
          }
        }
      }
    },
    "/carts/{id}": {
      "get": {
        "tags": [
          "carts"
        ],
        "summary": "Get a cart with its running total",
        "operationId": "sh-cart",
        "responses": {
          "200": {
            "description": "The cart, items and total",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          },
          "404": {
            "description": "Unknown cart",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Cart id from POST /carts (301+).",
            "schema": {
              "type": "integer",
              "format": "int32"
            },
            "example": 301
          }
        ]
      }
    },
    "/carts/{id}/items": {
      "post": {
        "tags": [
          "carts"
        ],
        "summary": "Add a product (409 when out of stock)",
        "operationId": "sh-cart-add",
        "responses": {
          "200": {
            "description": "Item added, cart returned",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          },
          "400": {
            "description": "Bad qty",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Unknown cart or product",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Not enough stock (product 3 is always out)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Cart id.",
            "schema": {
              "type": "integer",
              "format": "int32"
            },
            "example": 301
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "productId": {
                    "type": "number"
                  },
                  "qty": {
                    "type": "number"
                  }
                }
              },
              "example": {
                "productId": 2,
                "qty": 1
              }
            }
          }
        }
      }
    },
    "/carts/{id}/items/{productId}": {
      "delete": {
        "tags": [
          "carts"
        ],
        "summary": "Remove a product from the cart",
        "operationId": "sh-cart-remove",
        "responses": {
          "200": {
            "description": "Item removed, cart returned",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          },
          "404": {
            "description": "Unknown cart, or product not in cart",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Cart id.",
            "schema": {
              "type": "integer",
              "format": "int32"
            },
            "example": 301
          },
          {
            "name": "productId",
            "in": "path",
            "required": true,
            "description": "Product to remove.",
            "schema": {
              "type": "integer",
              "format": "int32"
            },
            "example": 2
          }
        ]
      }
    },
    "/carts/{id}/checkout": {
      "post": {
        "tags": [
          "checkout"
        ],
        "summary": "Check out (idempotent, coupon-aware)",
        "operationId": "sh-checkout",
        "responses": {
          "201": {
            "description": "Order placed, stock decremented",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            },
            "headers": {
              "Location": {
                "schema": {
                  "type": "string",
                  "format": "uri-reference"
                }
              }
            }
          },
          "400": {
            "description": "Empty cart",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Unknown cart",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Cart already checked out, or stock ran out since adding",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "422": {
            "description": "Coupon invalid or expired",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Cart id.",
            "schema": {
              "type": "integer",
              "format": "int32"
            },
            "example": 301
          },
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "description": "Repeat the same key on retry and the original order is replayed — no double charge.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "coupon": {
                    "type": "string"
                  }
                }
              },
              "example": {
                "coupon": "WELCOME10"
              }
            }
          }
        }
      }
    },
    "/orders/{id}": {
      "get": {
        "tags": [
          "checkout"
        ],
        "summary": "Get an order",
        "operationId": "sh-order",
        "responses": {
          "200": {
            "description": "The order",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          },
          "404": {
            "description": "Unknown order",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Order id from checkout (8001+).",
            "schema": {
              "type": "integer",
              "format": "int32"
            },
            "example": 8001
          }
        ]
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "description": "Use qa-admin-token / qa-viewer-token, or a JWT from POST /auth/v1/login."
      },
      "apiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key",
        "description": "Cargo demo key: cargo-key-123."
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": [
          "error",
          "message"
        ],
        "properties": {
          "error": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "requestId": {
            "type": "string"
          }
        },
        "additionalProperties": true
      }
    }
  }
}
