openapi: 3.0.3
info:
  title: LapsHub Live API
  version: 1.0.0-preview
  description: |
    **Preview specification — not deployed.** Endpoints below describe the intended v1
    control plane. Do not call these URLs in production; request access via lapshub.com.

    Motorsport live video stages (IVS Real-Time tokens) + session lifecycle.
    Telemetry ingest/subscribe is documented in the companion AsyncAPI file.
  contact:
    name: LapsHub
    url: https://www.lapshub.com
  license:
    name: Proprietary

servers:
  - url: https://api.lapshub.com
    description: Planned production host (not live — Preview)

tags:
  - name: Sessions
  - name: Stages
  - name: Telemetry
  - name: Usage

security:
  - ApiKeyAuth: []

paths:
  /v1/cars/{carId}/sessions:
    post:
      tags: [Sessions]
      summary: Open a live session for a car
      operationId: createSession
      parameters:
        - $ref: '#/components/parameters/carId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateSessionRequest'
      responses:
        '201':
          description: Session created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Session'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '429':
          $ref: '#/components/responses/RateLimited'

  /v1/sessions/{sessionId}:
    get:
      tags: [Sessions]
      summary: Get session
      operationId: getSession
      parameters:
        - $ref: '#/components/parameters/sessionId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Session'
        '404':
          $ref: '#/components/responses/NotFound'

  /v1/sessions/{sessionId}/stages:
    post:
      tags: [Stages]
      summary: Create IVS Real-Time stage and publisher token
      operationId: createStage
      parameters:
        - $ref: '#/components/parameters/sessionId'
      requestBody:
        required: false
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateStageRequest'
      responses:
        '201':
          description: Stage created; publish with IVS Broadcast SDK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/StageWithPublisherToken'
        '409':
          description: Session already has an active stage

  /v1/sessions/{sessionId}/stages/{stageId}/viewer-tokens:
    post:
      tags: [Stages]
      summary: Mint a viewer participant token
      description: |
        Billable while the viewer remains connected. Clients should disconnect when
        not watching (idle timeout enforced server-side when runtime ships).
      operationId: createViewerToken
      parameters:
        - $ref: '#/components/parameters/sessionId'
        - $ref: '#/components/parameters/stageId'
      responses:
        '201':
          description: Viewer token minted
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ViewerToken'
        '404':
          $ref: '#/components/responses/NotFound'

  /v1/sessions/{sessionId}/stages/{stageId}:
    delete:
      tags: [Stages]
      summary: Stop stage and invalidate tokens
      operationId: deleteStage
      parameters:
        - $ref: '#/components/parameters/sessionId'
        - $ref: '#/components/parameters/stageId'
      responses:
        '204':
          description: Stage stopped
        '404':
          $ref: '#/components/responses/NotFound'

  /v1/sessions/{sessionId}/telemetry-credentials:
    post:
      tags: [Telemetry]
      summary: Mint short-lived MQTT credentials for ingest/subscribe
      description: |
        Returns connection endpoint and topic ACLs. Message schema and topics are in
        AsyncAPI (`asyncapi-telemetry-v1.yaml`).
      operationId: createTelemetryCredentials
      parameters:
        - $ref: '#/components/parameters/sessionId'
      requestBody:
        required: false
        content:
          application/json:
            schema:
              type: object
              properties:
                role:
                  type: string
                  enum: [publish, subscribe, both]
                  default: both
                ttlSeconds:
                  type: integer
                  minimum: 60
                  maximum: 86400
                  default: 3600
      responses:
        '201':
          description: Temporary credentials
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TelemetryCredentials'

  /v1/sessions/{sessionId}/telemetry:
    post:
      tags: [Telemetry]
      summary: HTTPS batch ingest (secondary path)
      description: Escape hatch when MQTT is unavailable. Prefer MQTT for high-Hz.
      operationId: postTelemetryBatch
      parameters:
        - $ref: '#/components/parameters/sessionId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TelemetryBatch'
      responses:
        '202':
          description: Accepted
        '413':
          description: Batch too large

  /v1/usage:
    get:
      tags: [Usage]
      summary: Org usage summary for the current billing period
      operationId: getUsage
      parameters:
        - name: from
          in: query
          schema:
            type: string
            format: date-time
        - name: to
          in: query
          schema:
            type: string
            format: date-time
      responses:
        '200':
          description: Usage meters
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UsageSummary'

components:
  securitySchemes:
    ApiKeyAuth:
      type: http
      scheme: bearer
      bearerFormat: lh_live_
      description: Org API key issued after access approval (not available in Preview).

  parameters:
    carId:
      name: carId
      in: path
      required: true
      schema:
        type: string
    sessionId:
      name: sessionId
      in: path
      required: true
      schema:
        type: string
    stageId:
      name: stageId
      in: path
      required: true
      schema:
        type: string

  responses:
    Unauthorized:
      description: Missing or invalid API key
    NotFound:
      description: Resource not found or not in this org
    RateLimited:
      description: Org quota exceeded

  schemas:
    CreateSessionRequest:
      type: object
      properties:
        externalId:
          type: string
          description: Integrator correlation id
        trackId:
          type: string
        metadata:
          type: object
          additionalProperties: true

    Session:
      type: object
      required: [id, carId, status, createdAt]
      properties:
        id:
          type: string
        carId:
          type: string
        status:
          type: string
          enum: [open, closed]
        trackId:
          type: string
        createdAt:
          type: string
          format: date-time
        closedAt:
          type: string
          format: date-time
          nullable: true

    CreateStageRequest:
      type: object
      properties:
        maxDurationSeconds:
          type: integer
          default: 7200
          maximum: 14400
        video:
          type: object
          properties:
            maxResolution:
              type: string
              enum: [720p, 1080p]
              default: 720p

    StageWithPublisherToken:
      type: object
      required: [stageId, stageArn, publishToken, expiresAt]
      properties:
        stageId:
          type: string
        stageArn:
          type: string
        publishToken:
          type: string
        expiresAt:
          type: string
          format: date-time
        participantId:
          type: string

    ViewerToken:
      type: object
      required: [token, expiresAt]
      properties:
        token:
          type: string
        expiresAt:
          type: string
          format: date-time
        participantId:
          type: string

    TelemetryCredentials:
      type: object
      required: [endpoint, clientId, topicPublish, topicSubscribe, expiresAt]
      properties:
        endpoint:
          type: string
          description: MQTT broker host
        clientId:
          type: string
        topicPublish:
          type: string
        topicSubscribe:
          type: string
        username:
          type: string
        password:
          type: string
        expiresAt:
          type: string
          format: date-time

    TelemetryPoint:
      type: object
      required: [ts]
      properties:
        ts:
          type: string
          format: date-time
        lat:
          type: number
          format: double
        lon:
          type: number
          format: double
        speedMps:
          type: number
          format: float
        headingDeg:
          type: number
          format: float
        channels:
          type: object
          additionalProperties:
            type: number
            description: Named scalars (e.g. rpm, throttle)

    TelemetryBatch:
      type: object
      required: [points]
      properties:
        points:
          type: array
          minItems: 1
          maxItems: 500
          items:
            $ref: '#/components/schemas/TelemetryPoint'

    UsageSummary:
      type: object
      properties:
        orgId:
          type: string
        periodStart:
          type: string
          format: date-time
        periodEnd:
          type: string
          format: date-time
        meters:
          type: array
          items:
            type: object
            required: [meter, quantity, unit]
            properties:
              meter:
                type: string
                enum:
                  - video.publisher
                  - video.subscriber
                  - telemetry.message
                  - telemetry.bytes
              quantity:
                type: number
              unit:
                type: string
                enum: [participant-second, count, bytes]
