openapi: 3.0.3
info:
  title: SPC-Last API
  description: |
    API Selenium/GDS pour l'automatisation des operations sur les portails GDS (Amadeus Selling Platform Connect, Galileo).

    Ce service recoit des commandes via Messager et execute des operations Selenium
    (lecture PNR, emission, annulation, collectes BSP/BMP/RHC, approvisionnements).
  version: "2.0"
  contact:
    name: CETERIS Connect
servers:
  - url: http://localhost:5000
    description: Serveur local SPC-Last

security:
  - bearerAuth: []

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Token unifie SPC (SPC_UNIFIED_TOKEN)

  schemas:
    ErrorResponse:
      type: object
      properties:
        status:
          type: string
          enum: [error]
        message:
          type: string

    GdsType:
      type: string
      enum: [amadeus, galileo]
      description: Type de GDS. Seul amadeus est actuellement supporte.

    ExecutionResult:
      type: object
      properties:
        message_id:
          type: integer
        execution_uuid:
          type: string
        status:
          type: string
          enum: [success, failed]
        retryable:
          type: boolean
        result:
          type: object
          nullable: true
        error:
          type: string
          nullable: true
        error_code:
          type: string
          nullable: true
        execution_time_ms:
          type: integer
        timestamp:
          type: string
          format: date-time

paths:
  /api/v1/health:
    get:
      summary: Health check basique
      tags: [Health]
      security: []
      responses:
        "200":
          description: Service operationnel
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: ok
                  service:
                    type: string
                    example: SPC-Last API
                  version:
                    type: string
                    example: "2.0"
                  selenium_ready:
                    type: boolean
                  timestamp:
                    type: string
                    format: date-time

  /api/v1/health/detailed:
    get:
      summary: Health check detaille avec etat des setups et GDS
      tags: [Health]
      responses:
        "200":
          description: Etat detaille du service
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                  statistics:
                    type: object
                    properties:
                      total_active_setups:
                        type: integer
                      healthy:
                        type: integer
                      degraded:
                        type: integer
                      failed:
                        type: integer
                  setups:
                    type: object
                    additionalProperties:
                      type: object
                      properties:
                        status:
                          type: string
                          enum: [healthy, degraded, failed]
                        failures_count:
                          type: integer
                        circuit_breaker_active:
                          type: boolean
                  gds_connectors:
                    type: array
                    items:
                      type: object
                      properties:
                        gds_type:
                          type: string
                        status:
                          type: string
                        capabilities_count:
                          type: integer

  /api/v1/execute:
    post:
      summary: Executer une operation PNR (legacy)
      description: Endpoint principal pour recevoir les operations de Messager (mode sync ou async)
      tags: [Operations]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [message_id, payload, callback]
              properties:
                message_id:
                  type: integer
                payload:
                  type: object
                  required: [operation_type]
                  properties:
                    operation_type:
                      type: string
                      example: load_pnr
                    execution_uuid:
                      type: string
                      format: uuid
                    record_locator:
                      type: string
                      example: ABC123
                    command_user:
                      type: string
                    gds_type:
                      $ref: "#/components/schemas/GdsType"
                callback:
                  type: object
                  required: [url, token]
                  properties:
                    url:
                      type: string
                      format: uri
                    token:
                      type: string
                attempt:
                  type: integer
                  default: 1
      responses:
        "200":
          description: Resultat (mode sync)
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    enum: [completed]
                  result:
                    $ref: "#/components/schemas/ExecutionResult"
        "202":
          description: Accepte (mode async)
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    enum: [accepted]
                  message_id:
                    type: integer
        "501":
          description: GDS non implemente
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    enum: [error]
                  message:
                    type: string
                  error_code:
                    type: string
                    enum: [GDS_NOT_IMPLEMENTED]

  /api/v1/selenium/execute:
    post:
      summary: Endpoint unifie pour toutes les operations Selenium
      description: |
        Point d'entree unique pour les operations Selenium.
        Le type d'operation est determine par le domain dans le payload.
        Les requetes sont mises en queue par setup pour eviter les conflits.
      tags: [Operations]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [message_id, domain, payload]
              properties:
                message_id:
                  type: integer
                domain:
                  type: string
                  enum: [spc_selling, spc_appro, spc_bmp, spc_bsp, spc_rhc]
                payload:
                  type: object
                  properties:
                    type:
                      type: string
                      description: Type d'operation (read, load_pnr, issue_pnr, void_ticket, etc.)
                    gds_type:
                      $ref: "#/components/schemas/GdsType"
                metadata:
                  type: object
                callback_url:
                  type: string
                  format: uri
                callback_token:
                  type: string
      responses:
        "202":
          description: Requete mise en queue
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    enum: [queued]
                  setup:
                    type: string
                  queue_position:
                    type: integer
        "501":
          description: GDS non implemente
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"

  /api/v1/selenium/queue/status:
    get:
      summary: Etat des queues Selenium par setup
      tags: [Operations]
      responses:
        "200":
          description: Etat des queues
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                  queues:
                    type: object
                    additionalProperties:
                      type: object
                      properties:
                        queue_size:
                          type: integer
                        worker_alive:
                          type: boolean

  /api/v1/bmp/execute:
    post:
      summary: Collecte BMP daily (asynchrone)
      description: Declenche par le scheduler Laravel de Messager. Telecharge les fichiers CSV BMP.
      tags: [Collectes]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [callback_url]
              properties:
                callback_url:
                  type: string
                  format: uri
                callback_token:
                  type: string
                date_from:
                  type: string
                  format: date
                date_to:
                  type: string
                  format: date
      responses:
        "202":
          description: Collecte demarree en arriere-plan
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    enum: [accepted]
                  job_id:
                    type: string

  /api/v1/rhc/execute:
    post:
      summary: Collecte RHC IATA (asynchrone)
      description: Collecte les donnees RHC depuis le portail IATA.
      tags: [Collectes]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [callback_url]
              properties:
                callback_url:
                  type: string
                  format: uri
                callback_token:
                  type: string
      responses:
        "202":
          description: Collecte demarree en arriere-plan
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    enum: [accepted]
                  job_id:
                    type: string

  /api/v1/bsp/execute:
    post:
      summary: Collecte BSP IATA bi-mensuelle (asynchrone)
      description: Telecharge le fichier ZIP BSP depuis le portail IATA (declenchement 3 et 18 du mois).
      tags: [Collectes]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [callback_url]
              properties:
                callback_url:
                  type: string
                  format: uri
                callback_token:
                  type: string
                period_code:
                  type: string
                  description: Code periode BSP (ex 250902). Si vide ou "auto", calcul automatique.
                  example: "250902"
      responses:
        "202":
          description: Collecte demarree en arriere-plan
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    enum: [accepted]
                  job_id:
                    type: string

  /api/v1/appro/execute:
    post:
      summary: Approvisionnement BMP via Selenium
      description: Execute un approvisionnement BMP et envoie le callback a Estair.
      tags: [Operations]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                message_id:
                  type: integer
                payload:
                  type: object
                  properties:
                    mouvement_id:
                      type: integer
                    mouvement_uuid:
                      type: string
                    amount:
                      type: number
                    direction:
                      type: string
                    account_name:
                      type: string
                callback_url:
                  type: string
                  format: uri
                callback_token:
                  type: string
      responses:
        "202":
          description: Approvisionnement en cours de traitement
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    enum: [processing]
                  message:
                    type: string
