{
  "openapi": "3.1.0",
  "info": {
    "title": "JarDrop API",
    "description": "Venue feedback platform API.\n\nJarDrop provides QR code-based surveys with AI-powered sentiment analysis for service venues.\n\n## Architecture\n\nJarDrop uses a **Supabase RPC-first architecture**. All API endpoints are PostgreSQL functions\ncalled via Supabase's REST API. The base URL for RPC calls is:\n\n```\nPOST {SUPABASE_URL}/rest/v1/rpc/{function_name}\n```\n\n## Authentication\n\n- **Public endpoints**: No authentication required (anonymous access)\n- **Authenticated endpoints**: Require a valid JWT token in the `Authorization` header\n\n```\nAuthorization: Bearer {your_jwt_token}\n```\n\nJWT tokens are obtained via Supabase Auth (sign in/sign up flows).\n\n## Rate Limiting\n\n- Public feedback submission: 1 request per 30 seconds per client\n- Authenticated endpoints: Standard Supabase rate limits apply\n\n## Getting Started\n\n### 1. Get your API credentials\n\nYou'll need two things from your Supabase project:\n- **Project URL**: `https://your-project.supabase.co`\n- **Anon Key**: Your public anonymous key (safe to use in client-side code)\n\n### 2. Make your first request\n\n```bash\n# List public templates (no auth required)\ncurl -X POST 'https://dwxmozusznmzykqvffct.supabase.co/rest/v1/rpc/get_public_templates' \\\n  -H 'apikey: YOUR_ANON_KEY' \\\n  -H 'Content-Type: application/json' \\\n  -d '{}'\n```\n\n### 3. Authenticate for protected endpoints\n\n```bash\n# Sign in to get a JWT token\ncurl -X POST 'https://dwxmozusznmzykqvffct.supabase.co/auth/v1/token?grant_type=password' \\\n  -H 'apikey: YOUR_ANON_KEY' \\\n  -H 'Content-Type: application/json' \\\n  -d '{\"email\": \"user@example.com\", \"password\": \"your-password\"}'\n\n# Use the access_token in subsequent requests\ncurl -X POST 'https://dwxmozusznmzykqvffct.supabase.co/rest/v1/rpc/list_company_forms' \\\n  -H 'apikey: YOUR_ANON_KEY' \\\n  -H 'Authorization: Bearer YOUR_ACCESS_TOKEN' \\\n  -H 'Content-Type: application/json' \\\n  -d '{\"p_company_id\": \"your-org-uuid\"}'\n```\n\n## SDK Support\n\nWe recommend using the official Supabase client libraries:\n- **JavaScript**: `@supabase/supabase-js`\n- **Python**: `supabase-py`\n- **Dart/Flutter**: `supabase_flutter`\n\n```typescript\nimport { createClient } from '@supabase/supabase-js'\n\nconst supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)\n\n// Call RPC functions\nconst { data, error } = await supabase.rpc('get_public_templates', {\n  p_category: 'restaurant',\n  p_language: 'en'\n})\n```\n",
    "version": "1.0.0",
    "contact": {
      "name": "JarDrop Support",
      "url": "https://www.jardrop.com",
      "email": "support@jardrop.com"
    },
    "license": {
      "name": "Proprietary",
      "url": "https://www.jardrop.com/terms"
    }
  },
  "servers": [
    {
      "url": "https://dwxmozusznmzykqvffct.supabase.co/rest/v1",
      "description": "Production Supabase API"
    },
    {
      "url": "http://localhost:54321/rest/v1",
      "description": "Local Supabase (development)"
    }
  ],
  "externalDocs": {
    "description": "JarDrop API Documentation Portal",
    "url": "https://api.jardrop.com"
  },
  "tags": [
    {
      "name": "Forms",
      "description": "Form management endpoints for creating, listing, and updating feedback forms.\nForms are the primary way restaurants collect guest feedback via QR codes.\n"
    },
    {
      "name": "Feedback",
      "description": "Feedback submission and management. Includes public submission (anonymous guests)\nand authenticated management (restaurant owners).\n"
    },
    {
      "name": "Templates",
      "description": "Pre-built survey templates in 6 languages (EN, SV, DE, FR, IT, NL).\nTemplates provide ready-to-use question sets for different use cases.\n"
    },
    {
      "name": "Surveys",
      "description": "Multi-location survey management. Surveys can be assigned to multiple locations,\neach with a unique QR code token.\n"
    },
    {
      "name": "Validation",
      "description": "Input validation and configuration endpoints for organization setup and customization.\n"
    },
    {
      "name": "Customization",
      "description": "Organization customization and theming endpoints.\n"
    },
    {
      "name": "Webhooks",
      "description": "Edge function webhooks triggered by database events.\nThese are internal endpoints but documented for transparency.\n\n**Available Webhooks**:\n- `send-feedback-notification` - Triggered on new feedback\n- `send-welcome-email` - Triggered on new user signup\n- `generate-weekly-digest` - Scheduled every Sunday 9am UTC\n- `notify-admin-new-signup` - Admin notification on signups\n"
    }
  ],
  "paths": {
    "/rpc/get_form_by_identifier": {
      "post": {
        "tags": [
          "Forms"
        ],
        "summary": "Get form by identifier",
        "description": "Retrieves a form and its associated organization details by the form's public identifier.\nUsed when rendering the public feedback page for QR code scans.\n\n**Access**: Public (anonymous)\n",
        "operationId": "getFormByIdentifier",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "form_identifier"
                ],
                "properties": {
                  "form_identifier": {
                    "type": "string",
                    "description": "The 8-12 character alphanumeric form identifier",
                    "example": "abc123xyz",
                    "minLength": 8,
                    "maxLength": 12
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Form found successfully",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string",
                      "format": "uuid"
                    },
                    "identifier": {
                      "type": "string"
                    },
                    "name": {
                      "type": "string"
                    },
                    "welcome_message": {
                      "type": "string",
                      "nullable": true
                    },
                    "is_active": {
                      "type": "boolean"
                    },
                    "organization": {
                      "type": "object",
                      "properties": {
                        "id": {
                          "type": "string",
                          "format": "uuid"
                        },
                        "name": {
                          "type": "string"
                        },
                        "logo_url": {
                          "type": "string",
                          "nullable": true
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Form not found or inactive"
          }
        }
      }
    },
    "/rpc/create_form": {
      "post": {
        "tags": [
          "Forms"
        ],
        "summary": "Create a new form",
        "description": "Creates a new feedback form for an organization. Automatically generates\na unique 8-12 character alphanumeric identifier for QR codes.\n\n**Access**: Authenticated (organization owner/member)\n",
        "operationId": "createForm",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "p_company_id",
                  "p_name"
                ],
                "properties": {
                  "p_company_id": {
                    "type": "string",
                    "format": "uuid",
                    "description": "Organization UUID"
                  },
                  "p_name": {
                    "type": "string",
                    "description": "Form name (internal use)",
                    "maxLength": 255,
                    "example": "Main Dining Feedback"
                  },
                  "p_welcome_message": {
                    "type": "string",
                    "description": "Optional welcome message shown to guests",
                    "maxLength": 500,
                    "nullable": true,
                    "example": "We'd love to hear about your experience!"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Form created successfully",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean",
                      "example": true
                    },
                    "form_id": {
                      "type": "string",
                      "format": "uuid"
                    },
                    "identifier": {
                      "type": "string",
                      "description": "Generated form identifier for QR codes",
                      "example": "abc123xyz"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized - invalid or missing JWT token"
          },
          "403": {
            "description": "Forbidden - user does not have access to this organization"
          }
        }
      }
    },
    "/rpc/list_company_forms": {
      "post": {
        "tags": [
          "Forms"
        ],
        "summary": "List organization forms",
        "description": "Lists all forms for an organization with feedback counts.\n\n**Access**: Authenticated (organization owner/member)\n",
        "operationId": "listCompanyForms",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "p_company_id"
                ],
                "properties": {
                  "p_company_id": {
                    "type": "string",
                    "format": "uuid",
                    "description": "Organization UUID"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Forms listed successfully",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "id": {
                        "type": "string",
                        "format": "uuid"
                      },
                      "identifier": {
                        "type": "string"
                      },
                      "name": {
                        "type": "string"
                      },
                      "welcome_message": {
                        "type": "string",
                        "nullable": true
                      },
                      "is_active": {
                        "type": "boolean"
                      },
                      "feedback_count": {
                        "type": "integer"
                      },
                      "created_at": {
                        "type": "string",
                        "format": "date-time"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/rpc/update_form": {
      "post": {
        "tags": [
          "Forms"
        ],
        "summary": "Update a form",
        "description": "Updates form details including name, welcome message, and active status.\n\n**Access**: Authenticated (organization owner/member)\n",
        "operationId": "updateForm",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "p_form_id"
                ],
                "properties": {
                  "p_form_id": {
                    "type": "string",
                    "format": "uuid",
                    "description": "Form UUID to update"
                  },
                  "p_name": {
                    "type": "string",
                    "maxLength": 255,
                    "nullable": true
                  },
                  "p_welcome_message": {
                    "type": "string",
                    "maxLength": 500,
                    "nullable": true
                  },
                  "p_is_active": {
                    "type": "boolean",
                    "nullable": true
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Form updated successfully",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean"
                    },
                    "form": {
                      "type": "object"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/rpc/public_submit_feedback": {
      "post": {
        "tags": [
          "Feedback"
        ],
        "summary": "Submit anonymous feedback",
        "description": "Submits feedback from a guest. Supports both anonymous and contact modes.\n\n**Rate Limited**: 1 request per 30 seconds per client IP.\n\n**Validation**:\n- Feedback body: 5-5000 characters\n- No URLs allowed (spam prevention)\n- XSS content is sanitized server-side\n\n**Access**: Public (anonymous)\n",
        "operationId": "publicSubmitFeedback",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "form_identifier",
                  "feedback_body"
                ],
                "properties": {
                  "form_identifier": {
                    "type": "string",
                    "description": "Form identifier from QR code",
                    "example": "abc123xyz"
                  },
                  "feedback_body": {
                    "type": "string",
                    "description": "Guest feedback text",
                    "minLength": 5,
                    "maxLength": 5000,
                    "example": "The pasta was amazing! Great service too."
                  },
                  "feedback_mode": {
                    "type": "string",
                    "enum": [
                      "anonymous",
                      "with_contact"
                    ],
                    "default": "anonymous",
                    "description": "Feedback submission mode"
                  },
                  "first_name": {
                    "type": "string",
                    "maxLength": 100,
                    "nullable": true,
                    "description": "Required if feedback_mode is 'with_contact'"
                  },
                  "last_name": {
                    "type": "string",
                    "maxLength": 100,
                    "nullable": true
                  },
                  "email": {
                    "type": "string",
                    "format": "email",
                    "nullable": true,
                    "description": "Required if feedback_mode is 'with_contact'"
                  },
                  "client_ip": {
                    "type": "string",
                    "format": "ipv4",
                    "nullable": true,
                    "description": "Client IP for rate limiting (auto-detected if not provided)"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Feedback submitted successfully",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean",
                      "example": true
                    },
                    "feedback_id": {
                      "type": "string",
                      "format": "uuid"
                    }
                  }
                },
                "example": {
                  "success": true,
                  "feedback_id": "123e4567-e89b-12d3-a456-426614174000"
                }
              }
            }
          },
          "400": {
            "description": "Validation error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "success": false,
                  "error": "Feedback must be between 5 and 5000 characters"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "success": false,
                  "error": "Please wait 30 seconds between submissions"
                }
              }
            }
          }
        }
      }
    },
    "/rpc/delete_feedback": {
      "post": {
        "tags": [
          "Feedback"
        ],
        "summary": "Delete feedback",
        "description": "Deletes a single feedback item. Requires owner/member/admin authorization.\n\n**Access**: Authenticated (organization owner/member)\n",
        "operationId": "deleteFeedback",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "feedback_id"
                ],
                "properties": {
                  "feedback_id": {
                    "type": "string",
                    "format": "uuid"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Feedback deleted successfully",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean"
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "Not authorized to delete this feedback"
          }
        }
      }
    },
    "/rpc/bulk_delete_feedback": {
      "post": {
        "tags": [
          "Feedback"
        ],
        "summary": "Bulk delete feedback",
        "description": "Deletes multiple feedback items at once. Authorization is checked per item.\n\n**Access**: Authenticated (organization owner/member)\n",
        "operationId": "bulkDeleteFeedback",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "feedback_ids"
                ],
                "properties": {
                  "feedback_ids": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "format": "uuid"
                    },
                    "maxItems": 100
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Bulk delete completed",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean"
                    },
                    "deleted_count": {
                      "type": "integer"
                    },
                    "failed_count": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/rpc/get_public_templates": {
      "post": {
        "tags": [
          "Templates"
        ],
        "summary": "List public templates",
        "description": "Lists available survey templates, optionally filtered by category and language.\nTemplates are ordered by featured status and popularity.\n\n**Supported Languages**: EN, SV, DE, FR, IT, NL\n\n**Categories**: restaurant, cowork\n\n**Access**: Public (anonymous)\n",
        "operationId": "getPublicTemplates",
        "security": [],
        "requestBody": {
          "required": false,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "p_category": {
                    "type": "string",
                    "enum": [
                      "restaurant",
                      "cowork"
                    ],
                    "nullable": true,
                    "description": "Filter by template category"
                  },
                  "p_language": {
                    "type": "string",
                    "enum": [
                      "en",
                      "sv",
                      "de",
                      "fr",
                      "it",
                      "nl"
                    ],
                    "nullable": true,
                    "description": "Filter by template language"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Templates listed successfully",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Template"
                  }
                }
              }
            }
          }
        }
      }
    },
    "/rpc/get_template_by_slug": {
      "post": {
        "tags": [
          "Templates"
        ],
        "summary": "Get template by slug",
        "description": "Retrieves a single template by its URL slug for detail pages.\n\n**Access**: Public (anonymous)\n",
        "operationId": "getTemplateBySlug",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "p_slug"
                ],
                "properties": {
                  "p_slug": {
                    "type": "string",
                    "example": "restaurant-feedback-en"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Template found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Template"
                }
              }
            }
          },
          "404": {
            "description": "Template not found"
          }
        }
      }
    },
    "/rpc/create_survey_from_template": {
      "post": {
        "tags": [
          "Templates",
          "Surveys"
        ],
        "summary": "Create survey from template",
        "description": "Creates a new survey based on a template. Duplicates all template questions\nwith KPI metadata preserved.\n\n**Access**: Authenticated (organization owner/member)\n",
        "operationId": "createSurveyFromTemplate",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "p_location_id",
                  "p_template_id"
                ],
                "properties": {
                  "p_location_id": {
                    "type": "string",
                    "format": "uuid",
                    "description": "Location to assign the survey to"
                  },
                  "p_template_id": {
                    "type": "string",
                    "format": "uuid",
                    "description": "Template to create survey from"
                  },
                  "p_custom_name": {
                    "type": "string",
                    "maxLength": 255,
                    "nullable": true,
                    "description": "Custom survey name (defaults to template name)"
                  },
                  "p_language": {
                    "type": "string",
                    "enum": [
                      "en",
                      "sv",
                      "de",
                      "fr",
                      "it",
                      "nl"
                    ],
                    "nullable": true,
                    "description": "Survey language (defaults to template language)"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Survey created successfully",
            "content": {
              "application/json": {
                "schema": {
                  "type": "string",
                  "format": "uuid",
                  "description": "New survey ID"
                }
              }
            }
          }
        }
      }
    },
    "/rpc/get_survey_locations": {
      "post": {
        "tags": [
          "Surveys"
        ],
        "summary": "Get survey locations",
        "description": "Returns all active locations assigned to a survey with their unique QR tokens.\n\n**Access**: Authenticated (organization owner/member)\n",
        "operationId": "getSurveyLocations",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "p_survey_id"
                ],
                "properties": {
                  "p_survey_id": {
                    "type": "string",
                    "format": "uuid"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Locations retrieved successfully",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "location_id": {
                        "type": "string",
                        "format": "uuid"
                      },
                      "location_name": {
                        "type": "string"
                      },
                      "token": {
                        "type": "string",
                        "description": "Unique QR code token for this location"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/rpc/update_survey_locations": {
      "post": {
        "tags": [
          "Surveys"
        ],
        "summary": "Update survey locations",
        "description": "Replaces the survey-location assignments. Each location gets a unique QR token.\n\n**Access**: Authenticated (organization owner/member)\n",
        "operationId": "updateSurveyLocations",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "p_survey_id",
                  "p_location_ids"
                ],
                "properties": {
                  "p_survey_id": {
                    "type": "string",
                    "format": "uuid"
                  },
                  "p_location_ids": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "format": "uuid"
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Locations updated successfully",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/rpc/get_survey_by_location_token": {
      "post": {
        "tags": [
          "Surveys"
        ],
        "summary": "Get survey by location token",
        "description": "Looks up survey and location details by the unique QR code token.\nUsed when a guest scans a QR code at a specific location.\n\n**Access**: Public (anonymous)\n",
        "operationId": "getSurveyByLocationToken",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "p_token"
                ],
                "properties": {
                  "p_token": {
                    "type": "string",
                    "description": "12-character location-specific token",
                    "example": "abc123def456"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Survey found",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "survey_id": {
                      "type": "string",
                      "format": "uuid"
                    },
                    "survey_name": {
                      "type": "string"
                    },
                    "location_id": {
                      "type": "string",
                      "format": "uuid"
                    },
                    "location_name": {
                      "type": "string"
                    },
                    "organization": {
                      "type": "object",
                      "properties": {
                        "id": {
                          "type": "string",
                          "format": "uuid"
                        },
                        "name": {
                          "type": "string"
                        },
                        "logo_url": {
                          "type": "string",
                          "nullable": true
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Invalid or expired token"
          }
        }
      }
    },
    "/rpc/validate_company_input": {
      "post": {
        "tags": [
          "Validation"
        ],
        "summary": "Validate organization input",
        "description": "Validates organization name and slug format before creation.\n\n**Validation Rules**:\n- Name: 2-100 characters\n- Slug: 3-50 characters, lowercase alphanumeric with hyphens, unique\n\n**Access**: Public (anonymous)\n",
        "operationId": "validateCompanyInput",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "organization_name",
                  "slug"
                ],
                "properties": {
                  "organization_name": {
                    "type": "string",
                    "minLength": 2,
                    "maxLength": 100
                  },
                  "slug": {
                    "type": "string",
                    "minLength": 3,
                    "maxLength": 50,
                    "pattern": "^[a-z0-9-]+$"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Validation result",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "valid": {
                      "type": "boolean"
                    },
                    "errors": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/rpc/validate_customization_settings": {
      "post": {
        "tags": [
          "Validation"
        ],
        "summary": "Validate customization settings",
        "description": "Validates theme settings before saving.\n\n**Validation Rules**:\n- theme_name: Must be valid theme identifier\n- custom_css: Max 10KB\n- colors: Must be valid hex color codes\n\n**Access**: Authenticated\n",
        "operationId": "validateCustomizationSettings",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "settings"
                ],
                "properties": {
                  "settings": {
                    "type": "object",
                    "properties": {
                      "theme_name": {
                        "type": "string"
                      },
                      "primary_color": {
                        "type": "string",
                        "pattern": "^#[0-9A-Fa-f]{6}$"
                      },
                      "custom_css": {
                        "type": "string",
                        "maxLength": 10240
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Validation result",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "valid": {
                      "type": "boolean"
                    },
                    "errors": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/rpc/can_activate_survey": {
      "post": {
        "tags": [
          "Surveys",
          "Validation"
        ],
        "summary": "Check if survey can be activated",
        "description": "Checks if a survey has questions and can be activated for public use.\n\n**Access**: Authenticated\n",
        "operationId": "canActivateSurvey",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "p_survey_id"
                ],
                "properties": {
                  "p_survey_id": {
                    "type": "string",
                    "format": "uuid"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Activation check result",
            "content": {
              "application/json": {
                "schema": {
                  "type": "boolean"
                }
              }
            }
          }
        }
      }
    },
    "/rpc/update_company_customization": {
      "post": {
        "tags": [
          "Customization"
        ],
        "summary": "Update organization customization",
        "description": "Updates the customization settings for an organization including theme,\ncolors, and custom CSS.\n\n**Access**: Authenticated (organization owner)\n",
        "operationId": "updateCompanyCustomization",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "p_company_id",
                  "settings"
                ],
                "properties": {
                  "p_company_id": {
                    "type": "string",
                    "format": "uuid",
                    "description": "Organization UUID"
                  },
                  "settings": {
                    "type": "object",
                    "description": "Customization settings object",
                    "properties": {
                      "theme_name": {
                        "type": "string",
                        "description": "Theme identifier",
                        "example": "modern"
                      },
                      "primary_color": {
                        "type": "string",
                        "pattern": "^#[0-9A-Fa-f]{6}$",
                        "example": "#667eea"
                      },
                      "secondary_color": {
                        "type": "string",
                        "pattern": "^#[0-9A-Fa-f]{6}$",
                        "example": "#764ba2"
                      },
                      "custom_css": {
                        "type": "string",
                        "maxLength": 10240,
                        "description": "Custom CSS (max 10KB)"
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Customization updated successfully",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Invalid settings"
          },
          "403": {
            "description": "Not authorized to update this organization"
          }
        }
      }
    },
    "/functions/v1/send-feedback-notification": {
      "post": {
        "tags": [
          "Webhooks"
        ],
        "summary": "Send feedback notification (Internal)",
        "description": "Triggered automatically when new feedback is submitted.\nSends an email notification to the organization owner.\n\n**Trigger**: Database INSERT on `feedback` table\n**Internal**: Not intended for direct API calls\n",
        "operationId": "sendFeedbackNotification",
        "security": [],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "record": {
                    "type": "object",
                    "properties": {
                      "id": {
                        "type": "string",
                        "format": "uuid"
                      },
                      "body": {
                        "type": "string"
                      },
                      "company_id": {
                        "type": "string",
                        "format": "uuid"
                      },
                      "created_at": {
                        "type": "string",
                        "format": "date-time"
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Notification sent",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "message": {
                      "type": "string"
                    },
                    "feedbackId": {
                      "type": "string"
                    },
                    "companyName": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Internal error"
          }
        }
      }
    },
    "/functions/v1/generate-weekly-digest": {
      "post": {
        "tags": [
          "Webhooks"
        ],
        "summary": "Generate weekly digest (Scheduled)",
        "description": "Scheduled job that generates weekly analytics digests and sends email summaries.\n\n**Schedule**: Every Sunday at 9:00 AM UTC\n**Internal**: Triggered by pg_cron via pg_net, not for direct calls\n\n**Digest Contents**:\n- Score normalization and submission health\n- Response volume and trends\n- Top themes and concerns\n- Per-location breakdowns\n- Stored in weekly_digests table for in-app viewing\n",
        "operationId": "generateWeeklyDigest",
        "security": [],
        "responses": {
          "200": {
            "description": "Digest generated and emails sent",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean"
                    },
                    "digestsGenerated": {
                      "type": "integer"
                    },
                    "emailsSent": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "description": "Supabase JWT token obtained via authentication.\n\nAlso requires the `apikey` header with your Supabase anon key.\n"
      },
      "apiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "apikey",
        "description": "Supabase project anon key"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean",
            "example": false
          },
          "error": {
            "type": "string",
            "example": "An error occurred"
          }
        }
      },
      "Template": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "slug": {
            "type": "string",
            "example": "restaurant-feedback-en"
          },
          "name": {
            "type": "string",
            "example": "Restaurant Feedback"
          },
          "description": {
            "type": "string"
          },
          "category": {
            "type": "string",
            "enum": [
              "restaurant",
              "cowork"
            ]
          },
          "language": {
            "type": "string",
            "enum": [
              "en",
              "sv",
              "de",
              "fr",
              "it",
              "nl"
            ]
          },
          "is_featured": {
            "type": "boolean"
          },
          "popularity_score": {
            "type": "integer"
          },
          "questions": {
            "type": "array",
            "description": "Pre-defined survey questions",
            "items": {
              "$ref": "#/components/schemas/TemplateQuestion"
            }
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "TemplateQuestion": {
        "type": "object",
        "properties": {
          "question_text": {
            "type": "string"
          },
          "question_type": {
            "type": "string",
            "enum": [
              "rating",
              "text",
              "multiple_choice",
              "nps"
            ]
          },
          "is_required": {
            "type": "boolean"
          },
          "options": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "nullable": true
          },
          "kpi": {
            "type": "boolean",
            "description": "Whether this question tracks a KPI"
          },
          "kpi_key": {
            "type": "string",
            "enum": [
              "overall_satisfaction",
              "nps",
              "value_for_money"
            ],
            "nullable": true
          },
          "help_text": {
            "type": "string",
            "nullable": true
          }
        }
      },
      "Survey": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "organization_id": {
            "type": "string",
            "format": "uuid"
          },
          "is_active": {
            "type": "boolean"
          },
          "token": {
            "type": "string",
            "description": "Public survey token"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "Feedback": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "form_id": {
            "type": "string",
            "format": "uuid"
          },
          "body": {
            "type": "string"
          },
          "sentiment": {
            "type": "string",
            "enum": [
              "positive",
              "neutral",
              "negative"
            ],
            "nullable": true
          },
          "emoji_reaction": {
            "type": "string",
            "nullable": true
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "Organization": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "slug": {
            "type": "string"
          },
          "logo_url": {
            "type": "string",
            "nullable": true
          },
          "preferred_language": {
            "type": "string",
            "enum": [
              "en",
              "sv",
              "de",
              "fr",
              "it",
              "nl"
            ]
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      }
    }
  }
}