{
  "openapi": "3.1.0",
  "info": {
    "title": "MTH AI Estimation API",
    "version": "1.0.0",
    "description": "AI-powered construction estimation platform for the Australian market"
  },
  "servers": [
    {
      "url": "/api"
    }
  ],
  "tags": [
    {
      "name": "Projects",
      "description": "Project management"
    },
    {
      "name": "Plans",
      "description": "Plan document upload and sheet management"
    },
    {
      "name": "Pipeline",
      "description": "AI analysis pipeline control and progress"
    },
    {
      "name": "Elements",
      "description": "Extracted construction elements"
    },
    {
      "name": "Estimates",
      "description": "Cost estimate retrieval"
    },
    {
      "name": "Review",
      "description": "Human review queue and statistics"
    },
    {
      "name": "Reports",
      "description": "PDF, Excel, and audit report generation"
    },
    {
      "name": "Audit",
      "description": "AI decision log"
    },
    {
      "name": "Messages",
      "description": "In-app pipeline messages and notifications"
    },
    {
      "name": "Rates",
      "description": "Rate resolution"
    },
    {
      "name": "Billing",
      "description": "Stripe billing and subscriptions"
    }
  ],
  "components": {
    "securitySchemes": {
      "cookieAuth": {
        "type": "apiKey",
        "in": "cookie",
        "name": "next-auth.session-token"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          }
        },
        "required": [
          "error"
        ]
      },
      "ProjectStatus": {
        "type": "string",
        "enum": [
          "DRAFT",
          "UPLOADING",
          "ANALYSING",
          "ESTIMATING",
          "IN_REVIEW",
          "APPROVED",
          "PUBLISHED",
          "ARCHIVED"
        ]
      },
      "BuildingType": {
        "type": "string",
        "enum": [
          "RESIDENTIAL",
          "COMMERCIAL",
          "INDUSTRIAL",
          "HIGH_RISE",
          "MIXED_USE",
          "INFRASTRUCTURE"
        ]
      },
      "Project": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "client_name": {
            "type": "string",
            "nullable": true
          },
          "status": {
            "$ref": "#/components/schemas/ProjectStatus"
          },
          "building_type": {
            "$ref": "#/components/schemas/BuildingType",
            "nullable": true
          },
          "gfa_sqm": {
            "type": "number",
            "nullable": true
          },
          "total_excl_gst": {
            "type": "number",
            "nullable": true
          },
          "cost_per_sqm": {
            "type": "number",
            "nullable": true
          },
          "address_suburb": {
            "type": "string",
            "nullable": true
          },
          "address_state": {
            "type": "string",
            "nullable": true
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "updated_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "PlanSheet": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "sheet_number": {
            "type": "string",
            "nullable": true
          },
          "sheet_title": {
            "type": "string",
            "nullable": true
          },
          "discipline": {
            "type": "string",
            "nullable": true
          },
          "scale": {
            "type": "string",
            "nullable": true
          },
          "confidence": {
            "type": "number",
            "nullable": true
          }
        }
      },
      "ExtractedElement": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "element_type": {
            "type": "string"
          },
          "quantity": {
            "type": "number",
            "nullable": true
          },
          "unit": {
            "type": "string",
            "nullable": true
          },
          "confidence": {
            "type": "number",
            "nullable": true
          },
          "reasoning": {
            "type": "string",
            "nullable": true
          }
        }
      },
      "ReviewItem": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "type": {
            "type": "string"
          },
          "severity": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "resolved": {
            "type": "boolean"
          },
          "resolved_at": {
            "type": "string",
            "format": "date-time",
            "nullable": true
          }
        }
      },
      "UserMessage": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "type": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "body": {
            "type": "string",
            "nullable": true
          },
          "link": {
            "type": "string",
            "nullable": true
          },
          "acknowledged_at": {
            "type": "string",
            "format": "date-time",
            "nullable": true
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      }
    }
  },
  "security": [
    {
      "cookieAuth": []
    }
  ],
  "paths": {
    "/projects": {
      "get": {
        "summary": "List projects",
        "description": "Returns all projects for the authenticated user's organisation, ordered by last update.",
        "tags": [
          "Projects"
        ],
        "parameters": [
          {
            "name": "status",
            "in": "query",
            "schema": {
              "$ref": "#/components/schemas/ProjectStatus"
            },
            "description": "Filter by project status"
          },
          {
            "name": "building_type",
            "in": "query",
            "schema": {
              "$ref": "#/components/schemas/BuildingType"
            },
            "description": "Filter by building type"
          },
          {
            "name": "page",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 1
            }
          },
          {
            "name": "per_page",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 20,
              "maximum": 100
            }
          }
        ],
        "responses": {
          "200": {
            "description": "List of projects",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "projects": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Project"
                      }
                    },
                    "total": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorised",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Create project",
        "description": "Creates a new estimation project in DRAFT status.",
        "tags": [
          "Projects"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "name"
                ],
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "client_name": {
                    "type": "string"
                  },
                  "building_type": {
                    "$ref": "#/components/schemas/BuildingType"
                  },
                  "gfa_sqm": {
                    "type": "number"
                  },
                  "address_suburb": {
                    "type": "string"
                  },
                  "address_state": {
                    "type": "string"
                  },
                  "description": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created project",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Project"
                }
              }
            }
          },
          "400": {
            "description": "Validation error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorised"
          }
        }
      }
    },
    "/projects/{id}": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          },
          "description": "Project ID (cuid)"
        }
      ],
      "get": {
        "summary": "Get project",
        "description": "Returns a single project with full detail.",
        "tags": [
          "Projects"
        ],
        "responses": {
          "200": {
            "description": "Project",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Project"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorised"
          },
          "403": {
            "description": "Forbidden — not a project member"
          },
          "404": {
            "description": "Not found"
          }
        }
      },
      "patch": {
        "summary": "Update project",
        "description": "Updates mutable project fields. Cannot change status directly via PATCH — use pipeline trigger endpoints.",
        "tags": [
          "Projects"
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "client_name": {
                    "type": "string"
                  },
                  "building_type": {
                    "$ref": "#/components/schemas/BuildingType"
                  },
                  "gfa_sqm": {
                    "type": "number"
                  },
                  "address_suburb": {
                    "type": "string"
                  },
                  "address_state": {
                    "type": "string"
                  },
                  "description": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Updated project",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Project"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorised"
          },
          "403": {
            "description": "Forbidden"
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/projects/{id}/upload": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "post": {
        "summary": "Upload plan files",
        "description": "Accepts multipart/form-data with one or more PDF or DWG plan files. Files are stored in R2 and plan_documents records are created.",
        "tags": [
          "Plans"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "files": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "format": "binary"
                    },
                    "description": "Plan files (PDF or DWG, max 50 MB each)"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Upload result",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "uploaded": {
                      "type": "integer"
                    },
                    "documents": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": {
                            "type": "string"
                          },
                          "filename": {
                            "type": "string"
                          },
                          "size_bytes": {
                            "type": "integer"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "No files or invalid file type"
          },
          "401": {
            "description": "Unauthorised"
          },
          "403": {
            "description": "Forbidden"
          }
        }
      }
    },
    "/projects/{id}/analyse": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "post": {
        "summary": "Start pipeline analysis",
        "description": "Triggers the 17-layer AI analysis pipeline for the project. Returns immediately; progress can be monitored via the SSE /progress endpoint.",
        "tags": [
          "Pipeline"
        ],
        "responses": {
          "202": {
            "description": "Analysis started",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "jobId": {
                      "type": "string",
                      "nullable": true
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "No plan documents uploaded"
          },
          "401": {
            "description": "Unauthorised"
          },
          "403": {
            "description": "Forbidden"
          }
        }
      }
    },
    "/projects/{id}/progress": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "SSE progress stream",
        "description": "Server-Sent Events stream emitting pipeline progress updates. Events are JSON objects with `stage`, `progress` (0–100), and optional `message` fields.",
        "tags": [
          "Pipeline"
        ],
        "responses": {
          "200": {
            "description": "SSE stream",
            "content": {
              "text/event-stream": {
                "schema": {
                  "type": "string",
                  "description": "Newline-delimited SSE events. Each `data:` payload is JSON: { stage: string, progress: number, message?: string, done?: boolean }"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorised"
          },
          "404": {
            "description": "Project not found"
          }
        }
      }
    },
    "/projects/{id}/sheets": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "List plan sheets",
        "description": "Returns all classified plan sheets extracted from uploaded documents.",
        "tags": [
          "Plans"
        ],
        "responses": {
          "200": {
            "description": "Plan sheets",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "sheets": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/PlanSheet"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorised"
          },
          "403": {
            "description": "Forbidden"
          }
        }
      }
    },
    "/projects/{id}/elements": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "List extracted elements",
        "description": "Returns all construction elements extracted by the AI pipeline, with quantities, units, and confidence scores.",
        "tags": [
          "Elements"
        ],
        "parameters": [
          {
            "name": "trade",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Filter by trade code"
          },
          {
            "name": "min_confidence",
            "in": "query",
            "schema": {
              "type": "number",
              "minimum": 0,
              "maximum": 1
            },
            "description": "Minimum confidence threshold"
          }
        ],
        "responses": {
          "200": {
            "description": "Extracted elements",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "elements": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/ExtractedElement"
                      }
                    },
                    "total": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorised"
          },
          "403": {
            "description": "Forbidden"
          }
        }
      }
    },
    "/projects/{id}/estimate": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "Get estimate",
        "description": "Returns the full cost estimate broken down by trade, with line items, rates, quantities, and confidence scores in AUD.",
        "tags": [
          "Estimates"
        ],
        "responses": {
          "200": {
            "description": "Estimate",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "estimateId": {
                      "type": "string"
                    },
                    "total_excl_gst": {
                      "type": "number"
                    },
                    "total_incl_gst": {
                      "type": "number"
                    },
                    "cost_per_sqm": {
                      "type": "number",
                      "nullable": true
                    },
                    "overall_confidence": {
                      "type": "number"
                    },
                    "trades": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "trade_code": {
                            "type": "string"
                          },
                          "trade_name": {
                            "type": "string"
                          },
                          "subtotal_excl_gst": {
                            "type": "number"
                          },
                          "line_items": {
                            "type": "array",
                            "items": {
                              "type": "object",
                              "properties": {
                                "item_code": {
                                  "type": "string"
                                },
                                "description": {
                                  "type": "string"
                                },
                                "quantity": {
                                  "type": "number"
                                },
                                "unit": {
                                  "type": "string"
                                },
                                "rate": {
                                  "type": "number"
                                },
                                "total": {
                                  "type": "number"
                                },
                                "confidence": {
                                  "type": "number"
                                },
                                "reasoning": {
                                  "type": "string",
                                  "nullable": true
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorised"
          },
          "403": {
            "description": "Forbidden"
          },
          "404": {
            "description": "Estimate not yet generated for this project"
          }
        }
      }
    },
    "/projects/{id}/review": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "List review items",
        "description": "Returns all flagged review items requiring human validation, ordered by severity.",
        "tags": [
          "Review"
        ],
        "parameters": [
          {
            "name": "resolved",
            "in": "query",
            "schema": {
              "type": "boolean"
            },
            "description": "Filter by resolved status (default: false)"
          }
        ],
        "responses": {
          "200": {
            "description": "Review items",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "items": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/ReviewItem"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorised"
          },
          "403": {
            "description": "Forbidden"
          }
        }
      }
    },
    "/projects/{id}/review/stats": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "Review statistics",
        "description": "Returns counts of open, resolved, and critical review items for the project.",
        "tags": [
          "Review"
        ],
        "responses": {
          "200": {
            "description": "Review stats",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "total": {
                      "type": "integer"
                    },
                    "open": {
                      "type": "integer"
                    },
                    "resolved": {
                      "type": "integer"
                    },
                    "critical": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorised"
          },
          "403": {
            "description": "Forbidden"
          }
        }
      }
    },
    "/projects/{id}/report/pdf": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "post": {
        "summary": "Generate PDF report",
        "description": "Generates a formatted PDF cost estimate report using Playwright. Returns a signed R2 download URL valid for 1 hour.",
        "tags": [
          "Reports"
        ],
        "responses": {
          "200": {
            "description": "PDF download URL",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "url": {
                      "type": "string",
                      "format": "uri"
                    },
                    "expires_at": {
                      "type": "string",
                      "format": "date-time"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorised"
          },
          "403": {
            "description": "Forbidden"
          },
          "404": {
            "description": "Estimate not found"
          }
        }
      }
    },
    "/projects/{id}/report/excel": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "post": {
        "summary": "Generate Excel report",
        "description": "Generates an XLSX cost estimate workbook. Returns a signed R2 download URL valid for 1 hour.",
        "tags": [
          "Reports"
        ],
        "responses": {
          "200": {
            "description": "Excel download URL",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "url": {
                      "type": "string",
                      "format": "uri"
                    },
                    "expires_at": {
                      "type": "string",
                      "format": "date-time"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorised"
          },
          "403": {
            "description": "Forbidden"
          },
          "404": {
            "description": "Estimate not found"
          }
        }
      }
    },
    "/projects/{id}/report/audit": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "Download audit report",
        "description": "Streams a JSON audit report containing all AI decisions, confidence scores, and reasoning for every line item.",
        "tags": [
          "Reports"
        ],
        "responses": {
          "200": {
            "description": "Audit report JSON",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "description": "Full audit report with AI decision trail"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorised"
          },
          "403": {
            "description": "Forbidden"
          }
        }
      }
    },
    "/projects/{id}/audit-log": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "AI decision log",
        "description": "Returns the paginated AI decision log for a project — every layer decision, prompt, and extracted value.",
        "tags": [
          "Audit"
        ],
        "parameters": [
          {
            "name": "layer",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 17
            },
            "description": "Filter by pipeline layer number"
          },
          {
            "name": "page",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 1
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Decision log entries",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "entries": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": {
                            "type": "string"
                          },
                          "layer": {
                            "type": "integer"
                          },
                          "decision_type": {
                            "type": "string"
                          },
                          "input_summary": {
                            "type": "string",
                            "nullable": true
                          },
                          "output_summary": {
                            "type": "string",
                            "nullable": true
                          },
                          "confidence": {
                            "type": "number",
                            "nullable": true
                          },
                          "created_at": {
                            "type": "string",
                            "format": "date-time"
                          }
                        }
                      }
                    },
                    "total": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorised"
          },
          "403": {
            "description": "Forbidden"
          }
        }
      }
    },
    "/projects/{id}/user-messages": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "Pipeline messages",
        "description": "Returns pipeline-generated messages for a specific project (analysis complete, errors, warnings).",
        "tags": [
          "Messages"
        ],
        "responses": {
          "200": {
            "description": "Messages",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "messages": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/UserMessage"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorised"
          },
          "403": {
            "description": "Forbidden"
          }
        }
      }
    },
    "/rates/resolve/{itemCode}": {
      "parameters": [
        {
          "name": "itemCode",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          },
          "description": "Trade line item code (e.g. CONC-SLAB-100)",
          "example": "CONC-SLAB-100"
        }
      ],
      "get": {
        "summary": "Resolve rate for item",
        "description": "Resolves the current effective rate for a trade line item code, applying region, building type, and calibration factor overrides.",
        "tags": [
          "Rates"
        ],
        "parameters": [
          {
            "name": "region",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "State/region code (e.g. NSW, VIC)",
            "example": "NSW"
          },
          {
            "name": "building_type",
            "in": "query",
            "schema": {
              "$ref": "#/components/schemas/BuildingType"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Resolved rate",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "item_code": {
                      "type": "string"
                    },
                    "rate": {
                      "type": "number"
                    },
                    "unit": {
                      "type": "string"
                    },
                    "source": {
                      "type": "string",
                      "enum": [
                        "DB",
                        "RAWLINSONS",
                        "BENCHMARK",
                        "FALLBACK"
                      ]
                    },
                    "confidence": {
                      "type": "number"
                    },
                    "region": {
                      "type": "string",
                      "nullable": true
                    },
                    "building_type": {
                      "type": "string",
                      "nullable": true
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Item code not found"
          }
        }
      }
    },
    "/billing/checkout": {
      "post": {
        "summary": "Create Stripe checkout session",
        "description": "Creates a Stripe Checkout session for plan subscription. Returns the checkout URL to redirect the user to.",
        "tags": [
          "Billing"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "priceId"
                ],
                "properties": {
                  "priceId": {
                    "type": "string",
                    "description": "Stripe Price ID"
                  },
                  "successUrl": {
                    "type": "string",
                    "format": "uri",
                    "description": "Redirect URL on successful payment"
                  },
                  "cancelUrl": {
                    "type": "string",
                    "format": "uri",
                    "description": "Redirect URL on cancelled payment"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Checkout session",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "url": {
                      "type": "string",
                      "format": "uri",
                      "description": "Stripe Checkout URL"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Missing priceId"
          },
          "401": {
            "description": "Unauthorised"
          }
        }
      }
    }
  }
}