openapi: 3.0.3
info:
  title: TicketGate API — Integration maj-wp
  version: 1.0.0
  description: |
    API de pull pour maj-wp. TicketGate stocke les demandes validées ;
    maj-wp les réclame, crée les tickets, puis remonte l'état.

servers:
  - url: https://ticket-gate.sitecreateur.fr
    description: Production
  - url: http://localhost:3847
    description: Development

security:
  - bearerAuth: []

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: MAJ_WP_API_KEY configuré côté TicketGate

  schemas:
    IntakeItemPublic:
      type: object
      properties:
        id:
          type: string
        source_type:
          type: string
          enum: [gmail, monday_api, monday_chrome, demo]
        source_url:
          type: string
          nullable: true
        sender_name:
          type: string
          nullable: true
        sender_email:
          type: string
          nullable: true
        client_project:
          type: string
          nullable: true
        title:
          type: string
        body:
          type: string
        metadata:
          type: object
        attachments:
          type: array
          items:
            type: object
        received_at:
          type: string
          format: date-time
        ingested_at:
          type: string
          format: date-time
        decision:
          type: string
        status:
          type: string
          enum: [none, approved, claimed, created, in_progress, completed, failed]
        maj_wp_ticket_id:
          type: string
          nullable: true
        remote_status:
          type: string
          nullable: true
        remote_message:
          type: string
          nullable: true
        last_sync_at:
          type: string
          format: date-time
          nullable: true
        error:
          type: string
          nullable: true

paths:
  /api/v1/tickets/ready:
    get:
      summary: Liste les items approuvés prêts à être réclamés
      parameters:
        - name: limit
          in: query
          schema:
            type: integer
            default: 50
            maximum: 100
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  items:
                    type: array
                    items:
                      $ref: '#/components/schemas/IntakeItemPublic'
                  count:
                    type: integer
        '401':
          description: Non authentifié

  /api/v1/intake/claim-next:
    post:
      summary: Réclame atomiquement le prochain item prêt
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                consumer_id:
                  type: string
                  default: maj-wp
      responses:
        '200':
          description: Item réclamé
          content:
            application/json:
              schema:
                type: object
                properties:
                  item:
                    $ref: '#/components/schemas/IntakeItemPublic'
                  claim_token:
                    type: string
                  lease_seconds:
                    type: integer
        '404':
          description: Aucun item prêt
        '401':
          description: Non authentifié

  /api/v1/intake/{id}/claim:
    post:
      summary: Réclame un item spécifique
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                consumer_id:
                  type: string
      responses:
        '200':
          description: Réclamé
        '404':
          description: Introuvable
        '409':
          description: Déjà réclamé ou non approuvé

  /api/v1/intake/{id}/created:
    post:
      summary: Accuse réception de la création du ticket maj-wp
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [maj_wp_ticket_id]
              properties:
                claim_token:
                  type: string
                maj_wp_ticket_id:
                  type: string
                remote_status:
                  type: string
                remote_message:
                  type: string
      responses:
        '200':
          description: OK (idempotent si déjà créé)
        '404':
          description: Introuvable ou claim invalide

  /api/v1/intake/{id}/status:
    post:
      summary: Met à jour l'état remonté par maj-wp
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                maj_wp_ticket_id:
                  type: string
                status:
                  type: string
                  enum: [created, in_progress, completed, failed]
                remote_status:
                  type: string
                  description: État brut maj-wp (non normalisé)
                remote_message:
                  type: string
                error:
                  type: string
                  nullable: true
      responses:
        '200':
          description: OK
        '404':
          description: Introuvable

  /api/v1/intake/{id}:
    get:
      summary: Détail d'un item
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
        '404':
          description: Introuvable
