openapi: 3.0.3
info:
  title: Insforge Profiles API
  version: 1.0.0
  description: User profile management endpoints

paths:
  /api/profiles/me:
    get:
      summary: Get current user's profile
      description: Returns the profile data for the authenticated user
      security:
        - bearerAuth: []
      responses:
        '200':
          description: Profile retrieved successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ProfileRecord'
              example:
                id: "prof-123e4567-e89b-12d3-a456-426614174000"
                auth_id: "auth-123e4567-e89b-12d3-a456-426614174000"
                name: "John Doe"
                avatar_url: "https://example.com/avatar.jpg"
                bio: "Software developer passionate about open source"
                metadata:
                  interests: ["coding", "music", "travel"]
                  location: "San Francisco, CA"
                created_at: "2024-01-01T00:00:00Z"
                updated_at: "2024-01-21T10:30:00Z"
        '404':
          description: Profile not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error: "PROFILE_NOT_FOUND"
                message: "Profile not found for the current user"
                statusCode: 404
                nextAction: "Create a profile first"
    
    patch:
      summary: Update current user's profile
      description: Updates the profile data for the authenticated user
      security:
        - bearerAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                  example: Jane Doe
                avatar_url:
                  type: string
                  example: https://example.com/avatar.jpg
                bio:
                  type: string
                  example: Software developer passionate about open source
                metadata:
                  type: object
                  description: Additional flexible data
                  example:
                    interests: ["coding", "music", "travel"]
                    location: "San Francisco, CA"
      responses:
        '200':
          description: Profile updated successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ProfileRecord'
              example:
                id: "prof-123e4567-e89b-12d3-a456-426614174000"
                auth_id: "auth-123e4567-e89b-12d3-a456-426614174000"
                name: "Jane Doe"
                avatar_url: "https://example.com/new-avatar.jpg"
                bio: "Full-stack developer and tech enthusiast"
                metadata:
                  interests: ["coding", "AI", "blockchain"]
                  location: "New York, NY"
                created_at: "2024-01-01T00:00:00Z"
                updated_at: "2024-01-21T11:00:00Z"
        '404':
          description: Profile not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error: "PROFILE_NOT_FOUND"
                message: "Profile not found for the current user"
                statusCode: 404
                nextAction: "Create a profile first"
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error: "VALIDATION_ERROR"
                message: "Invalid URL format for avatar_url"
                statusCode: 400
                nextAction: "Provide a valid URL for the avatar"

  /api/profiles/search:
    get:
      summary: Search profiles
      description: Search for profiles by name, bio, or username
      parameters:
        - in: query
          name: q
          required: true
          schema:
            type: string
          description: Search query string
          example: developer
        - in: query
          name: limit
          schema:
            type: integer
            default: 20
          description: Number of results to return
        - in: query
          name: offset
          schema:
            type: integer
            default: 0
          description: Number of results to skip
      responses:
        '200':
          description: Search results
          headers:
            X-Total-Count:
              schema:
                type: integer
              description: Total number of matching profiles
            X-Page:
              schema:
                type: integer
              description: Current page number
            X-Page-Size:
              schema:
                type: integer
              description: Number of items per page
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/PublicProfile'
              example:
                - id: "prof-123"
                  auth_id: "auth-123"
                  name: "John Developer"
                  avatar_url: "https://example.com/john.jpg"
                  bio: "Senior developer with 10 years experience"
                  created_at: "2024-01-01T00:00:00Z"
                - id: "prof-456"
                  auth_id: "auth-456"
                  name: "Jane Developer"
                  avatar_url: null
                  bio: "Full-stack developer specializing in React"
                  created_at: "2024-01-02T00:00:00Z"
        '400':
          description: Missing search query
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error: "MISSING_QUERY"
                message: "Search query parameter 'q' is required"
                statusCode: 400
                nextAction: "Provide a search query"

  /api/profiles/{userId}:
    get:
      summary: Get public profile by user ID
      description: Returns public profile information for a specific user
      parameters:
        - in: path
          name: userId
          required: true
          schema:
            type: string
            format: uuid
          description: The auth ID of the user
      responses:
        '200':
          description: Public profile retrieved successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PublicProfile'
              example:
                id: "prof-123e4567-e89b-12d3-a456-426614174000"
                auth_id: "auth-123e4567-e89b-12d3-a456-426614174000"
                name: "John Doe"
                avatar_url: "https://example.com/avatar.jpg"
                bio: "Software developer passionate about open source"
                created_at: "2024-01-01T00:00:00Z"
        '404':
          description: User or profile not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error: "USER_NOT_FOUND"
                message: "User with ID 'auth-999' not found"
                statusCode: 404
                nextAction: "Check the user ID and try again"

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
  
  schemas:
    ProfileRecord:
      type: object
      properties:
        id:
          type: string
          format: uuid
        auth_id:
          type: string
          format: uuid
          description: Foreign key to auth table
        name:
          type: string
        avatar_url:
          type: string
          nullable: true
        bio:
          type: string
          nullable: true
        metadata:
          type: object
          description: JSONB field for flexible data
        created_at:
          type: string
          format: date-time
        updated_at:
          type: string
          format: date-time
    
    PublicProfile:
      type: object
      properties:
        id:
          type: string
          format: uuid
        auth_id:
          type: string
          format: uuid
          description: ID of the associated auth record (for search results)
        name:
          type: string
        avatar_url:
          type: string
          nullable: true
        bio:
          type: string
          nullable: true
        created_at:
          type: string
          format: date-time
      description: Public profile data (excludes sensitive metadata)
    
    ErrorResponse:
      type: object
      required:
        - error
        - message
        - statusCode
      properties:
        error:
          type: string
          description: Error code for programmatic handling
        message:
          type: string
          description: Human-readable error message
        statusCode:
          type: integer
          description: HTTP status code
        nextAction:
          type: string
          description: Suggested action to resolve the error