openapi: 3.1.0
info:
  title: IW7 IP Lookup API
  version: 2.1.0
  summary: GeoIP lookup API with public and JWT-protected endpoints.
  description: |
    Public GeoIP lookup service for IW7 using MaxMind GeoLite2 City.
    The service exposes public lookup endpoints and a JWT-protected endpoint
    for authenticated integrations.
servers:
  - url: https://ip-lookup-system.iw7.com.br
    description: Production
  - url: https://ip-lookup-homol.iw7.com.br
    description: Homolog
tags:
  - name: Public GeoIP
  - name: Secure GeoIP
paths:
  /api/v1/public/geo/city:
    get:
      tags: [Public GeoIP]
      operationId: getPublicGeoCityByIp
      summary: Public GeoIP City lookup
      description: Public endpoint with a fixed limit per source IP.
      parameters:
        - name: ip
          in: query
          required: true
          description: IPv4 or IPv6 address to resolve.
          schema:
            type: string
      responses:
        '200':
          description: City resolved successfully.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GeoCityResponse'
        '400':
          description: Invalid request or invalid IP address.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiErrorResponse'
        '404':
          description: IP not found in the GeoIP database.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiErrorResponse'
        '429':
          description: Public rate limit exceeded.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiErrorResponse'
        '503':
          description: GeoIP database unavailable.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiErrorResponse'
  /api/v1/public/geo/me:
    get:
      tags: [Public GeoIP]
      operationId: getViewerGeoCity
      summary: Resolve the current client IP and GeoIP data
      description: Detects the caller IP from trusted forwarding headers and returns GeoIP data when available.
      responses:
        '200':
          description: Client IP resolved. Geo fields may be null when no public match exists.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GeoCityResponse'
        '503':
          description: GeoIP database unavailable.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiErrorResponse'
  /api/v1/secure/geo/city:
    get:
      tags: [Secure GeoIP]
      operationId: getSecureGeoCityByIp
      summary: JWT-protected GeoIP City lookup
      description: |
        JWT-protected endpoint. The request limit is resolved per authenticated user
        from MongoDB and falls back to the configured default when no user config exists.
      security:
        - bearerAuth: []
      parameters:
        - name: ip
          in: query
          required: true
          description: IPv4 or IPv6 address to resolve.
          schema:
            type: string
      responses:
        '200':
          description: City resolved successfully.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GeoCityResponse'
        '400':
          description: Invalid request or invalid IP address.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiErrorResponse'
        '401':
          description: Missing or invalid JWT.
        '403':
          description: Authenticated client disabled.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiErrorResponse'
        '429':
          description: Authenticated user rate limit exceeded.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiErrorResponse'
        '503':
          description: GeoIP database unavailable.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiErrorResponse'
  /geo/city:
    get:
      deprecated: true
      tags: [Public GeoIP]
      operationId: getLegacyGeoCityByIp
      summary: Legacy public route
      description: Backward-compatible alias for `/api/v1/public/geo/city`.
      parameters:
        - name: ip
          in: query
          required: true
          schema:
            type: string
      responses:
        '200':
          description: City resolved successfully.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GeoCityResponse'
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
  schemas:
    GeoCityResponse:
      type: object
      required: [ip]
      properties:
        ip:
          type: string
          example: 8.8.8.8
        country:
          type: [string, 'null']
          example: United States
        subdivision:
          type: [string, 'null']
          example: California
        city:
          type: [string, 'null']
          example: Mountain View
        postalCode:
          type: [string, 'null']
          example: '94043'
        latitude:
          type: [number, 'null']
          format: double
          example: 37.386
        longitude:
          type: [number, 'null']
          format: double
          example: -122.0838
        timezone:
          type: [string, 'null']
          example: America/Los_Angeles
        accuracyRadius:
          type: [integer, 'null']
          format: int32
          example: 20
    ApiErrorResponse:
      type: object
      required: [timestamp, status, error, message, path]
      properties:
        timestamp:
          type: string
          format: date-time
        status:
          type: integer
          format: int32
        error:
          type: string
        message:
          type: string
        path:
          type: string
