# This is the source of truth for Coordinator endpoints we have decided are public.
# It is NOT auto-generated; update it when endpoint contracts change.
openapi: 3.0.0
info:
  title: Arcade Coordinator API
  description: Public API surface of the Arcade Coordinator service.
  contact:
    name: Arcade
    url: https://arcade.dev
    email: contact@arcade.dev
  license:
    name: Proprietary
    url: https://arcade.dev/license
  version: 0.1.0

servers:
  - url: https://cloud.arcade.dev

tags:
  - name: Audit Logs
    description: Query audit log events for organizations and projects.
  - name: Projects
    description: Create and manage projects within an organization.
  - name: RBAC
    description: |
      Role-based access control. Browse the roles catalog, read the caller's
      effective permissions, list org/project members, and assign, change, or
      revoke org-level and project-level roles.

      These routes are gated by the per-organization `operator-authz` feature
      flag. While the flag is OFF for an organization, every route below except
      `GET /capabilities` is absent and returns `404`.

paths:
  /api/v1/orgs/{organization_id}/audit_logs:
    get:
      security:
        - Bearer: []
      tags:
        - Audit Logs
      summary: List organization audit logs
      operationId: audit-logs-list-org
      description: |
        Query audit logs for an organization with cursor-based pagination.

        Optionally filter by action, source, resource type, and time range.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ActionFilter'
        - $ref: '#/components/parameters/SourceFilter'
        - $ref: '#/components/parameters/ResourceTypeFilter'
        - $ref: '#/components/parameters/StartTime'
        - $ref: '#/components/parameters/EndTime'
        - $ref: '#/components/parameters/Cursor'
        - $ref: '#/components/parameters/Limit'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuditLogListEnvelope'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '429':
          description: Rate limit exceeded (100 req / 60 s)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/projects/{project_id}/audit_logs:
    get:
      security:
        - Bearer: []
      tags:
        - Audit Logs
      summary: List project audit logs
      operationId: audit-logs-list-project
      description: |
        Query audit logs for a specific project with cursor-based pagination.

        Optionally filter by action, source, resource type, and time range.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
        - $ref: '#/components/parameters/ActionFilter'
        - $ref: '#/components/parameters/SourceFilter'
        - $ref: '#/components/parameters/ResourceTypeFilter'
        - $ref: '#/components/parameters/StartTime'
        - $ref: '#/components/parameters/EndTime'
        - $ref: '#/components/parameters/Cursor'
        - $ref: '#/components/parameters/Limit'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuditLogListEnvelope'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the organization or project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '429':
          description: Rate limit exceeded (100 req / 60 s)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  # ── Projects ────────────────────────────────────────────────────────────────
  /api/v1/orgs/{organization_id}/projects:
    get:
      security:
        - Bearer: []
      tags:
        - Projects
      summary: List all projects
      operationId: projects-list
      description: |
        Get all projects the caller has access to within an organization.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ListProjectsResponse'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    post:
      security:
        - Bearer: []
      tags:
        - Projects
      summary: Create a new project
      operationId: projects-create
      description: |
        Create a new project within an organization.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateProjectInput'
      responses:
        '200':
          description: Created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ProjectResponseEnvelope'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not an organization owner
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/projects/{project_id}:
    get:
      security:
        - Bearer: []
      tags:
        - Projects
      summary: Get project details
      operationId: projects-get
      description: |
        Get details for a specific project.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ProjectResponseEnvelope'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- project not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    put:
      security:
        - Bearer: []
      tags:
        - Projects
      summary: Update project
      operationId: projects-update
      description: |
        Update project details (name, description, image, color).
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UpdateProjectInput'
      responses:
        '200':
          description: Updated
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ProjectResponseEnvelope'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- project not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    delete:
      security:
        - Bearer: []
      tags:
        - Projects
      summary: Delete project
      operationId: projects-delete
      description: |
        Delete a project from the organization.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
      responses:
        '200':
          description: Deleted
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SuccessEnvelope'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not an organization owner
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- project not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/projects/{project_id}/members:
    get:
      security:
        - Bearer: []
      tags:
        - Projects
      summary: List project members
      operationId: projects-members-list
      description: |
        List all members of a project.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ListProjectMembersResponse'
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    post:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: Assign an org member to this project at a role
      operationId: rbac-project-members-assign
      description: |
        Directly assign an existing organization member to this project at a
        project-level role. Requires the `members_update` project permission.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MemberAssignRequest'
      responses:
        '201':
          description: Created -- the member was assigned to the project.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MemberRowEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `members_update` on the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF, or the org/project is not visible to the caller
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: Unprocessable Entity -- the role is not a project-level role, or `account_id` is malformed
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/projects/{project_id}/members/{account_id}:
    put:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: Change a project member's role
      operationId: rbac-project-members-change-role
      description: |
        Change the project-level role of an existing project member. Requires
        the `members_update` project permission. Re-assigning the role the member
        already holds is an idempotent success.

        Note: organization admins inherit administration over every child project
        and hold no per-project role binding, so their project role cannot be
        changed through this endpoint.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
        - $ref: '#/components/parameters/AccountId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MemberRoleUpdateRequest'
      responses:
        '200':
          description: OK -- the member's role was changed (or already matched).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MemberRowEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `members_update` on the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF, or the org/project/member is not visible to the caller
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: Unprocessable Entity -- the role is not a project-level role
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store (OpenFGA) could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/projects/{project_id}/assignable-members:
    get:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: List org members assignable to this project
      operationId: rbac-project-assignable-members-list
      description: |
        List organization members who are eligible to be assigned to this
        project. Requires the `members_update` project permission.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AssignableAccountsResponseEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `members_update` on the project
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF, or the org/project is not visible to the caller
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/projects/{project_id}/members/{member_account_id}:
    delete:
      security:
        - Bearer: []
      tags:
        - Projects
      summary: Remove a member from a project
      operationId: projects-members-remove
      description: |
        Remove a member from a project. Organization owners can remove members
        even if they are not currently members of the project.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/ProjectId'
        - in: path
          name: member_account_id
          required: true
          description: Account ID of the member to remove.
          schema:
            type: string
            format: uuid
      responses:
        '204':
          description: No Content
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller cannot remove this member
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  # ── RBAC: capabilities, roles catalog, org membership & roles ─────────────────
  /api/v1/orgs/{organization_id}/capabilities:
    get:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: Get the caller's org-level capabilities
      operationId: rbac-org-capabilities
      description: |
        Report the caller's effective org-level permissions plus the billing
        capability block. Mounted regardless of the `operator-authz` flag: when
        the flag is OFF the response carries `authz.enabled = false` with no
        permissions, so a client can detect the flag state without inferring it
        from a missing route.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OrgCapabilities'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- caller is not a member of the organization (flag-OFF)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/authz/roles:
    get:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: List roles available in the organization
      operationId: rbac-roles-list
      description: |
        Return the roles catalog for the organization -- every role key with its
        display name, level, and the permission bundle it grants. Use a role key
        from this catalog as the `role` field when assigning or changing a role.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RolesResponseEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller is not a member of the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF for this organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/members:
    get:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: List organization members with their org-level role
      operationId: rbac-org-members-list
      description: |
        The organization member directory: every account in the org with its
        org-level role (or none). Requires the `members_read` org permission.
        Results are limit/offset paginated; `total_count` reports the org-wide
        member count regardless of the page requested.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - name: limit
          in: query
          required: false
          description: Results per page.
          schema:
            type: integer
            default: 100
            minimum: 1
            maximum: 500
        - name: offset
          in: query
          required: false
          description: Rows to skip before the page.
          schema:
            type: integer
            default: 0
            minimum: 0
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OrgMembersResponseEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `members_read` on the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF, or the caller is not a member of the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: Unprocessable Entity -- `limit` or `offset` is out of range
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    post:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: Assign an org-level role
      operationId: rbac-org-members-assign
      description: |
        Assign an org-level role (e.g. `org_admin`) to an organization member.
        Requires the `members_update` org permission.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MemberAssignRequest'
      responses:
        '201':
          description: Created -- the org-level role was assigned.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OrgMemberRowEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `members_update` on the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF, or the caller is not a member of the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: Unprocessable Entity -- the role is not an org-level role, or `account_id` is malformed
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store (OpenFGA) could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

  /api/v1/orgs/{organization_id}/members/{account_id}:
    put:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: Change an org-level role
      operationId: rbac-org-members-change-role
      description: |
        Change the org-level role of an organization member. Requires the
        `members_update` org permission. Re-assigning the role the member already
        holds is an idempotent success.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/AccountId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MemberRoleUpdateRequest'
      responses:
        '200':
          description: OK -- the member's org-level role was changed (or already matched).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OrgMemberRowEnvelope'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `members_update` on the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF, or the org/member is not visible to the caller
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '422':
          description: Unprocessable Entity -- the role is not an org-level role
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store (OpenFGA) could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

    delete:
      security:
        - Bearer: []
      tags:
        - RBAC
      summary: Revoke an org-level role binding
      operationId: rbac-org-members-remove
      description: |
        Revoke the caller-specified account's org-level role binding. Requires
        the `members_delete` org permission. This removes the org-level role only
        and does not affect legacy organization membership.

        Removal is fail-closed: if the policy store cannot be reached the binding
        is left intact and the request fails with `503` rather than reporting a
        removal that did not happen.
      parameters:
        - $ref: '#/components/parameters/OrganizationId'
        - $ref: '#/components/parameters/AccountId'
      responses:
        '204':
          description: No Content -- the org-level role binding was revoked.
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: Forbidden -- caller lacks `members_delete` on the organization
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '404':
          description: Not Found -- the `operator-authz` flag is OFF, or the org/member is not visible to the caller
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: Service Unavailable -- the policy store (OpenFGA) could not be reached
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'

components:
  securitySchemes:
    Bearer:
      description: 'Enter your API key or API token in the format: Bearer <token>'
      type: apiKey
      name: Authorization
      in: header

  parameters:
    OrganizationId:
      name: organization_id
      in: path
      required: true
      description: Organization ID (UUID).
      schema:
        type: string
        format: uuid

    ProjectId:
      name: project_id
      in: path
      required: true
      description: Project ID (UUID).
      schema:
        type: string
        format: uuid

    AccountId:
      name: account_id
      in: path
      required: true
      description: Account ID (UUID) of the member whose role is being changed or revoked.
      schema:
        type: string
        format: uuid

    ActionFilter:
      name: action
      in: query
      required: false
      description: |
        Filter by audit action.
        Known values: `AUDIT_ACTION_CREATED`, `AUDIT_ACTION_UPDATED`,
        `AUDIT_ACTION_DELETED`, `AUDIT_ACTION_DISABLED`.
      schema:
        type: string

    SourceFilter:
      name: source
      in: query
      required: false
      description: |
        Filter by audit source.
        Known values: `AUDIT_SOURCE_API`, `AUDIT_SOURCE_DASHBOARD`,
        `AUDIT_SOURCE_CLI`, `AUDIT_SOURCE_SDK`, `AUDIT_SOURCE_SYSTEM`.
      schema:
        type: string

    ResourceTypeFilter:
      name: resource_type
      in: query
      required: false
      description: |
        Filter by resource type.
        Examples: `gateway`, `secret`, `worker`, `api_key`, `project`, etc.
      schema:
        type: string

    StartTime:
      name: start_time
      in: query
      required: false
      description: Inclusive lower bound for event timestamp (ISO 8601).
      schema:
        type: string
        format: date-time

    EndTime:
      name: end_time
      in: query
      required: false
      description: Exclusive upper bound for event timestamp (ISO 8601).
      schema:
        type: string
        format: date-time

    Cursor:
      name: cursor
      in: query
      required: false
      description: Opaque pagination cursor returned by a previous response.
      schema:
        type: string

    Limit:
      name: limit
      in: query
      required: false
      description: Number of results per page (1--100).
      schema:
        type: integer
        default: 50
        minimum: 1
        maximum: 100

  schemas:
    # ── Response envelope ────────────────────────────────────────────────
    AuditLogListEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/AuditLogListResponse'

    ErrorEnvelope:
      type: object
      required:
        - code
        - msg
      properties:
        code:
          type: integer
          example: 400
        msg:
          type: string
          example: Bad Request
        data:
          description: Optional error details.

    # ── Payload schemas ──────────────────────────────────────────────────
    AuditLogListResponse:
      type: object
      required:
        - items
        - has_more
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/AuditLogResponse'
        next_cursor:
          type: string
          nullable: true
          description: Cursor to pass as the `cursor` query parameter for the next page.
        has_more:
          type: boolean
          description: Whether additional pages exist beyond this one.

    AuditLogResponse:
      type: object
      required:
        - id
        - event_id
        - timestamp
        - action
        - source
        - display_name
        - organization_id
        - principal_id
        - principal_type
        - created_time
      properties:
        id:
          type: string
          format: uuid
          description: Internal audit log record ID.
        event_id:
          type: string
          format: uuid
          description: Unique event ID assigned at emission time.
        timestamp:
          type: string
          format: date-time
          description: When the audited action occurred.
        client_ip:
          type: string
          nullable: true
          description: IP address of the caller, if available.
        action:
          type: string
          description: 'Audit action (e.g. `AUDIT_ACTION_CREATED`).'
        source:
          type: string
          description: 'Origin of the action (e.g. `AUDIT_SOURCE_API`).'
        display_name:
          type: string
          description: Human-readable name of the caller.
        customer_id:
          type: string
          format: uuid
          nullable: true
          description: Customer ID, if applicable.
        organization_id:
          type: string
          format: uuid
          description: Organization the event belongs to.
        project_id:
          type: string
          format: uuid
          nullable: true
          description: Project the event belongs to, if scoped to a project.
        principal_id:
          type: string
          description: Account ID or API key ID of the caller.
        user_id:
          type: string
          nullable: true
          description: User ID for user-account callers.
        principal_type:
          type: string
          description: 'Type of principal (e.g. `ACCOUNT`, `API_KEY`).'
        resource_type:
          type: string
          nullable: true
          description: 'Type of the affected resource (e.g. `gateway`, `secret`).'
        resource_id:
          type: string
          nullable: true
          description: Identifier of the affected resource.
        resource_display:
          type: string
          nullable: true
          description: Human-readable name of the affected resource.
        data:
          type: object
          nullable: true
          description: |
            Structured event payload. Shape depends on the action:
            - **created**: `{"action_type": "created", "data": {...}}`
            - **updated**: `{"action_type": "updated", "original": {...}, "updated": {...}}`
            - **deleted**: `{"action_type": "deleted", "data": {...}}`

            Sensitive fields (secrets, tokens) are masked.
        created_time:
          type: string
          format: date-time
          description: When the audit log record was persisted.

    # ── Project schemas ─────────────────────────────────────────────────────
    SuccessEnvelope:
      type: object
      required:
        - code
        - msg
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          type: object
          nullable: true
          description: Payload representing the result of an operation. May be empty for delete/success responses.

    ProjectResponse:
      type: object
      required:
        - project_id
        - organization_id
        - name
        - is_default
        - created_at
      properties:
        project_id:
          type: string
          format: uuid
          description: Unique project identifier.
        organization_id:
          type: string
          format: uuid
          description: Organization the project belongs to.
        name:
          type: string
          description: Project name.
        description:
          type: string
          nullable: true
          description: Project description.
        image_url:
          type: string
          format: uri
          nullable: true
          description: Project image URL.
        color:
          type: string
          nullable: true
          description: Project color (hex, 6 chars).
        is_default:
          type: boolean
          description: Whether this is the caller's default project.
        created_at:
          type: string
          format: date-time
          description: When the project was created.

    ProjectResponseEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/ProjectResponse'

    ListProjectsResponse:
      type: object
      required:
        - items
        - count
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/ProjectResponse'
        count:
          type: integer
          description: Total number of items.
        total_count:
          type: integer
          description: Total number of items.

    CreateProjectInput:
      type: object
      required:
        - name
      properties:
        name:
          type: string
          minLength: 1
          maxLength: 255
          description: Project name.
        description:
          type: string
          maxLength: 1000
          nullable: true
          description: Project description.
        image_url:
          type: string
          format: uri
          nullable: true
          description: Project image URL.
        color:
          type: string
          nullable: true
          description: Project color (hex, 6 characters).

    UpdateProjectInput:
      type: object
      properties:
        name:
          type: string
          minLength: 1
          maxLength: 255
          nullable: true
          description: Project name.
        description:
          type: string
          maxLength: 1000
          nullable: true
          description: Project description.
        image_url:
          type: string
          format: uri
          nullable: true
          description: Project image URL.
        color:
          type: string
          nullable: true
          description: Project color (hex, 6 characters).

    ProjectMemberResponse:
      type: object
      required:
        - account_id
        - email
        - full_name
        - actions
      properties:
        account_id:
          type: string
          format: uuid
          description: Account ID of the member.
        email:
          type: string
          format: email
          description: Email address of the member.
        full_name:
          type: string
          description: Full name of the member.
        preferred_name:
          type: string
          nullable: true
          description: Preferred name of the member.
        actions:
          type: object
          properties:
            can_remove:
              type: boolean
              description: Whether the caller can remove this member.
            cannot_remove_reason:
              type: string
              nullable: true
              description: Reason why the member cannot be removed.

    ProjectMembersViewerResponse:
      type: object
      properties:
        can_leave:
          type: boolean
          description: Whether the caller can leave the project.
        cannot_leave_reason:
          type: string
          nullable: true
          description: Reason why the caller cannot leave.
        can_remove_members:
          type: boolean
          description: Whether the caller can remove other members.

    ListProjectMembersResponse:
      type: object
      required:
        - items
        - count
        - viewer
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/ProjectMemberResponse'
        count:
          type: integer
          description: Total number of members.
        total_count:
          type: integer
          description: Total number of items.
        viewer:
          $ref: '#/components/schemas/ProjectMembersViewerResponse'

    # ── RBAC schemas ─────────────────────────────────────────────────────────
    RoleLevel:
      type: string
      enum:
        - org
        - project
      description: The object level a role applies to.

    ContainerRef:
      type: object
      required:
        - type
        - id
      properties:
        type:
          $ref: '#/components/schemas/RoleLevel'
        id:
          type: string
          description: ID of the org or project the role is held in.
        name:
          type: string
          nullable: true
          description: Display name of the container, if available.

    RoleRef:
      type: object
      required:
        - key
        - display_name
        - level
        - builtin
        - container
      properties:
        key:
          type: string
          description: 'Opaque role key (e.g. `org_admin`, `project_admin`, `project_member`).'
        display_name:
          type: string
          description: Human-readable role name.
        level:
          $ref: '#/components/schemas/RoleLevel'
        builtin:
          type: boolean
          description: Whether this is a built-in role (not a custom org role).
        container:
          $ref: '#/components/schemas/ContainerRef'

    ScopedPermission:
      type: object
      required:
        - token
        - level
      properties:
        token:
          type: string
          description: |
            Permission token held by the role, e.g. `members_update`,
            `audit_logs_read`, `tools_execute`. The same token can exist at both
            levels, so `level` disambiguates.
        level:
          $ref: '#/components/schemas/RoleLevel'

    RoleCatalogEntry:
      type: object
      required:
        - key
        - display_name
        - level
        - builtin
        - editable
        - invitable
        - permissions
      properties:
        key:
          type: string
          description: Opaque role key.
        display_name:
          type: string
          description: Human-readable role name.
        level:
          $ref: '#/components/schemas/RoleLevel'
        builtin:
          type: boolean
          description: Whether this is a built-in role.
        editable:
          type: boolean
          description: Whether the role's permission bundle can be edited.
        invitable:
          type: boolean
          description: Whether members can be invited directly at this role.
        permissions:
          type: array
          items:
            $ref: '#/components/schemas/ScopedPermission'

    RolesResponse:
      type: object
      required:
        - roles
      properties:
        roles:
          type: array
          items:
            $ref: '#/components/schemas/RoleCatalogEntry'

    RolesResponseEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/RolesResponse'

    MemberActions:
      type: object
      required:
        - can_remove
        - can_change_role
      properties:
        can_remove:
          type: boolean
          description: Whether the caller can remove this member.
        cannot_remove_reason:
          type: string
          nullable: true
          description: Reason why the member cannot be removed, if any.
        can_change_role:
          type: boolean
          description: Whether the caller can change this member's role.

    MemberRow:
      type: object
      required:
        - account_id
        - email
        - role
        - actions
      properties:
        account_id:
          type: string
          format: uuid
          description: Account ID of the member.
        email:
          type: string
          format: email
          description: Email address of the member.
        full_name:
          type: string
          nullable: true
          description: Full name of the member.
        preferred_name:
          type: string
          nullable: true
          description: Preferred name of the member.
        role:
          $ref: '#/components/schemas/RoleRef'
        actions:
          $ref: '#/components/schemas/MemberActions'

    MemberRowEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/MemberRow'

    OrgMemberRow:
      type: object
      required:
        - account_id
        - email
      properties:
        account_id:
          type: string
          format: uuid
          description: Account ID of the member.
        email:
          type: string
          format: email
          description: Email address of the member.
        name:
          type: string
          nullable: true
          description: Full name of the member.
        org_role:
          allOf:
            - $ref: '#/components/schemas/RoleRef'
          nullable: true
          description: The member's org-level role, or null if they hold none.
        projects:
          type: array
          items:
            $ref: '#/components/schemas/MemberProject'
          description: >-
            Projects the member effectively covers in this org. An Org Admin covers
            every project; everyone else covers only projects they hold a direct
            binding on. Populated on the directory read.

    MemberProject:
      type: object
      required:
        - project_id
        - name
      properties:
        project_id:
          type: string
          format: uuid
          description: ID of the covered project.
        name:
          type: string
          description: Display name of the covered project.

    OrgMemberRowEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/OrgMemberRow'

    OrgMembersResponse:
      type: object
      required:
        - items
        - count
        - total_count
        - can_invite
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/OrgMemberRow'
        count:
          type: integer
          description: Number of members in this response page.
        total_count:
          type: integer
          description: Total number of members in the organization.
        can_invite:
          type: boolean
          description: >-
            Whether the caller may add a member to the organization (mirrors the
            POST /orgs/{organization_id}/members gate).

    OrgMembersResponseEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/OrgMembersResponse'

    AssignableAccount:
      type: object
      required:
        - account_id
        - email
      properties:
        account_id:
          type: string
          format: uuid
          description: Account ID of the assignable member.
        email:
          type: string
          format: email
          description: Email address of the member.
        name:
          type: string
          nullable: true
          description: Full name of the member.

    AssignableAccountsResponse:
      type: object
      required:
        - items
        - count
        - total_count
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/AssignableAccount'
        count:
          type: integer
          description: Number of members in this response.
        total_count:
          type: integer
          description: Total number of assignable members.

    AssignableAccountsResponseEnvelope:
      type: object
      required:
        - code
        - msg
        - data
      properties:
        code:
          type: integer
          example: 200
        msg:
          type: string
          example: Request successful
        data:
          $ref: '#/components/schemas/AssignableAccountsResponse'

    MemberAssignRequest:
      type: object
      required:
        - account_id
        - role
      properties:
        account_id:
          type: string
          format: uuid
          description: Account ID of the org member to assign.
        role:
          type: string
          description: |
            Role key to assign. Must be a role whose catalog level matches the
            endpoint (org-level key for org endpoints, project-level key for
            project endpoints), else the request is rejected with 422.

    MemberRoleUpdateRequest:
      type: object
      required:
        - role
      properties:
        role:
          type: string
          description: |
            New role key. Must match the endpoint's level (org or project), else
            the request is rejected with 422.

    OrgAuthzCapabilities:
      type: object
      required:
        - enabled
        - permissions
      properties:
        enabled:
          type: boolean
          description: Whether operator RBAC is enabled for this organization.
        permissions:
          type: array
          description: Org-level permission tokens the caller holds.
          items:
            type: string

    BillingCapabilities:
      type: object
      required:
        - enabled
      properties:
        enabled:
          type: boolean
          description: Whether billing capabilities are enabled for the caller.

    OrgCapabilities:
      type: object
      required:
        - authz
        - billing
      properties:
        authz:
          $ref: '#/components/schemas/OrgAuthzCapabilities'
        billing:
          $ref: '#/components/schemas/BillingCapabilities'
