openapi: 3.0.3
info:
  title: OptareID API
  version: 1.0.0
  description: |2-

        # OptareID API
        
        Enterprise-grade identity and access management API.
        
        ## Authentication
        
        All API endpoints require authentication using a Bearer token. You can obtain a token by:
        1. Using OAuth 2.0 authorization code flow
        2. Using API keys for server-to-server communication
        
        Include the token in the `Authorization` header:
        ```
        Authorization: Bearer YOUR_TOKEN
        ```
        
        ## Rate Limiting
        
        API requests are rate limited to 100 requests per minute per API key.
        Rate limit headers are included in all responses:
        - `X-RateLimit-Limit`: Maximum requests per window
        - `X-RateLimit-Remaining`: Remaining requests
        - `X-RateLimit-Reset`: Unix timestamp when limit resets
            
  contact:
    name: OptareID Support
    email: support@optare.one
    url: https://docs.optare.one
  license:
    name: MIT
    url: https://opensource.org/licenses/MIT
servers:
  - url: https://id.optare.one
    description: Production
  - url: http://localhost:3000
    description: Development
tags:
  - name: Users
    description: User management endpoints
  - name: Organizations
    description: Organization management
  - name: OAuth Clients
    description: OAuth 2.0 client management
  - name: Products
    description: Product catalog
  - name: Subscriptions
    description: Subscription management
  - name: Licenses
    description: License and entitlement management
  - name: Webhooks
    description: Webhook endpoint management
  - name: API Keys
    description: API key management
  - name: Billing
    description: Billing and payment management
security:
  - bearerAuth: []
components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: string
          description: Unique user identifier
        name:
          type: string
        email:
          type: string
          format: email
          description: User email address
        emailVerified:
          type: boolean
          nullable: true
        image:
          type: string
          nullable: true
        blocked:
          type: boolean
        authProviderUserId:
          type: string
          nullable: true
        locale:
          type: string
          nullable: true
          maxLength: 10
        scimExternalId:
          type: string
          nullable: true
        metadata:
          anyOf:
            - type: string
            - type: number
            - type: boolean
            - nullable: true
            - type: object
              additionalProperties:
                nullable: true
            - type: array
              items:
                nullable: true
            - nullable: true
        sessionVersion:
          type: integer
          minimum: -2147483648
          maximum: 2147483647
        twoFactorEnabled:
          type: boolean
          nullable: true
        twoFactorSecret:
          type: string
          nullable: true
        twoFactorBackupCodes:
          type: string
          nullable: true
        isPlatformAdmin:
          type: boolean
        onboardingState:
          anyOf:
            - type: string
            - type: number
            - type: boolean
            - nullable: true
            - type: object
              additionalProperties:
                nullable: true
            - type: array
              items:
                nullable: true
            - nullable: true
        createdAt:
          type: string
          format: date-time
          description: Account creation timestamp
        updatedAt:
          type: string
          nullable: true
        lastLoginAt:
          type: string
          nullable: true
        failedLoginAttempts:
          type: integer
          minimum: -2147483648
          maximum: 2147483647
        lockedUntil:
          type: string
          nullable: true
        deletedAt:
          type: string
          nullable: true
        deletedBy:
          type: string
          nullable: true
      required:
        - id
        - name
        - email
        - emailVerified
        - image
        - blocked
        - authProviderUserId
        - locale
        - scimExternalId
        - metadata
        - sessionVersion
        - twoFactorEnabled
        - twoFactorSecret
        - twoFactorBackupCodes
        - isPlatformAdmin
        - onboardingState
        - createdAt
        - updatedAt
        - lastLoginAt
        - failedLoginAttempts
        - lockedUntil
        - deletedAt
        - deletedBy
    CreateUser:
      type: object
      properties:
        name:
          type: string
          minLength: 1
          maxLength: 100
        email:
          type: string
          format: email
        password:
          type: string
          minLength: 8
          maxLength: 100
          pattern: '[A-Z]'
          description: User password (optional for SSO)
      required:
        - name
        - email
    UpdateUser:
      type: object
      properties:
        name:
          type: string
          minLength: 1
          description: User display name
        image:
          anyOf:
            - type: string
              format: uri
            - type: string
              enum:
                - ''
          description: Avatar URL
    Organization:
      type: object
      properties:
        id:
          type: string
          description: Unique organization identifier
        name:
          type: string
        slug:
          type: string
          description: URL-friendly identifier
        domain:
          type: string
          nullable: true
          maxLength: 255
        logoUrl:
          type: string
          nullable: true
        samlEnabled:
          type: boolean
          nullable: true
        samlEntityId:
          type: string
          nullable: true
        samlSsoUrl:
          type: string
          nullable: true
        samlCertificate:
          type: string
          nullable: true
        scimEnabled:
          type: boolean
          nullable: true
        scimToken:
          type: string
          nullable: true
        scimBaseUrl:
          type: string
          nullable: true
        plan:
          type: string
          nullable: true
          maxLength: 50
        maxMembers:
          type: string
          nullable: true
        domains:
          anyOf:
            - type: string
            - type: number
            - type: boolean
            - nullable: true
            - type: object
              additionalProperties:
                nullable: true
            - type: array
              items:
                nullable: true
            - nullable: true
        metadata:
          anyOf:
            - type: string
            - type: number
            - type: boolean
            - nullable: true
            - type: object
              additionalProperties:
                nullable: true
            - type: array
              items:
                nullable: true
            - nullable: true
        type:
          type: string
          nullable: true
          maxLength: 20
        parentTenantId:
          type: string
          nullable: true
        rateLimit:
          type: integer
          nullable: true
          minimum: -2147483648
          maximum: 2147483647
        createdAt:
          type: string
          nullable: true
        updatedAt:
          type: string
          nullable: true
      required:
        - id
        - name
        - slug
        - domain
        - logoUrl
        - samlEnabled
        - samlEntityId
        - samlSsoUrl
        - samlCertificate
        - scimEnabled
        - scimToken
        - scimBaseUrl
        - plan
        - maxMembers
        - domains
        - metadata
        - type
        - parentTenantId
        - rateLimit
        - createdAt
        - updatedAt
    CreateOrganization:
      type: object
      properties:
        name:
          type: string
          minLength: 1
          maxLength: 100
        slug:
          type: string
          minLength: 1
          maxLength: 100
          pattern: ^[a-z0-9-]+$
      required:
        - name
        - slug
    UpdateOrganization:
      type: object
      properties:
        name:
          type: string
          description: Organization name
        slug:
          type: string
          description: URL-friendly identifier
        logo:
          type: string
          nullable: true
          description: Logo URL
    OAuthClient:
      type: object
      properties:
        id:
          type: string
          description: Internal ID
        organizationId:
          type: string
        clientId:
          type: string
          description: OAuth client ID
        clientSecret:
          type: string
        name:
          type: string
        description:
          type: string
          nullable: true
        redirectUris:
          anyOf:
            - type: string
            - type: number
            - type: boolean
            - nullable: true
            - type: object
              additionalProperties:
                nullable: true
            - type: array
              items:
                nullable: true
            - nullable: true
        allowedOrigins:
          anyOf:
            - type: string
            - type: number
            - type: boolean
            - nullable: true
            - type: object
              additionalProperties:
                nullable: true
            - type: array
              items:
                nullable: true
            - nullable: true
        allowedScopes:
          anyOf:
            - type: string
            - type: number
            - type: boolean
            - nullable: true
            - type: object
              additionalProperties:
                nullable: true
            - type: array
              items:
                nullable: true
            - nullable: true
        allowedSocialProviders:
          anyOf:
            - type: string
            - type: number
            - type: boolean
            - nullable: true
            - type: object
              additionalProperties:
                nullable: true
            - type: array
              items:
                nullable: true
            - nullable: true
        isActive:
          type: boolean
          nullable: true
        pkceRequired:
          type: boolean
          nullable: true
        createdBy:
          type: string
          nullable: true
        metadata:
          anyOf:
            - type: string
            - type: number
            - type: boolean
            - nullable: true
            - type: object
              additionalProperties:
                nullable: true
            - type: array
              items:
                nullable: true
            - nullable: true
        createdAt:
          type: string
          nullable: true
        updatedAt:
          type: string
          nullable: true
      required:
        - id
        - organizationId
        - clientId
        - clientSecret
        - name
        - description
        - redirectUris
        - allowedOrigins
        - allowedScopes
        - allowedSocialProviders
        - isActive
        - pkceRequired
        - createdBy
        - metadata
        - createdAt
        - updatedAt
    CreateOAuthClient:
      type: object
      properties:
        name:
          type: string
          minLength: 1
          maxLength: 100
        description:
          type: string
          nullable: true
        redirectUris:
          anyOf:
            - type: string
            - type: number
            - type: boolean
            - nullable: true
            - type: object
              additionalProperties:
                nullable: true
            - type: array
              items:
                nullable: true
            - nullable: true
        allowedScopes:
          anyOf:
            - type: string
            - type: number
            - type: boolean
            - nullable: true
            - type: object
              additionalProperties:
                nullable: true
            - type: array
              items:
                nullable: true
            - nullable: true
      required:
        - name
        - redirectUris
    Product:
      type: object
      properties:
        id:
          type: string
          description: Product ID
        organizationId:
          type: string
          nullable: true
        oauthClientId:
          type: string
          nullable: true
        name:
          type: string
        slug:
          type: string
          maxLength: 100
        description:
          type: string
          nullable: true
        features:
          anyOf:
            - type: string
            - type: number
            - type: boolean
            - nullable: true
            - type: object
              additionalProperties:
                nullable: true
            - type: array
              items:
                nullable: true
            - nullable: true
        productType:
          type: string
          enum:
            - payment
            - tenancy
            - hybrid
        price:
          type: integer
          nullable: true
          minimum: -2147483648
          maximum: 2147483647
        currency:
          type: string
          nullable: true
          maxLength: 3
        stripePriceId:
          type: string
          nullable: true
        stripeProductId:
          type: string
          nullable: true
        paypalPlanId:
          type: string
          nullable: true
        paypalProductId:
          type: string
          nullable: true
        razorpayPlanId:
          type: string
          nullable: true
        metadata:
          anyOf:
            - type: string
            - type: number
            - type: boolean
            - nullable: true
            - type: object
              additionalProperties:
                nullable: true
            - type: array
              items:
                nullable: true
            - nullable: true
        createdAt:
          type: string
          nullable: true
        updatedAt:
          type: string
          nullable: true
      required:
        - id
        - organizationId
        - oauthClientId
        - name
        - slug
        - description
        - features
        - productType
        - price
        - currency
        - stripePriceId
        - stripeProductId
        - paypalPlanId
        - paypalProductId
        - razorpayPlanId
        - metadata
        - createdAt
        - updatedAt
    UpdateProduct:
      type: object
      properties:
        name:
          type: string
          description: Product name
        slug:
          type: string
          description: URL-friendly identifier
        description:
          type: string
          nullable: true
          description: Product description
        features:
          type: array
          items:
            type: string
          description: Feature list
        status:
          type: string
          enum:
            - active
            - inactive
          description: Product status
    Subscription:
      type: object
      properties:
        id:
          type: string
          description: Subscription ID
        organizationId:
          type: string
        productId:
          type: string
        totalSeats:
          type: integer
          minimum: -2147483648
          maximum: 2147483647
        seatsUsed:
          type: integer
          minimum: -2147483648
          maximum: 2147483647
        status:
          type: string
          enum:
            - active
            - cancelled
            - expired
            - trial
        startDate:
          type: string
        endDate:
          type: string
          nullable: true
        pricePerSeat:
          type: string
          nullable: true
        billingCycle:
          type: string
          nullable: true
          maxLength: 20
        providerType:
          type: string
          nullable: true
        providerSubscriptionId:
          type: string
          nullable: true
        providerPlanId:
          type: string
          nullable: true
        paymentStatus:
          type: string
          nullable: true
        nextBillingDate:
          type: string
          nullable: true
        lastPaymentDate:
          type: string
          nullable: true
        paypalSubscriptionId:
          type: string
          nullable: true
        paypalPlanId:
          type: string
          nullable: true
        metadata:
          anyOf:
            - type: string
            - type: number
            - type: boolean
            - nullable: true
            - type: object
              additionalProperties:
                nullable: true
            - type: array
              items:
                nullable: true
            - nullable: true
        createdAt:
          type: string
          nullable: true
        updatedAt:
          type: string
          nullable: true
      required:
        - id
        - organizationId
        - productId
        - totalSeats
        - seatsUsed
        - status
        - startDate
        - endDate
        - pricePerSeat
        - billingCycle
        - providerType
        - providerSubscriptionId
        - providerPlanId
        - paymentStatus
        - nextBillingDate
        - lastPaymentDate
        - paypalSubscriptionId
        - paypalPlanId
        - metadata
        - createdAt
        - updatedAt
    CreateSubscription:
      type: object
      properties:
        organization_id:
          type: string
          description: Organization ID
        product_id:
          type: string
          description: Product ID to subscribe to
        total_seats:
          type: number
          minimum: 1
          description: 'Number of seats (default: 1)'
        price_per_seat:
          type: number
          minimum: 0
          description: Price per seat
        billing_cycle:
          type: string
          enum:
            - monthly
            - yearly
          description: Billing cycle
        status:
          type: string
          enum:
            - active
            - trial
            - cancelled
            - expired
          description: Subscription status
        end_date:
          type: string
          format: date-time
          description: End date (ISO 8601)
      required:
        - organization_id
        - product_id
    License:
      type: object
      properties:
        id:
          type: string
          description: License ID
        userId:
          type: string
        organizationId:
          type: string
        productId:
          type: string
        subscriptionId:
          type: string
        status:
          type: string
          enum:
            - active
            - revoked
            - expired
        assignedBy:
          type: string
          nullable: true
        assignedAt:
          type: string
          nullable: true
        revokedAt:
          type: string
          nullable: true
        revokedBy:
          type: string
          nullable: true
        metadata:
          anyOf:
            - type: string
            - type: number
            - type: boolean
            - nullable: true
            - type: object
              additionalProperties:
                nullable: true
            - type: array
              items:
                nullable: true
            - nullable: true
        createdAt:
          type: string
          nullable: true
        updatedAt:
          type: string
          nullable: true
      required:
        - id
        - userId
        - organizationId
        - productId
        - subscriptionId
        - status
        - assignedBy
        - assignedAt
        - revokedAt
        - revokedBy
        - metadata
        - createdAt
        - updatedAt
    LicenseCheck:
      type: object
      properties:
        isEnabled:
          type: boolean
          description: Whether the feature is enabled
        limit:
          type: number
          nullable: true
          description: Usage limit (null = unlimited)
        usage:
          type: number
          description: Current usage count
      required:
        - isEnabled
        - limit
        - usage
    Entitlement:
      type: object
      properties:
        feature:
          type: string
          description: Feature name
        isEnabled:
          type: boolean
          description: Whether feature is enabled
        limit:
          type: number
          nullable: true
          description: Usage limit (null = unlimited)
        usage:
          type: number
          description: Current usage count
      required:
        - feature
        - isEnabled
        - limit
        - usage
    Webhook:
      type: object
      properties:
        id:
          type: string
          description: Webhook ID
        name:
          type: string
          description: Webhook name
        description:
          type: string
          nullable: true
          description: Description
        url:
          type: string
          format: uri
          description: Webhook endpoint URL
        events:
          type: array
          items:
            type: string
          description: Events to trigger webhook
        isActive:
          type: boolean
          description: Whether webhook is active
        createdAt:
          type: string
          format: date-time
          description: Creation timestamp
        updatedAt:
          type: string
          nullable: true
          format: date-time
          description: Last update timestamp
      required:
        - id
        - name
        - url
        - events
        - isActive
        - createdAt
    CreateWebhook:
      type: object
      properties:
        name:
          type: string
          description: Webhook name
        url:
          type: string
          format: uri
          description: Webhook endpoint URL
        events:
          type: array
          items:
            type: string
          description: Events to trigger webhook
        description:
          type: string
          description: Description
      required:
        - name
        - url
    UpdateWebhook:
      type: object
      properties:
        name:
          type: string
          description: Webhook name
        url:
          type: string
          format: uri
          description: Webhook endpoint URL
        events:
          type: array
          items:
            type: string
          description: Events to trigger webhook
        description:
          type: string
          nullable: true
          description: Description
        isActive:
          type: boolean
          description: Whether webhook is active
    ApiKey:
      type: object
      properties:
        id:
          type: string
          description: API key ID
        name:
          type: string
          description: API key name
        keyPrefix:
          type: string
          description: Key prefix for identification
        scopes:
          type: array
          nullable: true
          items:
            type: string
          description: Permission scopes
        createdAt:
          type: string
          format: date-time
          description: Creation timestamp
        lastUsedAt:
          type: string
          nullable: true
          format: date-time
          description: Last used timestamp
      required:
        - id
        - name
        - keyPrefix
        - createdAt
    CreateApiKey:
      type: object
      properties:
        name:
          type: string
          description: API key name
        organizationId:
          type: string
          description: Organization to create key for
        scopes:
          type: array
          items:
            type: string
          description: Permission scopes
      required:
        - name
        - organizationId
    Invitation:
      type: object
      properties:
        id:
          type: string
          description: Invitation ID
        email:
          type: string
          format: email
          description: Invitee email
        role:
          type: string
          enum:
            - admin
            - member
          description: Role to assign
        status:
          type: string
          description: Invitation status
        expiresAt:
          type: string
          format: date-time
          description: Expiration timestamp
        organizationName:
          type: string
          description: Organization name
      required:
        - id
        - email
        - role
        - status
        - expiresAt
        - organizationName
    CreateInvitation:
      type: object
      properties:
        email:
          type: string
          format: email
          description: Invitee email address
        role:
          type: string
          enum:
            - admin
            - member
          description: Role to assign (defaults to member)
      required:
        - email
    BillingConfig:
      type: object
      properties:
        provider:
          type: string
          enum:
            - stripe
            - paypal
            - razorpay
          description: Payment provider
        secretKey:
          type: string
          description: Provider secret key
        webhookSecret:
          type: string
          description: Webhook signing secret
        isSandbox:
          type: boolean
          description: Whether to use sandbox/test mode
      required:
        - provider
        - secretKey
    BillingPortalRequest:
      type: object
      properties:
        returnUrl:
          type: string
          format: uri
          description: URL to return to after portal session
    BillingSubscribe:
      type: object
      properties:
        planId:
          type: string
          description: Product/Plan ID to subscribe to
        organizationId:
          type: string
          description: Organization ID
        seats:
          type: number
          minimum: 1
          description: 'Number of seats (default: 1)'
        planType:
          type: string
          enum:
            - monthly
            - yearly
          description: Plan type
        returnUrl:
          type: string
          format: uri
          description: Return URL after checkout
        cancelUrl:
          type: string
          format: uri
          description: Cancel URL
      required:
        - planId
        - organizationId
    Success:
      type: object
      properties:
        success:
          type: boolean
          description: Operation success status
      required:
        - success
    Error:
      type: object
      properties:
        error:
          type: string
          description: Error message
        code:
          type: string
          description: Error code
        details:
          type: object
          additionalProperties:
            nullable: true
          description: Additional error details
      required:
        - error
    Pagination:
      type: object
      properties:
        page:
          type: number
          description: Current page number
        limit:
          type: number
          description: Items per page
        total:
          type: number
          description: Total number of items
        totalPages:
          type: number
          description: Total number of pages
      required:
        - page
        - limit
        - total
        - totalPages
  parameters: {}
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: JWT access token from OAuth flow or API key
paths:
  /api/v1/users:
    get:
      summary: List users
      description: Get a paginated list of users in the organization
      tags:
        - Users
      security:
        - bearerAuth: []
      parameters:
        - schema:
            type: string
            description: Page number
          required: false
          name: page
          in: query
        - schema:
            type: string
            description: Items per page
          required: false
          name: limit
          in: query
        - schema:
            type: string
            description: Search by name or email
          required: false
          name: search
          in: query
      responses:
        '200':
          description: List of users
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/User'
                  meta:
                    $ref: '#/components/schemas/Pagination'
                required:
                  - data
                  - meta
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
    post:
      summary: Create user
      description: Create a new user in the organization
      tags:
        - Users
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateUser'
      responses:
        '201':
          description: User created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'
        '400':
          description: Validation error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
  /api/v1/users/{id}:
    get:
      summary: Get user
      description: Get a user by ID
      tags:
        - Users
      security:
        - bearerAuth: []
      parameters:
        - schema:
            type: string
            description: User ID
          required: true
          name: id
          in: path
      responses:
        '200':
          description: User details
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'
        '404':
          description: User not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
  /api/v1/organizations:
    get:
      summary: List organizations
      description: Get organizations the user has access to
      tags:
        - Organizations
      security:
        - bearerAuth: []
      responses:
        '200':
          description: List of organizations
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/Organization'
                required:
                  - data
    post:
      summary: Create organization
      description: Create a new organization
      tags:
        - Organizations
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateOrganization'
      responses:
        '201':
          description: Organization created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Organization'
  /api/v1/oauth-clients:
    get:
      summary: List OAuth clients
      description: Get OAuth clients for the organization
      tags:
        - OAuth Clients
      security:
        - bearerAuth: []
      responses:
        '200':
          description: List of OAuth clients
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/OAuthClient'
                required:
                  - data
    post:
      summary: Create OAuth client
      description: Register a new OAuth client application
      tags:
        - OAuth Clients
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateOAuthClient'
      responses:
        '201':
          description: OAuth client created
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/OAuthClient'
                  - type: object
                    properties:
                      clientSecret:
                        type: string
                        description: Client secret (only shown once)
  /api/v1/products:
    get:
      summary: List products
      description: Get available products
      tags:
        - Products
      security:
        - bearerAuth: []
      responses:
        '200':
          description: List of products
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/Product'
                required:
                  - data
  /api/v1/subscriptions:
    get:
      summary: List subscriptions
      description: Get organization subscriptions
      tags:
        - Subscriptions
      security:
        - bearerAuth: []
      responses:
        '200':
          description: List of subscriptions
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/Subscription'
                required:
                  - data
    post:
      summary: Create subscription
      description: Create a new subscription for an organization
      tags:
        - Subscriptions
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateSubscription'
      responses:
        '200':
          description: Subscription created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Subscription'
        '403':
          description: Must be owner or admin to create subscriptions
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
  /api/v1/licenses:
    get:
      summary: List licenses
      description: Get user product licenses
      tags:
        - Licenses
      security:
        - bearerAuth: []
      responses:
        '200':
          description: List of licenses
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/License'
                required:
                  - data
  /api/v1/users/me:
    get:
      summary: Get current user
      description: Get the currently authenticated user profile
      tags:
        - Users
      security:
        - bearerAuth: []
      responses:
        '200':
          description: Current user profile
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
    patch:
      summary: Update current user
      description: Update the currently authenticated user profile
      tags:
        - Users
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UpdateUser'
      responses:
        '200':
          description: Updated user profile
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'
    delete:
      summary: Delete current user
      description: Delete the currently authenticated user account
      tags:
        - Users
      security:
        - bearerAuth: []
      responses:
        '200':
          description: Account deleted
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Success'
  /api/v1/organizations/{id}:
    get:
      summary: Get organization
      description: Get organization details by ID
      tags:
        - Organizations
      security:
        - bearerAuth: []
      parameters:
        - schema:
            type: string
            description: Organization ID
          required: true
          name: id
          in: path
      responses:
        '200':
          description: Organization details
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Organization'
        '404':
          description: Organization not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
    patch:
      summary: Update organization
      description: Update organization details (owner or admin only)
      tags:
        - Organizations
      security:
        - bearerAuth: []
      parameters:
        - schema:
            type: string
            description: Organization ID
          required: true
          name: id
          in: path
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UpdateOrganization'
      responses:
        '200':
          description: Updated organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Organization'
        '403':
          description: Insufficient permissions
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
    delete:
      summary: Delete organization
      description: Delete an organization (owner only)
      tags:
        - Organizations
      security:
        - bearerAuth: []
      parameters:
        - schema:
            type: string
            description: Organization ID
          required: true
          name: id
          in: path
      responses:
        '200':
          description: Organization deleted
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Success'
        '403':
          description: Only owners can delete organizations
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
  /api/v1/organizations/{id}/invite:
    post:
      summary: Invite user to organization
      description: Send an invitation to join the organization
      tags:
        - Organizations
      security:
        - bearerAuth: []
      parameters:
        - schema:
            type: string
            description: Organization ID
          required: true
          name: id
          in: path
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateInvitation'
      responses:
        '201':
          description: Invitation created
          content:
            application/json:
              schema:
                type: object
                properties:
                  invitation:
                    $ref: '#/components/schemas/Invitation'
                required:
                  - invitation
        '400':
          description: User already a member or invitation pending
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
  /api/v1/products/{id}:
    get:
      summary: Get product
      description: Get product details by ID
      tags:
        - Products
      security:
        - bearerAuth: []
      parameters:
        - schema:
            type: string
            description: Product ID
          required: true
          name: id
          in: path
      responses:
        '200':
          description: Product details
          content:
            application/json:
              schema:
                type: object
                properties:
                  product:
                    $ref: '#/components/schemas/Product'
                required:
                  - product
        '404':
          description: Product not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
    patch:
      summary: Update product
      description: Update product details
      tags:
        - Products
      security:
        - bearerAuth: []
      parameters:
        - schema:
            type: string
            description: Product ID
          required: true
          name: id
          in: path
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UpdateProduct'
      responses:
        '200':
          description: Updated product
          content:
            application/json:
              schema:
                type: object
                properties:
                  product:
                    $ref: '#/components/schemas/Product'
                required:
                  - product
    delete:
      summary: Delete product
      description: Delete a product
      tags:
        - Products
      security:
        - bearerAuth: []
      parameters:
        - schema:
            type: string
            description: Product ID
          required: true
          name: id
          in: path
      responses:
        '200':
          description: Product deleted
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Success'
  /api/v1/licenses/check:
    get:
      summary: Check feature access
      description: Check if a specific feature is enabled for the user
      tags:
        - Licenses
      security:
        - bearerAuth: []
      parameters:
        - schema:
            type: string
            description: Feature name to check
          required: true
          name: feature
          in: query
        - schema:
            type: string
            description: Organization ID (optional)
          required: false
          name: organizationId
          in: query
      responses:
        '200':
          description: Feature check result
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/LicenseCheck'
  /api/v1/licenses/entitlements:
    get:
      summary: List entitlements
      description: Get all feature entitlements for the user
      tags:
        - Licenses
      security:
        - bearerAuth: []
      responses:
        '200':
          description: List of entitlements
          content:
            application/json:
              schema:
                type: object
                properties:
                  entitlements:
                    type: array
                    items:
                      $ref: '#/components/schemas/Entitlement'
                required:
                  - entitlements
  /api/v1/webhooks:
    get:
      summary: List webhooks
      description: Get all webhooks for the organization
      tags:
        - Webhooks
      security:
        - bearerAuth: []
      responses:
        '200':
          description: List of webhooks
          content:
            application/json:
              schema:
                type: object
                properties:
                  webhooks:
                    type: array
                    items:
                      $ref: '#/components/schemas/Webhook'
                required:
                  - webhooks
    post:
      summary: Create webhook
      description: Register a new webhook endpoint
      tags:
        - Webhooks
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateWebhook'
      responses:
        '201':
          description: Webhook created (includes secret, only shown once)
          content:
            application/json:
              schema:
                type: object
                properties:
                  webhook:
                    allOf:
                      - $ref: '#/components/schemas/Webhook'
                      - type: object
                        properties:
                          secret:
                            type: string
                            description: Webhook secret for HMAC signing (only shown once)
                        required:
                          - secret
                required:
                  - webhook
  /api/v1/webhooks/{id}:
    get:
      summary: Get webhook
      description: Get webhook details by ID
      tags:
        - Webhooks
      security:
        - bearerAuth: []
      parameters:
        - schema:
            type: string
            description: Webhook ID
          required: true
          name: id
          in: path
      responses:
        '200':
          description: Webhook details
          content:
            application/json:
              schema:
                type: object
                properties:
                  webhook:
                    $ref: '#/components/schemas/Webhook'
                required:
                  - webhook
        '404':
          description: Webhook not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
    patch:
      summary: Update webhook
      description: Update webhook configuration
      tags:
        - Webhooks
      security:
        - bearerAuth: []
      parameters:
        - schema:
            type: string
            description: Webhook ID
          required: true
          name: id
          in: path
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UpdateWebhook'
      responses:
        '200':
          description: Updated webhook
          content:
            application/json:
              schema:
                type: object
                properties:
                  webhook:
                    $ref: '#/components/schemas/Webhook'
                required:
                  - webhook
    delete:
      summary: Delete webhook
      description: Delete a webhook
      tags:
        - Webhooks
      security:
        - bearerAuth: []
      parameters:
        - schema:
            type: string
            description: Webhook ID
          required: true
          name: id
          in: path
      responses:
        '200':
          description: Webhook deleted
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Success'
  /api/v1/api-keys:
    get:
      summary: List API keys
      description: Get all API keys for the authenticated user
      tags:
        - API Keys
      security:
        - bearerAuth: []
      responses:
        '200':
          description: List of API keys
          content:
            application/json:
              schema:
                type: object
                properties:
                  keys:
                    type: array
                    items:
                      $ref: '#/components/schemas/ApiKey'
                required:
                  - keys
    post:
      summary: Create API key
      description: Create a new API key (key is only shown once)
      tags:
        - API Keys
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateApiKey'
      responses:
        '200':
          description: API key created (full key only shown once)
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/ApiKey'
                  - type: object
                    properties:
                      key:
                        type: string
                        description: Full API key (only shown once)
                    required:
                      - key
    delete:
      summary: Delete API key
      description: Revoke an API key
      tags:
        - API Keys
      security:
        - bearerAuth: []
      parameters:
        - schema:
            type: string
            description: API key ID to delete
          required: true
          name: id
          in: query
      responses:
        '200':
          description: API key deleted
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Success'
        '404':
          description: Key not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
  /api/v1/billing/products:
    get:
      summary: List billing products
      description: Get available products for billing
      tags:
        - Billing
      security:
        - bearerAuth: []
      responses:
        '200':
          description: List of billing products
          content:
            application/json:
              schema:
                type: object
                properties:
                  products:
                    type: array
                    items:
                      $ref: '#/components/schemas/Product'
                required:
                  - products
  /api/v1/billing/config:
    put:
      summary: Configure billing
      description: Configure payment provider settings for the organization
      tags:
        - Billing
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/BillingConfig'
      responses:
        '200':
          description: Billing configured
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Success'
  /api/v1/billing/portal:
    post:
      summary: Create billing portal session
      description: Create a Stripe billing portal session for subscription management
      tags:
        - Billing
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/BillingPortalRequest'
      responses:
        '200':
          description: Portal session URL
          content:
            application/json:
              schema:
                type: object
                properties:
                  url:
                    type: string
                    format: uri
                required:
                  - url
  /api/v1/billing/subscribe:
    post:
      summary: Create subscription checkout
      description: Create a checkout session to subscribe to a plan
      tags:
        - Billing
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/BillingSubscribe'
      responses:
        '200':
          description: Checkout URL
          content:
            application/json:
              schema:
                type: object
                properties:
                  checkoutUrl:
                    type: string
                    format: uri
                    description: URL to redirect user to for payment
                  subscriptionId:
                    type: string
                    description: Pending subscription ID
                required:
                  - checkoutUrl
                  - subscriptionId
