openapi: 3.0.3
info:
  title: MasProvision API
  version: 3.0.0
  description: >
    ### Release Notes

    * **3.0.0**
      - Add Provision TV Agile API

    ## Release and compatibility

    - Deprecations for current versions whenever a new version is announced is 1 months.
    - After 1 months, the deprecated version will no longer be supported but will remain accessible for at least 3 months.
    - Our APIs will allow requests only from two versions simultaneously, however we won't support the older of the two.
    - All enum in responses are define as x-extensible-enum, it means they all are susceptible to increase values.

    ## Version criteria

        {MAJOR}.{MINOR}.{PATCH}

    1. `{MAJOR}` implies incompatible changes with the previous `{MAJOR}` version
    2. `{MINOR}` implies new functionalities or information compatible with previous version
    3. `{PATCH}` implies bug fixes compatible with previous version

servers:
  - url: https://provision-master.private.dev.k8s.masmovil.com/v3
    description: development
  - url: https://provision.sta.masstack.com/v3
    description: staging
  - url: https://provision.masstack.com/v3
    description: production

tags:
  - name: Agile TV
    description: API REST para gestionar suscripciones, productos y decodificadores (STB) del servicio Agile TV, actuando como fachada para el sistema de provisión SHoPA.

paths:
  /orgs/{org_id}/tv/agile/subscriptions/{service_id}:
    get:
      tags:
        - Agile TV
      summary: Consultar una suscripción
      description: Obtiene la información detallada de una suscripción, incluyendo productos y decodificadores.
      operationId: agileGetSubscription
      parameters:
        - $ref: '#/components/parameters/OrgId'
        - $ref: '#/components/parameters/ServiceId'
        - $ref: '#/components/parameters/RequestCorrelationId'
      responses:
        '200':
          description: Información de la suscripción obtenida correctamente.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AgileSubscriptionInfo'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
        '500':
          $ref: '#/components/responses/InternalServerError'
    post:
      tags:
        - Agile TV
      summary: Activar una nueva suscripción
      description: Realiza el alta inicial de un cliente con sus productos, addons y decodificadores.
      operationId: agileActivateSubscription
      parameters:
        - $ref: '#/components/parameters/OrgId'
        - $ref: '#/components/parameters/ServiceId'
        - $ref: '#/components/parameters/RequestCorrelationId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AgileActivateSubscriptionRequest'
      responses:
        '202':
          description: Petición de alta aceptada. La provisión se realiza de forma asíncrona.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AgileWorkflowResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '500':
          $ref: '#/components/responses/InternalServerError'
    delete:
      tags:
        - Agile TV
      summary: Dar de baja una suscripción
      description: Realiza la baja completa del servicio de TV para un cliente.
      operationId: agileTerminateSubscription
      parameters:
        - $ref: '#/components/parameters/OrgId'
        - $ref: '#/components/parameters/ServiceId'
        - $ref: '#/components/parameters/RequestCorrelationId'
      responses:
        '202':
          description: Petición de baja aceptada. La provisión se realiza de forma asíncrona.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AgileWorkflowResponse'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
        '500':
          $ref: '#/components/responses/InternalServerError'
    patch:
      tags:
        - Agile TV
      summary: Editar datos de una suscripción
      description: Permite editar datos generales de una suscripción existente.
      operationId: agileEditSubscription
      parameters:
        - $ref: '#/components/parameters/OrgId'
        - $ref: '#/components/parameters/ServiceId'
        - $ref: '#/components/parameters/RequestCorrelationId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AgileEditSubscriptionRequest'
      responses:
        '202':
          description: Petición de edición aceptada.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AgileWorkflowResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
        '500':
          $ref: '#/components/responses/InternalServerError'

  /orgs/{org_id}/tv/agile/subscriptions/{service_id}/products:
    patch:
      tags:
        - Agile TV
      summary: Modificar productos y addons de una suscripción
      description: Permite añadir o eliminar productos principales y addons.
      operationId: agileModifyProductsAndAddons
      parameters:
        - $ref: '#/components/parameters/OrgId'
        - $ref: '#/components/parameters/ServiceId'
        - $ref: '#/components/parameters/RequestCorrelationId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AgileModifyProductsRequest'
      responses:
        '202':
          description: Petición de modificación de productos aceptada.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AgileWorkflowResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
        '500':
          $ref: '#/components/responses/InternalServerError'

  /orgs/{org_id}/tv/agile/subscriptions/{service_id}/stbs:
    patch:
      tags:
        - Agile TV
      summary: Modificar decodificadores (STBs) de una suscripción
      description: Permite añadir o eliminar decodificadores de una suscripción.
      operationId: agileModifyStbs
      parameters:
        - $ref: '#/components/parameters/OrgId'
        - $ref: '#/components/parameters/ServiceId'
        - $ref: '#/components/parameters/RequestCorrelationId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AgileModifyStbsRequest'
      responses:
        '202':
          description: Petición de modificación de STBs aceptada.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AgileWorkflowResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
        '500':
          $ref: '#/components/responses/InternalServerError'

  /orgs/{org_id}/tv/agile/subscriptions/{service_id}/suspend:
    post:
      tags:
        - Agile TV
      summary: Bloquear (suspender) una suscripción
      description: Suspende el servicio de TV o únicamente el servicio de compras.
      operationId: agileSuspendSubscription
      parameters:
        - $ref: '#/components/parameters/OrgId'
        - $ref: '#/components/parameters/ServiceId'
        - $ref: '#/components/parameters/RequestCorrelationId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AgileSuspendRequest'
      responses:
        '202':
          description: Petición de bloqueo aceptada.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AgileWorkflowResponse'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
        '500':
          $ref: '#/components/responses/InternalServerError'

  /orgs/{org_id}/tv/agile/subscriptions/{service_id}/reactivate:
    post:
      tags:
        - Agile TV
      summary: Desbloquear (reactivar) una suscripción
      description: Reactiva una suscripción previamente suspendida.
      operationId: agileReactivateSubscription
      parameters:
        - $ref: '#/components/parameters/OrgId'
        - $ref: '#/components/parameters/ServiceId'
        - $ref: '#/components/parameters/RequestCorrelationId'
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AgileReactivateSubscriptionRequest'
      responses:
        '202':
          description: Petición de desbloqueo aceptada.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AgileWorkflowResponse'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
        '500':
          $ref: '#/components/responses/InternalServerError'

components:
  parameters:
    OrgId:
      in: path
      name: org_id
      required: true
      description: Identificador de la marca.
      schema:
        type: string
      example: masmovil

    ServiceId:
      in: path
      name: service_id
      required: true
      description: Identificador único de la suscripción del cliente.
      schema:
        type: string
      example: "ACC_123456"

    RequestCorrelationId:
      in: header
      name: Request-Correlation-Id
      required: true
      description: Identificador para trazar la transacción a través de los sistemas.
      schema:
        type: string
      example: "2106f808-36e9-402d-bf37-f1f7fa04776b"

  responses:
    BadRequest:
      description: La petición es incorrecta o faltan parámetros.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AgileErrorResponse'
    Unauthorized:
      description: Fallo de autenticación o autorización.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AgileErrorResponse'
    NotFound:
      description: El recurso solicitado (ej. suscripción) no ha sido encontrado.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AgileErrorResponse'
    InternalServerError:
      description: Error interno en el servidor.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AgileErrorResponse'

  schemas:
    # ------------- Request Schemas -------------
    AgileActivateSubscriptionRequest:
      type: object
      required:
        - products
        - postalCode
      properties:
        externalId:
          type: string
          description: Identificador opcional para la transacción.
        postalCode:
          type: string
          example: "48001"
        ftthWorkOrderId:
          type: string
          description: Identificador de la orden de trabajo FTTH.
        products:
          type: array
          minItems: 1
          items:
            $ref: '#/components/schemas/AgileProduct'
        addons:
          type: array
          items:
            $ref: '#/components/schemas/AgileProduct'
        stbs:
          type: array
          items:
            $ref: '#/components/schemas/AgileSettopboxInput'

    AgileEditSubscriptionRequest:
      type: object
      required:
        - postalCode
        - ftthWorkOrderId
      properties:
        externalId:
          type: string
          description: Identificador opcional para la transacción.
        postalCode:
          type: string
          example: "48001"
        ftthWorkOrderId:
          type: string
          description: Identificador de la orden de trabajo FTTH.

    AgileModifyProductsRequest:
      type: object
      properties:
        externalId:
          type: string
          description: Identificador opcional para la transacción.
        productsToAdd:
          type: array
          description: Lista de productos principales para añadir.
          items:
            $ref: '#/components/schemas/AgileProduct'
        productsToDelete:
          type: array
          description: Lista de productos principales para eliminar.
          items:
            $ref: '#/components/schemas/AgileProduct'
        addonsToAdd:
          type: array
          description: Lista de addons para añadir.
          items:
            $ref: '#/components/schemas/AgileProduct'
        addonsToDelete:
          type: array
          description: Lista de addons para eliminar.
          items:
            $ref: '#/components/schemas/AgileProduct'

    AgileModifyStbsRequest:
      type: object
      properties:
        externalId:
          type: string
          description: Identificador opcional para la transacción.
        postalCode:
          type: string
          description: Código postal asociado a la dirección de los STBs. Requerido si se añaden nuevos STBs.
          example: "48001"
        ftthWorkOrderId:
          type: string
          description: Identificador de la orden de trabajo FTTH, necesario para algunas operaciones de STB.
        stbsToAdd:
          type: array
          items:
            $ref: '#/components/schemas/AgileSettopboxInput'
        stbsToDelete:
          type: array
          description: Lista de STBs a eliminar.
          items:
            $ref: '#/components/schemas/AgileSettopboxInput'

    AgileSuspendRequest:
      type: object
      properties:
        externalId:
          type: string
          description: Identificador opcional para la transacción.

    AgileReactivateSubscriptionRequest:
      type: object
      properties:
        externalId:
          type: string
          description: Identificador opcional para la transacción.

    # ------------- Shared Objects -------------
    AgileProduct:
      type: object
      description: Representa un producto con su identificador externo opcional.
      required:
        - id
      properties:
        id:
          type: string
          description: ID del producto.
        externalId:
          type: string
          description: Identificador externo opcional para el producto.

    AgileSettopboxInput:
      type: object
      description: Representa un decodificador para operaciones de entrada.
      required:
        - stbReference
        - macAddress
        - serialNumber
      properties:
        stbReference:
          type: string
          description: Referencia única del decodificador.
        macAddress:
          type: string
          description: Dirección MAC del dispositivo.
        serialNumber:
          type: string
          description: Número de serie del dispositivo.
        externalId:
          type: string
          description: Identificador externo opcional para el STB.

    # ------------- Response Schemas -------------
    AgileWorkflowResponse:
      type: object
      properties:
        message:
          type: string
          example: "Request accepted and is being processed."
        workflowId:
          type: string
          example: "agile-tv-bf37-f1f7fa04776b"

    AgileSubscriptionInfo:
      type: object
      properties:
        service_id:
          type: string
          example: "ACC_123456"
        accountState:
          type: string
          x-extensible-enum:
            - ACTIVE
            - SUSPENDED
          example: ACTIVE
        products:
          type: array
          items:
            $ref: '#/components/schemas/AgileProduct'
        stbs:
          type: array
          items:
            $ref: '#/components/schemas/AgileSettopboxInput'

    AgileErrorResponse:
      type: object
      properties:
        code:
          type: string
          example: "RESOURCE_NOT_FOUND"
        title:
          type: string
          example: "Subscription not found"
        details:
          type: string
          example: "The subscription with service_id 'ACC_999999' does not exist."
