openapi: 3.1.0
info:
  title: InsureLink Agent Orchestrator API
  version: 1.0.0
  description: >
    API for autonomous AI agents to discover capabilities, query reputation,
    manage SLA agreements, and execute x402-powered actions on Base (EIP-155:8453).
  contact:
    email: support@insurelink.xyz
    url: https://insurelink.xyz
  license:
    name: MIT
    url: https://opensource.org/licenses/MIT

servers:
  - url: https://ljoaoopkawioavzxjdkn.supabase.co/functions/v1/agent-orchestrator
    description: Production

paths:
  /discover:
    get:
      operationId: discoverCapabilities
      summary: Discover platform capabilities
      description: Returns the full capability manifest including supported actions, tokens, pricing, and framework compatibility. Start here.
      responses:
        "200":
          description: Capability manifest
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/DiscoverResponse"

  /reputation:
    get:
      operationId: getReputation
      summary: Get agent reputation
      description: Returns reputation score, tier, stats, and flags for a given wallet address.
      parameters:
        - name: wallet
          in: query
          required: true
          schema:
            type: string
          description: Wallet address (0x...)
      responses:
        "200":
          description: Reputation data
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ReputationResponse"

  /leaderboard:
    get:
      operationId: getLeaderboard
      summary: Top 25 agents by reputation
      description: Returns the top 25 most reliable agents ranked by reputation score.
      responses:
        "200":
          description: Leaderboard data
          content:
            application/json:
              schema:
                type: object
                properties:
                  agents:
                    type: array
                    items:
                      $ref: "#/components/schemas/LeaderboardEntry"

  /sla-history:
    get:
      operationId: getSlaHistory
      summary: SLA history for a wallet
      description: Returns the full SLA history for a given wallet address.
      parameters:
        - name: wallet
          in: query
          required: true
          schema:
            type: string
          description: Wallet address (0x...)
      responses:
        "200":
          description: SLA history
          content:
            application/json:
              schema:
                type: object
                properties:
                  slas:
                    type: array
                    items:
                      $ref: "#/components/schemas/SlaRecord"

  /activity:
    get:
      operationId: getActivity
      summary: Recent platform transactions
      description: Returns recent platform activity with optional limit.
      parameters:
        - name: limit
          in: query
          required: false
          schema:
            type: integer
            default: 50
          description: Number of recent transactions to return
      responses:
        "200":
          description: Activity feed
          content:
            application/json:
              schema:
                type: object
                properties:
                  transactions:
                    type: array
                    items:
                      $ref: "#/components/schemas/Transaction"

  /mint-sla:
    post:
      operationId: mintSla
      summary: Create a new SLA-NFT
      description: Mints a new ERC-721 SLA agreement. Requires x402 payment header ($0.01).
      parameters:
        - name: x-payment
          in: header
          required: true
          schema:
            type: string
          description: x402 payment token
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/MintSlaRequest"
      responses:
        "200":
          description: SLA minted successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ActionResponse"
        "402":
          description: Payment required

  /micro-reset:
    post:
      operationId: microReset
      summary: Micro-reset insurance window
      description: Resets the insurance window for an SLA. Requires x402 payment header ($0.001).
      parameters:
        - name: x-payment
          in: header
          required: true
          schema:
            type: string
          description: x402 payment token
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [tokenId]
              properties:
                tokenId:
                  type: integer
                  description: SLA token ID
      responses:
        "200":
          description: Insurance window reset
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ActionResponse"
        "402":
          description: Payment required

  /renew:
    post:
      operationId: renewSla
      summary: Renew an existing SLA
      description: Renews an SLA agreement. Requires x402 payment header ($0.005).
      parameters:
        - name: x-payment
          in: header
          required: true
          schema:
            type: string
          description: x402 payment token
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [tokenId]
              properties:
                tokenId:
                  type: integer
                  description: SLA token ID
                duration:
                  type: integer
                  enum: [5, 7, 10]
                  description: Renewal duration in years
      responses:
        "200":
          description: SLA renewed
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ActionResponse"
        "402":
          description: Payment required

  /early-exit:
    post:
      operationId: earlyExit
      summary: Early exit from an SLA
      description: Exits an SLA early with applicable protection adjustment. Requires x402 payment header ($0.005).
      parameters:
        - name: x-payment
          in: header
          required: true
          schema:
            type: string
          description: x402 payment token
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [tokenId]
              properties:
                tokenId:
                  type: integer
                  description: SLA token ID
      responses:
        "200":
          description: Early exit processed
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ActionResponse"
        "402":
          description: Payment required

  /wrap:
    post:
      operationId: wrapUsdc
      summary: Wrap USDC to iUSDC
      description: Wraps USDC into iUSDC (InsureLink wrapped token). Requires x402 payment header ($0.001).
      parameters:
        - name: x-payment
          in: header
          required: true
          schema:
            type: string
          description: x402 payment token
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [amount]
              properties:
                amount:
                  type: string
                  description: Amount of USDC to wrap (in base units)
      responses:
        "200":
          description: USDC wrapped successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ActionResponse"
        "402":
          description: Payment required

components:
  schemas:
    DiscoverResponse:
      type: object
      properties:
        name:
          type: string
        chain:
          type: string
        contract:
          type: string
        capabilities:
          type: array
          items:
            type: object
            properties:
              action:
                type: string
              cost:
                type: string
              method:
                type: string
        supportedTokens:
          type: array
          items:
            type: string
        frameworks:
          type: array
          items:
            type: string

    ReputationResponse:
      type: object
      properties:
        wallet:
          type: string
        score:
          type: integer
        tier:
          type: string
        completedSlas:
          type: integer
        activeSlas:
          type: integer
        earlyExits:
          type: integer
        hasInsurance:
          type: boolean
        flags:
          type: array
          items:
            type: string

    LeaderboardEntry:
      type: object
      properties:
        rank:
          type: integer
        wallet:
          type: string
        score:
          type: integer
        tier:
          type: string
        completedSlas:
          type: integer

    SlaRecord:
      type: object
      properties:
        tokenId:
          type: integer
        counterparty:
          type: string
        duration:
          type: integer
        bondAmount:
          type: number
        status:
          type: string
        startDate:
          type: string
          format: date-time
        expiryDate:
          type: string
          format: date-time

    Transaction:
      type: object
      properties:
        id:
          type: string
        txType:
          type: string
        amount:
          type: number
        wallet:
          type: string
        timestamp:
          type: string
          format: date-time

    MintSlaRequest:
      type: object
      required: [counterparty, duration, bondAmount]
      properties:
        counterparty:
          type: string
          description: Wallet address of the counterparty
        duration:
          type: integer
          enum: [5, 7, 10]
          description: Agreement duration in years
        bondAmount:
          type: string
          description: Bond amount in iUSDC base units
        coverageLevel:
          type: integer
          default: 0
          description: Optional insurance coverage level (0 = none)

    ActionResponse:
      type: object
      properties:
        success:
          type: boolean
        txHash:
          type: string
        message:
          type: string
