{
 "openapi": "3.0.3",
 "info": {
  "title": "Dowozik API",
  "version": "1.1.0",
  "description": "Zlecenia dostaw dla restauracji. Dokumentacja: https://dowozik.app/api-docs.html"
 },
 "servers": [
  {
   "url": "https://dowozik.app/api/v1"
  }
 ],
 "security": [
  {
   "bearer": []
  }
 ],
 "components": {
  "securitySchemes": {
   "bearer": {
    "type": "http",
    "scheme": "bearer",
    "description": "dwz_live_… (produkcja), dwz_test_… (tryb testowy) albo dwz_partner_… + nagłówek X-Restaurant-Id"
   }
  },
  "parameters": {
   "RestaurantId": {
    "name": "X-Restaurant-Id",
    "in": "header",
    "required": false,
    "schema": {
     "type": "string"
    },
    "description": "Wymagany przy kluczu partnera"
   }
  },
  "schemas": {
   "StopInput": {
    "type": "object",
    "properties": {
     "address": {
      "type": "string",
      "example": "Zwycięstwa 12, 44-100 Gliwice"
     },
     "lat": {
      "type": "number"
     },
     "lon": {
      "type": "number"
     },
     "apartment": {
      "type": "string"
     },
     "phone": {
      "type": "string"
     },
     "note": {
      "type": "string"
     },
     "cashPln": {
      "type": "number",
      "description": "Gotówka do pobrania"
     },
     "cardPln": {
      "type": "number",
      "description": "Płatność kartą u kuriera"
     }
    },
    "description": "Podaj address albo lat+lon."
   },
   "Order": {
    "type": "object",
    "properties": {
     "id": {
      "type": "string"
     },
     "externalId": {
      "type": "string",
      "nullable": true
     },
     "source": {
      "type": "string"
     },
     "test": {
      "type": "boolean",
      "description": "Tylko zlecenia testowe"
     },
     "status": {
      "type": "string",
      "enum": [
       "scheduled",
       "searching_courier",
       "courier_assigned",
       "courier_at_restaurant",
       "picked_up",
       "delivered",
       "cancelled"
      ]
     },
     "createdAt": {
      "type": "string",
      "format": "date-time"
     },
     "scheduledAt": {
      "type": "string",
      "format": "date-time",
      "nullable": true
     },
     "readyAt": {
      "type": "string",
      "format": "date-time",
      "nullable": true
     },
     "pricePln": {
      "type": "number"
     },
     "distanceMeters": {
      "type": "integer"
     },
     "courier": {
      "type": "object",
      "nullable": true,
      "properties": {
       "name": {
        "type": "string"
       },
       "phone": {
        "type": "string"
       },
       "etaMinutes": {
        "type": "integer",
        "nullable": true
       }
      }
     },
     "timeline": {
      "type": "object",
      "properties": {
       "assignedAt": {
        "type": "string",
        "format": "date-time",
        "nullable": true
       },
       "atRestaurantAt": {
        "type": "string",
        "format": "date-time",
        "nullable": true
       },
       "pickedUpAt": {
        "type": "string",
        "format": "date-time",
        "nullable": true
       },
       "completedAt": {
        "type": "string",
        "format": "date-time",
        "nullable": true
       },
       "cancelledAt": {
        "type": "string",
        "format": "date-time",
        "nullable": true
       }
      }
     },
     "stops": {
      "type": "array",
      "items": {
       "type": "object",
       "properties": {
        "id": {
         "type": "string"
        },
        "address": {
         "type": "string"
        },
        "apartment": {
         "type": "string"
        },
        "phone": {
         "type": "string"
        },
        "cashPln": {
         "type": "number"
        },
        "cardPln": {
         "type": "number"
        },
        "status": {
         "type": "string",
         "enum": [
          "pending",
          "delivered",
          "cancelled",
          "failed"
         ]
        },
        "deliveredAt": {
         "type": "string",
         "format": "date-time",
         "nullable": true
        },
        "trackingUrl": {
         "type": "string",
         "nullable": true
        }
       }
      }
     }
    }
   },
   "Error": {
    "type": "object",
    "properties": {
     "error": {
      "type": "string"
     },
     "code": {
      "type": "string"
     }
    }
   },
   "Quote": {
    "type": "object",
    "properties": {
     "pricePln": {
      "type": "number"
     },
     "currency": {
      "type": "string"
     },
     "distanceMeters": {
      "type": "integer"
     },
     "available": {
      "type": "boolean"
     },
     "freeCouriers": {
      "type": "integer"
     },
     "courierToRestaurantMinutes": {
      "type": "integer",
      "nullable": true
     },
     "estimatedPickupAt": {
      "type": "string",
      "format": "date-time",
      "nullable": true
     },
     "estimatedDeliveryAt": {
      "type": "string",
      "format": "date-time",
      "nullable": true
     },
     "validUntil": {
      "type": "string",
      "format": "date-time"
     }
    }
   },
   "CourierLocation": {
    "type": "object",
    "properties": {
     "lat": {
      "type": "number"
     },
     "lon": {
      "type": "number"
     },
     "accuracy": {
      "type": "number",
      "nullable": true
     },
     "heading": {
      "type": "number",
      "nullable": true
     },
     "at": {
      "type": "string",
      "format": "date-time"
     }
    }
   },
   "WebhookStatus": {
    "type": "object",
    "properties": {
     "event": {
      "type": "string",
      "enum": [
       "order.status"
      ]
     },
     "sentAt": {
      "type": "string"
     },
     "restaurantId": {
      "type": "string"
     },
     "previousStatus": {
      "type": "string",
      "nullable": true
     },
     "test": {
      "type": "boolean"
     },
     "order": {
      "$ref": "#/components/schemas/Order"
     }
    }
   },
   "WebhookStop": {
    "type": "object",
    "properties": {
     "event": {
      "type": "string",
      "enum": [
       "order.stop"
      ]
     },
     "sentAt": {
      "type": "string"
     },
     "restaurantId": {
      "type": "string"
     },
     "orderId": {
      "type": "string"
     },
     "externalId": {
      "type": "string",
      "nullable": true
     },
     "previousStatus": {
      "type": "string"
     },
     "stop": {
      "type": "object"
     }
    }
   }
  }
 },
 "paths": {
  "/quotes": {
   "post": {
    "summary": "Wycena i szacowany czas (bez tworzenia zlecenia)",
    "parameters": [
     {
      "$ref": "#/components/parameters/RestaurantId"
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "required": [
         "stops"
        ],
        "properties": {
         "externalId": {
          "type": "string"
         },
         "stops": {
          "type": "array",
          "minItems": 1,
          "maxItems": 25,
          "items": {
           "$ref": "#/components/schemas/StopInput"
          }
         },
         "readyInMin": {
          "type": "integer",
          "minimum": 0,
          "maximum": 180
         },
         "scheduledAt": {
          "type": "string",
          "format": "date-time"
         },
         "pool": {
          "type": "string",
          "enum": [
           "self",
           "ondemand",
           "public"
          ]
         },
         "returnToRestaurant": {
          "type": "boolean"
         },
         "source": {
          "type": "string"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Quote"
        }
       }
      }
     },
     "400": {
      "description": "Niepoprawne dane",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "401": {
      "description": "Zły klucz",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "404": {
      "description": "Nie znaleziono",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "429": {
      "description": "Limit zapytań",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   }
  },
  "/orders": {
   "post": {
    "summary": "Utwórz zlecenie",
    "parameters": [
     {
      "$ref": "#/components/parameters/RestaurantId"
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "required": [
         "stops"
        ],
        "properties": {
         "externalId": {
          "type": "string"
         },
         "stops": {
          "type": "array",
          "minItems": 1,
          "maxItems": 25,
          "items": {
           "$ref": "#/components/schemas/StopInput"
          }
         },
         "readyInMin": {
          "type": "integer",
          "minimum": 0,
          "maximum": 180
         },
         "scheduledAt": {
          "type": "string",
          "format": "date-time"
         },
         "pool": {
          "type": "string",
          "enum": [
           "self",
           "ondemand",
           "public"
          ]
         },
         "returnToRestaurant": {
          "type": "boolean"
         },
         "source": {
          "type": "string"
         }
        }
       }
      }
     }
    },
    "responses": {
     "201": {
      "description": "Utworzone",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Order"
        }
       }
      }
     },
     "200": {
      "description": "Już istniało (ten sam externalId)",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Order"
        }
       }
      }
     },
     "400": {
      "description": "Niepoprawne dane",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "401": {
      "description": "Zły klucz",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "404": {
      "description": "Nie znaleziono",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "429": {
      "description": "Limit zapytań",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "422": {
      "description": "Nie znaleziono adresu",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   },
   "get": {
    "summary": "Lista zleceń",
    "parameters": [
     {
      "$ref": "#/components/parameters/RestaurantId"
     },
     {
      "name": "since",
      "in": "query",
      "schema": {
       "type": "string",
       "format": "date-time"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "properties": {
          "orders": {
           "type": "array",
           "items": {
            "$ref": "#/components/schemas/Order"
           }
          }
         }
        }
       }
      }
     },
     "400": {
      "description": "Niepoprawne dane",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "401": {
      "description": "Zły klucz",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "404": {
      "description": "Nie znaleziono",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "429": {
      "description": "Limit zapytań",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   }
  },
  "/orders/{id}": {
   "get": {
    "summary": "Pobierz zlecenie",
    "parameters": [
     {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
       "type": "string"
      },
      "description": "id zlecenia albo Twój externalId"
     },
     {
      "$ref": "#/components/parameters/RestaurantId"
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Order"
        }
       }
      }
     },
     "400": {
      "description": "Niepoprawne dane",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "401": {
      "description": "Zły klucz",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "404": {
      "description": "Nie znaleziono",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "429": {
      "description": "Limit zapytań",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   },
   "patch": {
    "summary": "Zmień zlecenie: opóźnienie, telefon, mieszkanie, uwagi",
    "parameters": [
     {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
       "type": "string"
      },
      "description": "id zlecenia albo Twój externalId"
     },
     {
      "$ref": "#/components/parameters/RestaurantId"
     }
    ],
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "delayMin": {
          "type": "integer",
          "minimum": 1,
          "maximum": 120
         },
         "stops": {
          "type": "array",
          "items": {
           "type": "object",
           "properties": {
            "id": {
             "type": "string"
            },
            "phone": {
             "type": "string"
            },
            "apartment": {
             "type": "string"
            },
            "note": {
             "type": "string"
            }
           }
          }
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Order"
        }
       }
      }
     },
     "400": {
      "description": "Niepoprawne dane",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "401": {
      "description": "Zły klucz",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "404": {
      "description": "Nie znaleziono",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "429": {
      "description": "Limit zapytań",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "409": {
      "description": "Za późno na zmianę",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   }
  },
  "/orders/{id}/cancel": {
   "post": {
    "summary": "Anuluj (tylko przed przyjęciem przez kuriera)",
    "parameters": [
     {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
       "type": "string"
      },
      "description": "id zlecenia albo Twój externalId"
     },
     {
      "$ref": "#/components/parameters/RestaurantId"
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Order"
        }
       }
      }
     },
     "400": {
      "description": "Niepoprawne dane",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "401": {
      "description": "Zły klucz",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "404": {
      "description": "Nie znaleziono",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "429": {
      "description": "Limit zapytań",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "409": {
      "description": "Kurier już przyjął",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   }
  },
  "/orders/{id}/courier": {
   "get": {
    "summary": "Pozycja kuriera w trakcie kursu",
    "parameters": [
     {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
       "type": "string"
      },
      "description": "id zlecenia albo Twój externalId"
     },
     {
      "$ref": "#/components/parameters/RestaurantId"
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/CourierLocation"
        }
       }
      }
     },
     "400": {
      "description": "Niepoprawne dane",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "401": {
      "description": "Zły klucz",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "404": {
      "description": "Nie znaleziono",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "429": {
      "description": "Limit zapytań",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   }
  },
  "/orders/{id}/simulate": {
   "post": {
    "summary": "Tylko klucz testowy: wymuś status",
    "parameters": [
     {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
       "type": "string"
      },
      "description": "id zlecenia albo Twój externalId"
     }
    ],
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "status": {
          "type": "string",
          "enum": [
           "searching_courier",
           "courier_assigned",
           "courier_at_restaurant",
           "picked_up",
           "delivered",
           "cancelled"
          ]
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Order"
        }
       }
      }
     },
     "400": {
      "description": "Niepoprawne dane",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "401": {
      "description": "Zły klucz",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "404": {
      "description": "Nie znaleziono",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "429": {
      "description": "Limit zapytań",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   }
  },
  "/me": {
   "get": {
    "summary": "Dane restauracji dla klucza",
    "parameters": [
     {
      "$ref": "#/components/parameters/RestaurantId"
     }
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "400": {
      "description": "Niepoprawne dane",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "401": {
      "description": "Zły klucz",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "404": {
      "description": "Nie znaleziono",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "429": {
      "description": "Limit zapytań",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   }
  },
  "/partner/connect": {
   "post": {
    "summary": "Partner: podłącz restaurację kodem z jej panelu",
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "code": {
          "type": "string"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "restaurantId i nazwa"
     },
     "400": {
      "description": "Niepoprawne dane",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "401": {
      "description": "Zły klucz",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "404": {
      "description": "Nie znaleziono",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "429": {
      "description": "Limit zapytań",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   }
  },
  "/partner/restaurants": {
   "get": {
    "summary": "Partner: lista podłączonych restauracji",
    "responses": {
     "200": {
      "description": "OK"
     },
     "400": {
      "description": "Niepoprawne dane",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "401": {
      "description": "Zły klucz",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "404": {
      "description": "Nie znaleziono",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "429": {
      "description": "Limit zapytań",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   }
  }
 }
}