openapi: 3.1.0
info:
  title: STEADYWRK Dispatch API
  version: 0.1.0
  summary: Agentic Control Plane for Critical Field Operations
  description: |
    Public API for the STEADYWRK Dispatch Engine. Three endpoints:
    - `POST /api/dispatch/v1/quote` — instant quote from trade + location + urgency
    - `POST /api/dispatch/v1/order` — create a tracked work order with auto-matched contractor
    - `GET  /api/dispatch/analytics/evals` — rolling operational evals (public read-only)

    Auth for mutating endpoints: `x-api-key` header. Issue per tenant.
    Rate limits: 20/min for quote, 10/min for order, per IP.
  contact:
    name: STEADYWRK
    email: hello@steadywrk.app
    url: https://steadywrk.app/developers
  license:
    name: MIT
servers:
  - url: https://steadywrk.app
    description: Production
paths:
  /api/dispatch/v1/quote:
    post:
      operationId: dispatchQuote
      summary: Instant dispatch quote
      tags: [Dispatch]
      security:
        - ApiKeyAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/QuoteRequest'
      responses:
        '200':
          description: Quote generated (available may be false if no contractor matched)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/QuoteResponse'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '422':
          $ref: '#/components/responses/ValidationError'
        '429':
          $ref: '#/components/responses/RateLimited'

  /api/dispatch/v1/order:
    post:
      operationId: dispatchOrder
      summary: Create a tracked work order
      tags: [Dispatch]
      security:
        - ApiKeyAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/OrderRequest'
      responses:
        '201':
          description: Work order created and contractor assigned
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OrderResponse'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '422':
          description: No contractor available or validation failed
        '429':
          $ref: '#/components/responses/RateLimited'

  /api/dispatch/analytics/evals:
    get:
      operationId: dispatchEvals
      summary: Rolling operational evals
      tags: [Evals]
      parameters:
        - name: period
          in: query
          required: false
          schema:
            type: string
            enum: [rolling_7d, rolling_30d, rolling_90d]
            default: rolling_30d
      responses:
        '200':
          description: Eval snapshot for the requested window
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EvalsResponse'

components:
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: x-api-key

  responses:
    Unauthorized:
      description: Missing or invalid API key
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    ValidationError:
      description: Input failed Zod validation
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    RateLimited:
      description: Rate limit exceeded
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'

  schemas:
    Urgency:
      type: string
      enum: [emergency, urgent, routine, scheduled]

    QuoteLocation:
      type: object
      required: [state]
      properties:
        state:
          type: string
          minLength: 2
          maxLength: 2
        zip:
          type: string
          maxLength: 10
        city:
          type: string
          maxLength: 255

    OrderLocation:
      allOf:
        - $ref: '#/components/schemas/QuoteLocation'
        - type: object
          properties:
            address:
              type: string
              maxLength: 1000
            name:
              type: string
              maxLength: 500

    QuoteRequest:
      type: object
      required: [trade, location]
      properties:
        trade:
          type: string
          minLength: 1
        location:
          $ref: '#/components/schemas/QuoteLocation'
        urgency:
          $ref: '#/components/schemas/Urgency'
        nte_cents:
          type: integer
          minimum: 1
        description:
          type: string
          maxLength: 2000

    QuoteResponse:
      type: object
      required: [available]
      properties:
        available:
          type: boolean
        quote:
          type: object
          properties:
            trade:
              type: string
            location:
              $ref: '#/components/schemas/QuoteLocation'
            urgency:
              $ref: '#/components/schemas/Urgency'
            estimated_eta:
              type: string
            best_match:
              type: object
              properties:
                estimated_cost_cents:
                  type: integer
                estimated_cost_usd:
                  type: string
                estimated_response_minutes:
                  type: integer
                match_score:
                  type: number
            alternates_count:
              type: integer
            generated_at:
              type: string
              format: date-time
            valid_for_minutes:
              type: integer

    OrderRequest:
      type: object
      required: [trade, location, description]
      properties:
        trade:
          type: string
        location:
          $ref: '#/components/schemas/OrderLocation'
        urgency:
          $ref: '#/components/schemas/Urgency'
        nte_cents:
          type: integer
          minimum: 1
        description:
          type: string
          minLength: 1
          maxLength: 5000
        callback_url:
          type: string
          format: uri
        reference_id:
          type: string
          maxLength: 255

    OrderResponse:
      type: object
      properties:
        created:
          type: boolean
        order:
          type: object
          properties:
            id:
              type: string
            status:
              type: string
            trade:
              type: string
            location:
              $ref: '#/components/schemas/OrderLocation'
            urgency:
              $ref: '#/components/schemas/Urgency'
            quoted_cost_cents:
              type: integer
            quoted_cost_usd:
              type: string
            assigned_contractor_ref:
              type: string
              description: Masked reference (SW-xxxxxxxx), never raw contractor identity
            estimated_response_minutes:
              type: integer
            created_at:
              type: string
              format: date-time
        tracking:
          type: object
          properties:
            status_url:
              type: string
              format: uri

    EvalsResponse:
      type: object
      required: [schema_version, period, goal_fulfillment, operational, trust_signals]
      properties:
        schema_version:
          type: string
        period:
          type: string
          enum: [rolling_7d, rolling_30d, rolling_90d]
        goal_fulfillment:
          type: object
          properties:
            completion_rate:
              type: number
            nte_accuracy:
              type: number
            redispatch_rate:
              type: number
            human_override_rate:
              type: number
        operational:
          type: object
          properties:
            avg_dispatch_latency_ms:
              type: integer
            p95_dispatch_latency_ms:
              type: integer
            jobs_dispatched_30d:
              type: integer
            active_providers:
              type: integer
            active_states:
              type: integer
        trust_signals:
          type: object
          properties:
            opsec_audit_last_passed:
              type: string
              format: date
            canonical_metrics_drift:
              type: boolean
            build_hash:
              type: string

    Error:
      type: object
      properties:
        error:
          type: string
        details:
          type: object
          additionalProperties: true
