openapi: 3.0.3
info:
  title: Loro Streams Dashboard API
  version: 0.1.0
  description: |
    Public API surface of the Loro Streams dashboard service.

    The current deployment and internal service name may still use `riverrun-dashboard`;
    that is an implementation detail of the underlying infra.

    This document includes:
    - Better Auth endpoints used by the developer portal
    - Custom API key management endpoints
    - Dashboard read APIs (buckets, usage, quota, storage)

    Note: Better Auth exposes additional endpoints under `/api/auth/*` that are not listed here.
servers:
  - url: https://{dashboard_host}
    variables:
      dashboard_host:
        default: streams.loro.dev

tags:
  - name: Auth
    description: Better Auth endpoints used for developer sign-up/sign-in/session.
  - name: CliAuth
    description: CLI-specific auth bridge endpoints for device flow and token lifecycle.
  - name: ApiKey
    description: Custom API key CRUD + exchange flows.
  - name: Dashboard
    description: Organization-scoped dashboard metrics and metadata.

paths:
  /api/auth/sign-up/email:
    post:
      tags: [Auth]
      summary: Sign up with email/password
      description: Returns `400 EMAIL_PASSWORD_SIGN_UP_DISABLED` while `DASHBOARD_DISABLE_SIGN_UP=true`.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [email, password, name]
              properties:
                email:
                  type: string
                  format: email
                password:
                  type: string
                name:
                  type: string
      responses:
        '200':
          description: Sign-up accepted (Better Auth default payload)
          content:
            application/json:
              schema:
                type: object
                additionalProperties: true
        '400':
          description: Invalid request or new-user registration is temporarily disabled

  /api/auth/sign-in/email:
    post:
      tags: [Auth]
      summary: Sign in with email/password
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [email, password]
              properties:
                email:
                  type: string
                  format: email
                password:
                  type: string
      responses:
        '200':
          description: Sign-in success (session cookie is set)
          content:
            application/json:
              schema:
                type: object
                additionalProperties: true
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/auth/sign-in/social:
    post:
      tags: [Auth]
      summary: Start social OAuth sign-in (GitHub)
      description: |
        Returns the upstream provider authorization URL and redirect intent.
        The client should navigate to `url` when `redirect=true`.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [provider]
              properties:
                provider:
                  type: string
                  enum: [github]
                callbackURL:
                  type: string
                  format: uri
                errorCallbackURL:
                  type: string
                  format: uri
                newUserCallbackURL:
                  type: string
                  format: uri
                disableRedirect:
                  type: boolean
      responses:
        '200':
          description: OAuth start response
          content:
            application/json:
              schema:
                type: object
                required: [url, redirect]
                properties:
                  url:
                    type: string
                    format: uri
                  redirect:
                    type: boolean
        '404':
          description: Provider is not configured
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  /api/auth/callback/{provider}:
    get:
      tags: [Auth]
      summary: OAuth callback endpoint
      parameters:
        - in: path
          name: provider
          required: true
          schema:
            type: string
            enum: [github]
      responses:
        '302':
          description: Redirects to callbackURL/error URL
    post:
      tags: [Auth]
      summary: OAuth callback endpoint (POST variant)
      parameters:
        - in: path
          name: provider
          required: true
          schema:
            type: string
            enum: [github]
      responses:
        '302':
          description: Redirects to callbackURL/error URL

  /api/auth/get-session:
    get:
      tags: [Auth]
      summary: Get current session
      responses:
        '200':
          description: Session object or null
          content:
            application/json:
              schema:
                type: object
                nullable: true
                additionalProperties: true

  /api/auth/sign-out:
    post:
      tags: [Auth]
      summary: Sign out current user
      security:
        - SessionCookie: []
      responses:
        '200':
          description: Signed out
          content:
            application/json:
              schema:
                type: object
                required: [success]
                properties:
                  success:
                    type: boolean

  /api/auth/jwks:
    get:
      tags: [Auth]
      summary: Get JWKS for JWT verification
      responses:
        '200':
          description: JSON Web Key Set
          content:
            application/json:
              schema:
                type: object
                required: [keys]
                properties:
                  keys:
                    type: array
                    items:
                      type: object
                      additionalProperties: true

  /api/auth/cli/start:
    post:
      tags: [CliAuth]
      summary: Start CLI device login session
      description: |
        Starts a device-like OAuth flow for CLI login.
        Returns a one-time device code + verification URL for browser approval.
      requestBody:
        required: false
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CliStartRequest'
      responses:
        '200':
          description: Device login session created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CliStartResponse'
        '400':
          $ref: '#/components/responses/BadRequest'

  /api/auth/cli/poll:
    post:
      tags: [CliAuth]
      summary: Poll CLI device login status
      description: |
        Polls the device flow status. Returns tokens after user approval.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CliPollRequest'
      responses:
        '200':
          description: Authorization completed and CLI tokens issued
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CliTokenResponse'
        '400':
          description: Pending/slow-down/expired or invalid request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  /api/auth/cli/refresh:
    post:
      tags: [CliAuth]
      summary: Refresh CLI access token
      description: |
        Exchanges a CLI refresh token (session token) for a fresh access token.
        - `audience=dashboard`: returns session-style bearer token.
        - `audience=gateway`: returns short-lived JWT.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CliRefreshRequest'
      responses:
        '200':
          description: Access token refreshed
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CliTokenResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/auth/cli/revoke:
    post:
      tags: [CliAuth]
      summary: Revoke CLI refresh token
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CliRevokeRequest'
      responses:
        '200':
          description: Token revoked (idempotent)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CliRevokeResponse'
        '400':
          $ref: '#/components/responses/BadRequest'

  /api/auth/cli/whoami:
    get:
      tags: [CliAuth]
      summary: Get current CLI identity and active organization context
      security:
        - DashboardBearer: []
      responses:
        '200':
          description: Current identity
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CliWhoamiResponse'
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/auth/api-key:
    post:
      tags: [ApiKey]
      summary: Create API key
      security:
        - SessionCookie: []
        - DashboardBearer: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ApiKeyCreateRequest'
      responses:
        '201':
          description: API key created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiKeyCreateResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'

  /api/auth/api-key/list:
    get:
      tags: [ApiKey]
      summary: List API keys in current org
      security:
        - SessionCookie: []
        - DashboardBearer: []
      responses:
        '200':
          description: API key list
          content:
            application/json:
              schema:
                type: object
                required: [keys]
                properties:
                  keys:
                    type: array
                    items:
                      $ref: '#/components/schemas/ApiKeyListItem'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'

  /api/auth/api-key/revoke:
    post:
      tags: [ApiKey]
      summary: Revoke API key in current org
      security:
        - SessionCookie: []
        - DashboardBearer: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [id]
              properties:
                id:
                  type: string
      responses:
        '200':
          description: Revoked
          content:
            application/json:
              schema:
                type: object
                required: [revoked]
                properties:
                  revoked:
                    type: boolean
                    enum: [true]
        '404':
          description: API key not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'

  /api/auth/api-key/verify:
    post:
      tags: [ApiKey]
      summary: Verify API key (gateway/public use)
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [key]
              properties:
                key:
                  type: string
      responses:
        '200':
          description: Key is valid
          content:
            application/json:
              schema:
                type: object
                required: [valid, name, referenceId, tenantId, organizationId, email, permissions]
                properties:
                  valid:
                    type: boolean
                    enum: [true]
                  name:
                    type: string
                  referenceId:
                    type: string
                  tenantId:
                    type: string
                  organizationId:
                    type: string
                    nullable: true
                  email:
                    type: string
                  permissions:
                    $ref: '#/components/schemas/AccessTokenScope'
        '400':
          description: Missing key
          content:
            application/json:
              schema:
                type: object
                required: [valid, error]
                properties:
                  valid:
                    type: boolean
                    enum: [false]
                  error:
                    type: string
        '401':
          description: Invalid API key
          content:
            application/json:
              schema:
                type: object
                required: [valid]
                properties:
                  valid:
                    type: boolean
                    enum: [false]

  /api/auth/api-key/token:
    post:
      tags: [ApiKey]
      summary: Exchange API key for short-lived JWT
      description: >
        Authenticates the API key and optionally narrows its scope and expiry.
        A caller-supplied deadline never grants authority or proves device
        authentication. JWT exp is capped by the server authorization-start time
        plus 900 seconds and the stored API key expiry, if any. A trusted upstream
        issuer must preserve its own earlier deadline across retries and cached
        authorization results. This endpoint does not enforce a data-plane
        connection or final-write deadline.
      security:
        - ApiKeyBearer: []
      requestBody:
        required: false
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ApiKeyTokenRequest'
      responses:
        '200':
          description: JWT issued
          content:
            application/json:
              schema:
                type: object
                required: [token, expires_in, expires_at]
                properties:
                  token:
                    type: string
                  expires_in:
                    type: integer
                    minimum: 1
                    maximum: 900
                    description: Whole seconds remaining at response creation, rounded down.
                    example: 900
                  expires_at:
                    type: integer
                    description: Absolute Unix seconds; identical to the signed JWT exp. Cache this value unchanged.
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'

  /api/dashboard/buckets:
    get:
      tags: [Dashboard]
      summary: List active buckets for current org
      security:
        - SessionCookie: []
        - DashboardBearer: []
      responses:
        '200':
          description: Bucket list
          content:
            application/json:
              schema:
                type: object
                required: [buckets]
                properties:
                  buckets:
                    type: array
                    items:
                      $ref: '#/components/schemas/BucketEntry'
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/dashboard/usage:
    get:
      tags: [Dashboard]
      summary: Get aggregated usage by operation
      description: >-
        Use granularity=hour for ranges containing rolled-up usage. from is
        rounded down and to rounded up to UTC hours, with an exclusive upper
        bound; the response reports the effective range. Omit granularity only
        for legacy raw-only inclusive ranges or unbounded totals. Historical
        and new sources must contain disjoint requests.
      security:
        - SessionCookie: []
        - DashboardBearer: []
      parameters:
        - in: query
          name: granularity
          schema:
            type: string
            enum: [hour]
          required: false
        - in: query
          name: from
          schema:
            type: string
            format: date-time
          required: false
        - in: query
          name: to
          schema:
            type: string
            format: date-time
          required: false
      responses:
        '200':
          description: Usage stats
          content:
            application/json:
              schema:
                type: object
                required: [usage, granularity, effective_from, effective_to, to_exclusive]
                properties:
                  usage:
                    type: array
                    items:
                      $ref: '#/components/schemas/UsageEntry'
                  granularity:
                    type: string
                    enum: [hour, legacy]
                  effective_from:
                    type: string
                    format: date-time
                    nullable: true
                  effective_to:
                    type: string
                    format: date-time
                    nullable: true
                  to_exclusive:
                    type: boolean
        '400':
          description: Invalid dates, reversed range, empty hourly range or unsupported granularity
        '409':
          description: Bounded legacy query overlaps rolled-up usage; retry with granularity=hour
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/dashboard/quota:
    get:
      tags: [Dashboard]
      summary: Get quota and billing state for current tenant
      security:
        - SessionCookie: []
        - DashboardBearer: []
      responses:
        '200':
          description: Quota state (or null when absent)
          content:
            application/json:
              schema:
                type: object
                required: [quota]
                properties:
                  quota:
                    allOf:
                      - $ref: '#/components/schemas/QuotaEntry'
                    nullable: true
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/dashboard/storage:
    get:
      tags: [Dashboard]
      summary: Get storage trend snapshots
      security:
        - SessionCookie: []
        - DashboardBearer: []
      responses:
        '200':
          description: Storage trend data
          content:
            application/json:
              schema:
                type: object
                required: [storage]
                properties:
                  storage:
                    type: array
                    items:
                      $ref: '#/components/schemas/StorageEntry'
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/dashboard/organizations:
    get:
      tags: [Dashboard]
      summary: List organizations available to current user
      security:
        - SessionCookie: []
        - DashboardBearer: []
      responses:
        '200':
          description: Organization list with current active organization context
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OrganizationsListResponse'
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/dashboard/organizations/activate:
    post:
      tags: [Dashboard]
      summary: Set active organization context for current session
      security:
        - SessionCookie: []
        - DashboardBearer: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/OrganizationsActivateRequest'
      responses:
        '200':
          description: Active organization switched
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OrganizationsActivateResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/dashboard/members:
    get:
      tags: [Dashboard]
      summary: List members for active or selected organization
      security:
        - SessionCookie: []
        - DashboardBearer: []
      parameters:
        - in: query
          name: organization_id
          schema:
            type: string
          required: false
      responses:
        '200':
          description: Member list
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MembersListResponse'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'

  /api/dashboard/members/invite:
    post:
      tags: [Dashboard]
      summary: Invite a member into organization
      security:
        - SessionCookie: []
        - DashboardBearer: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MembersInviteRequest'
      responses:
        '200':
          description: Invitation created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MembersInviteResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'

  /api/dashboard/members/remove:
    post:
      tags: [Dashboard]
      summary: Remove a member from organization
      security:
        - SessionCookie: []
        - DashboardBearer: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MembersRemoveRequest'
      responses:
        '200':
          description: Member removed
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MembersRemoveResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'

components:
  securitySchemes:
    SessionCookie:
      type: apiKey
      in: cookie
      name: better-auth.session_token
      description: |
        Better Auth session cookie. Actual cookie name may vary by deployment options.
    ApiKeyBearer:
      type: http
      scheme: bearer
      bearerFormat: API Key
      description: "API key raw value in `Authorization: Bearer sk_riverrun_...` (historical prefix kept for compatibility)."
    DashboardBearer:
      type: http
      scheme: bearer
      bearerFormat: Dashboard Access Token
      description: |
        Better Auth dashboard access token sent as `Authorization: Bearer <token>`.
        This is the token returned by `/api/auth/cli/refresh` with `audience=dashboard`
        and the token the CLI sends to dashboard/control-plane endpoints.

  responses:
    BadRequest:
      description: Bad request
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    Unauthorized:
      description: Unauthorized
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    Forbidden:
      description: Forbidden
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'

  schemas:
    ErrorResponse:
      type: object
      required: [error]
      properties:
        error:
          type: string
        required_role:
          type: string

    CliStartRequest:
      type: object
      properties:
        provider:
          type: string
          enum: [github]

    CliStartResponse:
      type: object
      required: [device_code, user_code, verification_url, expires_at, interval_sec]
      properties:
        device_code:
          type: string
        user_code:
          type: string
        verification_url:
          type: string
          format: uri
        expires_at:
          type: string
          format: date-time
        interval_sec:
          type: integer
          minimum: 1

    CliPollRequest:
      type: object
      required: [device_code]
      properties:
        device_code:
          type: string

    CliRefreshRequest:
      type: object
      required: [refresh_token]
      properties:
        refresh_token:
          type: string
        audience:
          type: string
          enum: [dashboard, gateway]

    CliUser:
      type: object
      properties:
        id:
          type: string
        email:
          type: string
          format: email
        name:
          type: string
      additionalProperties: true

    CliOrganization:
      type: object
      required: [id, role]
      properties:
        id:
          type: string
        role:
          type: string

    CliTokenResponse:
      type: object
      required: [access_token, refresh_token, expires_in]
      properties:
        access_token:
          type: string
        refresh_token:
          type: string
        expires_in:
          type: integer
        organization_id:
          type: string
        user:
          $ref: '#/components/schemas/CliUser'

    CliRevokeRequest:
      type: object
      required: [refresh_token]
      properties:
        refresh_token:
          type: string

    CliRevokeResponse:
      type: object
      required: [revoked]
      properties:
        revoked:
          type: boolean
          enum: [true]

    CliWhoamiResponse:
      type: object
      required: [user, organization]
      properties:
        user:
          $ref: '#/components/schemas/CliUser'
        organization:
          $ref: '#/components/schemas/CliOrganization'

    ResourceSet:
      type: object
      properties:
        none:
          type: boolean
          description: >
            When true, matches no resources (takes precedence over exact/prefix).
            Control-plane tokens with no data-plane stream access should set
            `{"none": true, "exact": "/"}`.
        exact:
          type: string
        prefix:
          type: string

    OpGroupFlags:
      type: object
      properties:
        read:
          type: boolean
        write:
          type: boolean

    AccessTokenScope:
      type: object
      properties:
        buckets:
          $ref: '#/components/schemas/ResourceSet'
        streams:
          $ref: '#/components/schemas/ResourceSet'
        spaces:
          $ref: '#/components/schemas/ResourceSet'
        space_generation:
          type: integer
          minimum: 1
          description: >
            Optional positive UserSpace generation pin. Omitted values default
            implicitly to 1 for UserSpace requests.
        op_groups:
          type: object
          properties:
            bucket:
              $ref: '#/components/schemas/OpGroupFlags'
            stream:
              $ref: '#/components/schemas/OpGroupFlags'
            blob:
              $ref: '#/components/schemas/OpGroupFlags'
        ops:
          type: array
          items:
            type: string

    ApiKeyCreateRequest:
      type: object
      required: [name, bucket]
      properties:
        name:
          type: string
          maxLength: 256
        bucket:
          type: string
          description: >
            Existing bucket id in the authenticated organization. The server
            checks bucket ownership and generates an exact-bucket gateway scope.
        allowSpaceAdmin:
          type: boolean
          default: false
          description: >
            Opt in to issuing a bucket-level UserSpace issuer key capable of
            minting Tier 1/2/3 UserSpace JWTs across spaces and generations.
        expiresInSeconds:
          type: integer
          minimum: 1
          maximum: 3153600000

    ApiKeyCreateResponse:
      type: object
      required: [id, key]
      properties:
        id:
          type: string
        key:
          type: string
          description: Raw API key (shown only once).

    ApiKeyTokenRequest:
      type: object
      additionalProperties: false
      properties:
        expires_at:
          type: integer
          minimum: 1
          maximum: 9007199254740991
          description: >
            Optional absolute Unix-seconds deadline that can only shorten the
            server-issued grant. Invalid values return 400. If the effective
            deadline has less than one whole second remaining, before or after
            signing, the exchange returns 401 without a token. Reuse the same
            deadline on retries; only a fresh upstream authorization check may
            establish a new upstream deadline. This value is not authorization
            evidence and must not be accepted from an untrusted end user as such.
        scope:
          allOf:
            - $ref: '#/components/schemas/AccessTokenScope'
          description: >
            Optional narrower JWT scope. When omitted, the issued JWT keeps the
            same scope as the API key permissions. When provided, it must be a
            subset of the API key permissions.

    ApiKeyListItem:
      type: object
      required:
        - id
        - name
        - keyPreview
        - referenceId
        - permissions
        - enabled
        - expiresAt
        - createdAt
      properties:
        id:
          type: string
        name:
          type: string
        keyPreview:
          type: string
        referenceId:
          type: string
        permissions:
          $ref: '#/components/schemas/AccessTokenScope'
        enabled:
          type: boolean
        expiresAt:
          type: string
          format: date-time
          nullable: true
        createdAt:
          type: string
          format: date-time

    BucketEntry:
      type: object
      required: [bucket_id, current_bytes, stream_count, created_at]
      properties:
        bucket_id:
          type: string
        current_bytes:
          type: integer
        stream_count:
          type: integer
        created_at:
          type: string
          format: date-time

    UsageEntry:
      type: object
      required: [operation, request_count, request_bytes, response_bytes]
      properties:
        operation:
          type: string
        request_count:
          $ref: '#/components/schemas/UsageInteger'
        request_bytes:
          $ref: '#/components/schemas/UsageInteger'
        response_bytes:
          $ref: '#/components/schemas/UsageInteger'

    UsageInteger:
      description: JSON number when safe in JavaScript; otherwise an exact decimal string
      oneOf:
        - type: integer
          minimum: 0
          maximum: 9007199254740991
        - type: string
          pattern: '^[0-9]+$'

    QuotaEntry:
      type: object
      required: [max_storage_bytes, max_ops_per_month, billing_balance, suspended, updated_at]
      properties:
        max_storage_bytes:
          type: integer
          nullable: true
        max_ops_per_month:
          type: integer
          nullable: true
        billing_balance:
          type: string
          nullable: true
        suspended:
          type: boolean
        updated_at:
          type: string
          format: date-time

    StorageEntry:
      type: object
      required: [sampled_at, total_bytes, reclaimable_bytes]
      properties:
        sampled_at:
          type: string
          format: date-time
        total_bytes:
          type: integer
        reclaimable_bytes:
          type: integer

    OrganizationEntry:
      type: object
      required: [id, name, slug, createdAt]
      properties:
        id:
          type: string
        name:
          type: string
        slug:
          type: string
        createdAt:
          type: string
          format: date-time
        logo:
          type: string
          nullable: true
        metadata:
          type: object
          nullable: true
          additionalProperties: true
      additionalProperties: true

    OrganizationsListResponse:
      type: object
      required: [organizations, active_organization_id]
      properties:
        organizations:
          type: array
          items:
            $ref: '#/components/schemas/OrganizationEntry'
        active_organization_id:
          type: string
          nullable: true

    OrganizationsActivateRequest:
      type: object
      required: [organization_id]
      properties:
        organization_id:
          type: string

    OrganizationsActivateResponse:
      type: object
      required: [activated, organization_id, organization]
      properties:
        activated:
          type: boolean
          enum: [true]
        organization_id:
          type: string
        organization:
          allOf:
            - $ref: '#/components/schemas/OrganizationEntry'
          nullable: true

    MemberUser:
      type: object
      required: [id, email, name]
      properties:
        id:
          type: string
        email:
          type: string
          format: email
        name:
          type: string
        image:
          type: string
          nullable: true
      additionalProperties: true

    MemberEntry:
      type: object
      required: [id, organizationId, userId, role, createdAt, user]
      properties:
        id:
          type: string
        organizationId:
          type: string
        userId:
          type: string
        role:
          type: string
        createdAt:
          type: string
          format: date-time
        user:
          $ref: '#/components/schemas/MemberUser'
      additionalProperties: true

    MembersListResponse:
      type: object
      required: [members, total]
      properties:
        members:
          type: array
          items:
            $ref: '#/components/schemas/MemberEntry'
        total:
          type: integer

    MembersInviteRequest:
      type: object
      required: [email, role]
      properties:
        email:
          type: string
          format: email
        role:
          type: string
          enum: [member, admin, owner]
        organization_id:
          type: string

    MembersInviteResponse:
      type: object
      required: [invitation]
      properties:
        invitation:
          type: object
          additionalProperties: true

    MembersRemoveRequest:
      type: object
      required: [member_id_or_email]
      properties:
        member_id_or_email:
          type: string
        organization_id:
          type: string

    MembersRemoveResponse:
      type: object
      required: [removed, member]
      properties:
        removed:
          type: boolean
          enum: [true]
        member:
          type: object
          additionalProperties: true
