# 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.

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'

  /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'

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

    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'
