subscription:
  post:
    tags: [Shared Package]
    summary: Subscribe to an AI pricing package
    description: |
      - Requires an authenticated **client or provider** bearer token (`ClientBearerAuth` or `ProviderBearerAuth`).
      - Request body is **multipart/form-data** with `id` (AI `Package` id) and `paymentMethod` (`wallet` | `online`).
      - Optional coupon fields: `coupon` (Coupon id from `PATCH /apply-coupon`) and `totalAfterCoupon` (required when `coupon` is set). Server reloads the coupon, recomputes discount from `package.price`, rejects mismatches beyond ±0.02, charges the discounted amount, stores coupon fields on the subscription, and atomically increments coupon `counter` only when `payableAmount > 0`.
      - Same payment strategies as the rest of More (`wallet` deducts balance; `online` is currently a pass-through stub).
      - Free packages (`isFree: true` or `price: 0`) skip charging and reject coupons.
      - Creates a `Subscription` row (`userType` + `userRef` match the bearer actor) and points `user.subscribe` at it.
      - Re-subscribing to the **same** package while it is still active is rejected.
      - Any previous **active** AI subscriptions for the same user on a **different** package are set to `cancelled` (and their expire cron jobs are cleared) so only one active row exists.
      - When the expire cron runs for the new subscription, its status becomes `expired`.

      <div class="figma-links"><span>Design:</span><a href="https://www.figma.com/proto/mTRILiWaS8pzLcNnmAUr2k/%D9%83%D9%85-%D8%AA%D8%B3%D9%88%D9%8A?node-id=10792-109725&viewport=176%2C570%2C0.02&t=c4yTtQLlUc9FQir5-1&scaling=scale-down&content-scaling=fixed&starting-point-node-id=9064%3A6087&page-id=0%3A1" target="_blank" rel="noopener">Mobile</a><a href="https://www.figma.com/proto/mTRILiWaS8pzLcNnmAUr2k/%D9%83%D9%85-%D8%AA%D8%B3%D9%88%D9%8A?page-id=10227%3A31854&node-id=10792-112272&viewport=-948%2C0%2C0.12&t=PNIRCKFxGV24peHK-1&scaling=min-zoom&content-scaling=fixed&starting-point-node-id=10449%3A65038" target="_blank" rel="noopener">Web</a></div>
    x-figma:
      mobile: "https://www.figma.com/proto/mTRILiWaS8pzLcNnmAUr2k/%D9%83%D9%85-%D8%AA%D8%B3%D9%88%D9%8A?node-id=10792-109725&viewport=176%2C570%2C0.02&t=c4yTtQLlUc9FQir5-1&scaling=scale-down&content-scaling=fixed&starting-point-node-id=9064%3A6087&page-id=0%3A1"
      mobileNodeId: "10792:109725"
      web: "https://www.figma.com/proto/mTRILiWaS8pzLcNnmAUr2k/%D9%83%D9%85-%D8%AA%D8%B3%D9%88%D9%8A?page-id=10227%3A31854&node-id=10792-112272&viewport=-948%2C0%2C0.12&t=PNIRCKFxGV24peHK-1&scaling=min-zoom&content-scaling=fixed&starting-point-node-id=10449%3A65038"
      webNodeId: "10792:112272"
    operationId: subscribeAiPackage
    security:
      - SecretKeyAuth: []
        ClientBearerAuth: []
      - SecretKeyAuth: []
        ProviderBearerAuth: []
    parameters:
      - $ref: '../openapi.yaml#/components/parameters/LangHeader'
    requestBody:
      required: true
      content:
        multipart/form-data:
          schema:
            $ref: '../openapi.yaml#/components/schemas/SubscribeRequest'
          examples:
            wallet:
              summary: Pay from wallet
              value:
                id: "665f1c2a9b4e1d0012ab34a1"
                paymentMethod: wallet
            withCoupon:
              summary: Pay with coupon (discounted wallet charge)
              value:
                id: "665f1c2a9b4e1d0012ab34a1"
                paymentMethod: wallet
                coupon: "665f1c2a9b4e1d0012ab34c0"
                totalAfterCoupon: 80
            online:
              summary: Pay online (stub)
              value:
                id: "665f1c2a9b4e1d0012ab34a1"
                paymentMethod: online
    responses:
      '200':
        description: Subscription created. Previous active AI subscriptions for this user are cancelled.
        content:
          application/json:
            schema: { $ref: '../openapi.yaml#/components/schemas/SubscribeSuccessResponse' }
            examples:
              success:
                value:
                  key: success
                  message: "تم الاشتراك بنجاح"
                  status: 200
      '400':
        description: Validation or payment failure.
        content:
          application/json:
            schema: { $ref: '../openapi.yaml#/components/schemas/ErrorResponse' }
            examples:
              invalidPayment:
                value: { key: fail, message: "طريقة الدفع غير صحيحة. اختر من [wallet, online]", status: 400 }
              insufficientBalance:
                value: { key: fail, message: "رصيد المحفظة غير كافٍ", status: 400 }
      '404':
        description: Package not found.
        content:
          application/json:
            schema: { $ref: '../openapi.yaml#/components/schemas/ErrorResponse' }
            example: { key: notFound, message: "الباقه غير موجودة", status: 404 }
      '419':
        description: Bearer token is missing, invalid, expired, or revoked.
        content:
          application/json:
            schema: { $ref: '../openapi.yaml#/components/schemas/ErrorResponse' }
            example: { key: unauthorized, message: "يجب تسجيل الدخول", status: 419 }
      '500': { $ref: '#/shared-server-error' }
