{
  "openapi": "3.0.3",
  "info": {
    "title": "Klimatkalkylatorn REST API",
    "version": "1.0.0",
    "description": "Read-only REST API for retrieving projects, project materials and custom EPDs scoped to an organization. Authenticate using an organization-scoped API key sent in the X-API-Key header.",
    "contact": {
      "name": "Klimatkalkylatorn",
      "url": "https://klimatkalkylatorn.com"
    },
    "license": { "name": "Proprietary" }
  },
  "servers": [
    {
      "url": "https://rakeqijnhsfexbsleygm.supabase.co/functions/v1/public-api",
      "description": "Production"
    }
  ],
  "security": [{ "ApiKeyAuth": [] }],
  "tags": [
    { "name": "Projects", "description": "Building projects" },
    { "name": "Materials", "description": "Project materials" },
    { "name": "EPDs", "description": "Custom Environmental Product Declarations" }
  ],
  "paths": {
    "/projects": {
      "get": {
        "tags": ["Projects"],
        "summary": "List projects",
        "description": "Returns all projects for the organization associated with the API key, ordered by created_at descending.",
        "operationId": "listProjects",
        "responses": {
          "200": {
            "description": "List of projects",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/ProjectSummary" }
                    }
                  }
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    },
    "/projects/{id}": {
      "get": {
        "tags": ["Projects"],
        "summary": "Get project by id",
        "description": "Returns a single project including its materials.",
        "operationId": "getProject",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" }
          }
        ],
        "responses": {
          "200": {
            "description": "Project detail with materials",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": { "$ref": "#/components/schemas/ProjectDetail" }
                  }
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "404": { "$ref": "#/components/responses/NotFound" }
        }
      }
    },
    "/projects/{id}/materials": {
      "get": {
        "tags": ["Materials"],
        "summary": "List materials for a project",
        "operationId": "listProjectMaterials",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" }
          }
        ],
        "responses": {
          "200": {
            "description": "Materials for the project",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/ProjectMaterial" }
                    }
                  }
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    },
    "/epds": {
      "get": {
        "tags": ["EPDs"],
        "summary": "List custom EPDs",
        "description": "Returns all custom EPDs for the organization.",
        "operationId": "listEpds",
        "responses": {
          "200": {
            "description": "List of custom EPDs",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/CustomEpd" }
                    }
                  }
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key",
        "description": "Organization-scoped API key. Create one in the app under Organization settings."
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "Missing or invalid API key",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" },
            "example": { "error": "Invalid or revoked API key" }
          }
        }
      },
      "NotFound": {
        "description": "Resource not found",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" },
            "example": { "error": "Not found" }
          }
        }
      },
      "ServerError": {
        "description": "Internal server error",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" },
            "example": { "error": "Internal server error" }
          }
        }
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": { "error": { "type": "string" } },
        "required": ["error"]
      },
      "ProjectSummary": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "name": { "type": "string" },
          "building_type": { "type": "string", "nullable": true },
          "property_designation": { "type": "string", "nullable": true },
          "gross_floor_area": { "type": "number", "nullable": true },
          "status": { "type": "string", "nullable": true },
          "created_at": { "type": "string", "format": "date-time" },
          "updated_at": { "type": "string", "format": "date-time" }
        }
      },
      "ProjectDetail": {
        "allOf": [
          { "$ref": "#/components/schemas/ProjectSummary" },
          {
            "type": "object",
            "properties": {
              "organization_id": { "type": "string", "format": "uuid" },
              "user_id": { "type": "string", "format": "uuid", "nullable": true },
              "description": { "type": "string", "nullable": true },
              "materials": {
                "type": "array",
                "items": { "$ref": "#/components/schemas/ProjectMaterial" }
              }
            }
          }
        ]
      },
      "ProjectMaterial": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "project_id": { "type": "string", "format": "uuid" },
          "material_name": { "type": "string" },
          "category": { "type": "string", "nullable": true },
          "quantity": { "type": "number" },
          "unit": { "type": "string" },
          "co2_a1_a3": { "type": "number", "nullable": true },
          "co2_a4": { "type": "number", "nullable": true },
          "co2_a5": { "type": "number", "nullable": true },
          "co2_total": { "type": "number", "nullable": true },
          "created_at": { "type": "string", "format": "date-time" }
        }
      },
      "CustomEpd": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "organization_id": { "type": "string", "format": "uuid" },
          "name": { "type": "string" },
          "manufacturer": { "type": "string", "nullable": true },
          "category": { "type": "string", "nullable": true },
          "unit": { "type": "string" },
          "density": { "type": "number", "nullable": true },
          "co2_a1_a3": { "type": "number", "nullable": true },
          "co2_a4": { "type": "number", "nullable": true },
          "co2_a5": { "type": "number", "nullable": true },
          "created_at": { "type": "string", "format": "date-time" },
          "updated_at": { "type": "string", "format": "date-time" }
        }
      }
    }
  }
}
