openapi: 3.1.0
info:
  title: PatrolTech AI API
  description: API for AI agent integrations with PatrolTech platform.
  version: 0.1.0
  contact:
    name: PatrolTech
    url: https://patroltech.online

servers:
  - url: https://api.patroltech.online
    description: Production

paths:
  /api/ai/leads:
    post:
      operationId: createLead
      summary: Submit a lead captured by an AI agent
      description: |
        Receives lead information collected by AI agents (chatbots, voice assistants, etc.)
        and forwards it to the CRM. At least one contact method (email or phone_whatsapp)
        is required. The full payload is stored as an event so no information is lost.
      tags:
        - AI Leads
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateLeadRequest'
            example:
              name: "Juan Garcia"
              contact:
                phone_whatsapp: "+34612345678"
                email: "juan@example.com"
              requester_agent:
                agent_name: "WebChatBot"
                agent_type: "web"
                agent_version: "1.0.0"
                provider: "patrol-ai"
              conversation_context:
                language: "es"
                summary: "Looking for security patrol service for industrial zone"
                user_intent: "hire_service"
                key_points:
                  - "Needs 24/7 coverage"
                  - "Industrial zone"
                constraints:
                  - "Budget limited"
              optional:
                company_name: "Acme Corp"
                what_they_need: "Security patrols for warehouse"
              consent:
                confirmed_by_user: true
                consent_text_shown: "I agree to share my data with PatrolTech"
                confirmed_at_iso: "2025-01-15T10:00:00Z"
              metadata:
                source: "website-chatbot"
                page_url: "https://patroltech.online/chat"
                conversation_id: "conv-abc-123"
      responses:
        '201':
          description: Lead created successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SuccessResponse'
              example:
                success: true
                message: "Lead created successfully"
                data:
                  received: true
        '400':
          description: Validation error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ValidationErrorResponse'
              example:
                success: false
                error:
                  code: "VALIDATION_ERROR"
                  message: "Request validation failed"
                  details:
                    body:
                      name: "Name is required"
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                success: false
                message: "Failed to process lead"

components:
  schemas:
    CreateLeadRequest:
      type: object
      required:
        - name
        - contact
        - consent
      properties:
        name:
          type: string
          minLength: 1
          description: Full name of the lead
        contact:
          $ref: '#/components/schemas/LeadContact'
        requester_agent:
          $ref: '#/components/schemas/LeadRequesterAgent'
        conversation_context:
          $ref: '#/components/schemas/LeadConversationContext'
        optional:
          $ref: '#/components/schemas/LeadOptional'
        consent:
          $ref: '#/components/schemas/LeadConsent'
        metadata:
          $ref: '#/components/schemas/LeadMetadata'

    LeadContact:
      type: object
      description: At least one contact method (email or phone_whatsapp) is required.
      properties:
        phone_whatsapp:
          type: string
          description: WhatsApp phone number (with country code)
          examples:
            - "+34612345678"
        email:
          type: string
          format: email
          description: Email address

    LeadRequesterAgent:
      type: object
      description: Information about the AI agent that captured the lead.
      properties:
        agent_name:
          type: string
          description: Name of the agent
        agent_type:
          type: string
          description: "Type of agent (e.g. web, voice, whatsapp)"
        agent_version:
          type: string
          description: Version of the agent
        provider:
          type: string
          description: Provider or platform running the agent

    LeadConversationContext:
      type: object
      description: Context from the conversation where the lead was captured.
      properties:
        language:
          type: string
          description: "Language of the conversation (ISO 639-1, e.g. es, en, fr)"
        summary:
          type: string
          description: Brief summary of the conversation
        user_intent:
          type: string
          description: Detected intent of the user
        key_points:
          type: array
          items:
            type: string
          description: Key points extracted from the conversation
        constraints:
          type: array
          items:
            type: string
          description: Constraints or limitations mentioned by the user

    LeadOptional:
      type: object
      description: Additional optional information about the lead.
      properties:
        company_name:
          type: string
          description: Company or organization name
        what_they_need:
          type: string
          description: Description of what the lead needs

    LeadConsent:
      type: object
      required:
        - confirmed_by_user
      description: User consent information. Required for GDPR compliance.
      properties:
        confirmed_by_user:
          type: boolean
          description: Whether the user explicitly confirmed consent
        consent_text_shown:
          type: string
          description: The exact consent text shown to the user
        confirmed_at_iso:
          type: string
          format: date-time
          description: ISO 8601 timestamp of when consent was confirmed

    LeadMetadata:
      type: object
      description: Technical metadata about the lead capture.
      properties:
        source:
          type: string
          description: "Source identifier (e.g. website-chatbot, whatsapp-bot)"
        page_url:
          type: string
          format: uri
          description: URL of the page where the conversation happened
        conversation_id:
          type: string
          description: Unique identifier of the conversation

    SuccessResponse:
      type: object
      properties:
        success:
          type: boolean
          const: true
        message:
          type: string
        data:
          type: object

    ErrorResponse:
      type: object
      properties:
        success:
          type: boolean
          const: false
        message:
          type: string

    ValidationErrorResponse:
      type: object
      properties:
        success:
          type: boolean
          const: false
        error:
          type: object
          properties:
            code:
              type: string
            message:
              type: string
            details:
              type: object
