{
  "openapi": "3.1.0",
  "info": {
    "title": "AI Newsletter Public API",
    "version": "1.0.0",
    "description": "Public REST API for the AI Newsletter platform. Authenticate with `Authorization: Bearer sk_live_…`."
  },
  "servers": [
    {
      "url": "https://qdqyoolnfejkojdcufkk.functions.supabase.co",
      "description": "Production"
    }
  ],
  "components": {
    "securitySchemes": {
      "apiKey": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "sk_live_...",
        "description": "Live API key issued from the dashboard. Must include the required scope for the endpoint."
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean",
            "enum": [
              false
            ]
          },
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string",
                "enum": [
                  "missing_api_key",
                  "invalid_api_key",
                  "revoked_api_key",
                  "scope_denied",
                  "rate_limited",
                  "invalid_request",
                  "not_found",
                  "forbidden",
                  "conflict",
                  "internal_error",
                  "method_not_allowed"
                ]
              },
              "message": {
                "type": "string"
              }
            },
            "required": [
              "code",
              "message"
            ]
          }
        },
        "required": [
          "success",
          "error"
        ]
      },
      "MeResponse": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean",
            "enum": [
              true
            ]
          },
          "data": {
            "type": "object",
            "properties": {
              "user_id": {
                "type": "string",
                "format": "uuid"
              },
              "plan": {
                "type": "string",
                "enum": [
                  "free",
                  "starter",
                  "pro",
                  "business"
                ]
              },
              "display_name": {
                "type": "string",
                "nullable": true
              },
              "newsletters": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string",
                      "format": "uuid"
                    },
                    "name": {
                      "type": "string"
                    },
                    "slug": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      },
      "Subscriber": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "email": {
            "type": "string",
            "format": "email"
          },
          "name": {
            "type": "string",
            "nullable": true
          },
          "status": {
            "type": "string",
            "enum": [
              "subscribed",
              "unsubscribed",
              "pending"
            ]
          },
          "source": {
            "type": "string"
          },
          "subscribed_at": {
            "type": "string",
            "format": "date-time"
          },
          "unsubscribed_at": {
            "type": "string",
            "format": "date-time",
            "nullable": true
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "SubscriberListResponse": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean",
            "enum": [
              true
            ]
          },
          "data": {
            "type": "object",
            "properties": {
              "items": {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/Subscriber"
                }
              },
              "next_cursor": {
                "type": "string",
                "nullable": true
              }
            }
          }
        }
      },
      "SubscriberResponse": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean",
            "enum": [
              true
            ]
          },
          "data": {
            "$ref": "#/components/schemas/Subscriber"
          }
        }
      },
      "CreateSubscriberRequest": {
        "type": "object",
        "required": [
          "newsletter_id",
          "email"
        ],
        "properties": {
          "newsletter_id": {
            "type": "string",
            "format": "uuid"
          },
          "email": {
            "type": "string",
            "format": "email"
          },
          "name": {
            "type": "string",
            "nullable": true
          }
        }
      }
    }
  },
  "security": [
    {
      "apiKey": [
        "read:me",
        "read:subscribers",
        "write:subscribers"
      ]
    }
  ],
  "paths": {
    "/v1/me": {
      "get": {
        "summary": "Get authenticated account context",
        "description": "Returns the user, current plan, and accessible newsletters.",
        "security": [
          {
            "apiKey": [
              "read:me"
            ]
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MeResponse"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/subscribers": {
      "get": {
        "summary": "List subscribers",
        "security": [
          {
            "apiKey": [
              "read:subscribers"
            ]
          }
        ],
        "parameters": [
          {
            "name": "newsletter_id",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "status",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "subscribed",
                "unsubscribed",
                "pending"
              ]
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 50
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SubscriberListResponse"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Scope denied",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Create a subscriber",
        "description": "Adds a subscriber to a newsletter. Returns the existing record if the email is already subscribed.",
        "security": [
          {
            "apiKey": [
              "write:subscribers"
            ]
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateSubscriberRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Already exists (returned unchanged)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SubscriberResponse"
                }
              }
            }
          },
          "201": {
            "description": "Created",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SubscriberResponse"
                }
              }
            }
          },
          "409": {
            "description": "Email is suppressed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "422": {
            "description": "Validation failed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/subscribers/{id}": {
      "delete": {
        "summary": "Soft-unsubscribe a subscriber",
        "description": "Marks the subscriber as unsubscribed. Never hard-deletes.",
        "security": [
          {
            "apiKey": [
              "write:subscribers"
            ]
          }
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          },
          "404": {
            "description": "Not found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  }
}