{
  "openapi": "3.1.0",
  "info": {
    "title": "CrystalCut API",
    "description": "Public partner API for AI product photo processing. Debits platform credits only on SUCCESS. Partners (e.g. Sellvia) pass external_user_id + idempotency_key and never call AI engines directly. See docs/PARTNER_API.md.",
    "version": "1.1.0",
    "contact": { "name": "CrystalCut" }
  },
  "servers": [
    {
      "url": "https://crystalcut.pl",
      "description": "Production"
    },
    {
      "url": "http://localhost:3000",
      "description": "Local development"
    }
  ],
  "paths": {
    "/api/v1/remove-bg": {
      "post": {
        "operationId": "removeBackground",
        "summary": "Remove background and frame product photo",
        "description": "Removes background, frames the product, returns image_url. Debits 1 credit (cut) or 2 (studio) only on SUCCESS. Supports partner idempotency and external user metering.",
        "tags": ["Processing"],
        "security": [{ "BearerAuth": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/RemoveBgRequest" },
              "examples": {
                "partner": {
                  "summary": "Sellvia / partner call",
                  "value": {
                    "image_url": "https://example.com/product.jpg",
                    "mode": "cut",
                    "source": "sellvia",
                    "external_user_id": "user_abc",
                    "external_org_id": "shop_xyz",
                    "idempotency_key": "job_20260805_001",
                    "metadata": { "offer_id": "123" }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Processing succeeded",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/RemoveBgSuccess" }
              }
            }
          },
          "400": {
            "description": "Invalid request",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ApiError" }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ApiError" }
              }
            }
          },
          "402": {
            "description": "Insufficient credits on the API key account",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ApiError" }
              }
            }
          },
          "429": {
            "description": "Rate limit",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ApiError" }
              }
            }
          },
          "500": {
            "description": "Processing failed (no debit)",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ApiError" }
              }
            }
          },
          "503": {
            "description": "Service unavailable",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ApiError" }
              }
            }
          }
        }
      }
    },
    "/api/v1/generations/{id}": {
      "get": {
        "operationId": "getGeneration",
        "summary": "Get generation status",
        "tags": ["Jobs"],
        "security": [{ "BearerAuth": [] }],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Generation found",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/GenerationResponse" }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ApiError" }
              }
            }
          },
          "404": {
            "description": "Not found",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ApiError" }
              }
            }
          }
        }
      }
    },
    "/api/v1/usage": {
      "get": {
        "operationId": "getUsage",
        "summary": "Usage report for partner reconciliation",
        "description": "Credits charged and job counts for the API key account. Filter by external_user_id / source (e.g. sellvia).",
        "tags": ["Billing"],
        "security": [{ "BearerAuth": [] }],
        "parameters": [
          {
            "name": "from",
            "in": "query",
            "schema": { "type": "string", "example": "2026-08-01" }
          },
          {
            "name": "to",
            "in": "query",
            "schema": { "type": "string", "example": "2026-08-31" }
          },
          {
            "name": "external_user_id",
            "in": "query",
            "schema": { "type": "string" }
          },
          {
            "name": "external_org_id",
            "in": "query",
            "schema": { "type": "string" }
          },
          {
            "name": "source",
            "in": "query",
            "schema": { "type": "string", "example": "sellvia" }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": { "type": "integer", "default": 50, "maximum": 200 }
          }
        ],
        "responses": {
          "200": {
            "description": "Usage summary",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/UsageResponse" }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ApiError" }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "cbg_live_… / cbg_test_…",
        "description": "API key from Dashboard → API Keys (platform account for partners)."
      }
    },
    "schemas": {
      "RemoveBgRequest": {
        "type": "object",
        "properties": {
          "image_url": {
            "type": "string",
            "format": "uri",
            "description": "Public HTTPS URL of the source image."
          },
          "image_b64": {
            "type": "string",
            "description": "Raw base64 image (no data: prefix). Max ~12MB."
          },
          "fill_ratio": {
            "type": "number",
            "minimum": 0.5,
            "maximum": 1,
            "default": 0.88
          },
          "canvas_size": {
            "type": "integer",
            "minimum": 512,
            "maximum": 2048,
            "default": 1024
          },
          "mode": {
            "type": "string",
            "enum": ["cut", "studio"],
            "default": "cut",
            "description": "cut = 1 credit; studio = 2 credits"
          },
          "idempotency_key": {
            "type": "string",
            "minLength": 8,
            "maxLength": 128,
            "description": "Same key + same account → no double charge"
          },
          "external_user_id": {
            "type": "string",
            "description": "Partner end-user id (e.g. Sellvia user)"
          },
          "external_org_id": {
            "type": "string",
            "description": "Partner shop / org id"
          },
          "source": {
            "type": "string",
            "description": "Partner label, e.g. sellvia",
            "example": "sellvia"
          },
          "metadata": {
            "type": "object",
            "additionalProperties": true,
            "description": "Free-form partner metadata (no secrets)"
          }
        },
        "anyOf": [
          { "required": ["image_url"] },
          { "required": ["image_b64"] }
        ]
      },
      "RemoveBgSuccess": {
        "type": "object",
        "required": [
          "status",
          "id",
          "image_url",
          "mode",
          "credits_charged",
          "credits_remaining"
        ],
        "properties": {
          "status": { "type": "string", "enum": ["success"] },
          "id": { "type": "string" },
          "image_url": { "type": "string", "format": "uri" },
          "mode": { "type": "string", "enum": ["cut", "studio"] },
          "fill_ratio": { "type": "number" },
          "canvas_size": { "type": "integer" },
          "credits_charged": {
            "type": "integer",
            "description": "0 on idempotent replay"
          },
          "credits_remaining": { "type": "integer" },
          "idempotent_replay": { "type": "boolean" },
          "external_user_id": { "type": "string" },
          "external_org_id": { "type": "string" }
        }
      },
      "GenerationResponse": {
        "type": "object",
        "properties": {
          "status": { "type": "string", "enum": ["success"] },
          "generation": {
            "type": "object",
            "properties": {
              "id": { "type": "string" },
              "job_status": {
                "type": "string",
                "enum": ["PENDING", "PROCESSING", "SUCCESS", "FAILED"]
              },
              "image_url": { "type": "string", "nullable": true },
              "credits_charged": { "type": "integer" },
              "credits_remaining": { "type": "integer" },
              "external_user_id": { "type": "string", "nullable": true },
              "external_org_id": { "type": "string", "nullable": true },
              "source": { "type": "string", "nullable": true },
              "created_at": { "type": "string", "format": "date-time" }
            }
          }
        }
      },
      "UsageResponse": {
        "type": "object",
        "properties": {
          "status": { "type": "string", "enum": ["success"] },
          "period": {
            "type": "object",
            "properties": {
              "from": { "type": "string" },
              "to": { "type": "string" }
            }
          },
          "account": {
            "type": "object",
            "properties": {
              "credits_remaining": { "type": "integer" }
            }
          },
          "summary": {
            "type": "object",
            "properties": {
              "successful_jobs": { "type": "integer" },
              "failed_jobs": { "type": "integer" },
              "credits_charged": { "type": "integer" }
            }
          },
          "by_external_user": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "external_user_id": { "type": "string" },
                "successful_jobs": { "type": "integer" },
                "credits_charged": { "type": "integer" }
              }
            }
          },
          "recent_jobs": { "type": "array", "items": { "type": "object" } }
        }
      },
      "ApiError": {
        "type": "object",
        "required": ["status", "code", "message"],
        "properties": {
          "status": { "type": "string", "enum": ["error"] },
          "code": {
            "type": "string",
            "enum": [
              "MISSING_API_KEY",
              "INVALID_API_KEY",
              "NO_CREDITS",
              "RATE_LIMIT",
              "INVALID_REQUEST",
              "NOT_FOUND",
              "PROCESSING_FAILED",
              "SERVICE_UNAVAILABLE",
              "INTERNAL_ERROR"
            ]
          },
          "message": { "type": "string" },
          "details": {},
          "id": { "type": "string" },
          "credits_remaining": { "type": "integer" }
        }
      }
    }
  }
}
