# DataProductSpec — lamoda.orders: enrich-архетип — per-заказ второй GET
# /api/v1/orders/{id} (детали с items), стоп сбора по createdAt < today-7.
apiVersion: specs.gwptd.io/v1alpha1
kind: DataProduct
metadata:
  name: lamoda.orders
  generation: 1
  lifecycle: active
contract:
  version: 1.0.0

source:
  marketplaceRef: lamoda
  mpId: 9
  authProfileRef: lamoda.oauth
  originRef: lamoda.b2b

request:
  method: GET
  path: /api/v1/orders

transport:
  strategy: http.page_number_query
  limit: 100
  limitParam: limit
  pageParam: page
  shortPageStop: true                # S2: stop immediately on a short list page
  maxPages: 500

extract:
  itemsPath: "$._embedded.orders"
  businessKeyPaths: ["$.id"]
  itemStop:
    field: createdAt
    sliceDays: 7                     # сем. legacy cutoff (стоп на первом старом)
  enrich:
    pathTemplate: "/api/v1/orders/{id}"
    idField: id
    mergeKey: _detail

outputs:
  intakeByKind:
    field: _kind
    defaultKind: order
    targets:
      order:
        - table: lamoda_orders             # INSERT-only журнал (родитель)
          fields:
            payload_id:       {var: payload_id}
            mp_id:            {var: mp_id}
            order_id:         {python_func: lam_ord_id_str}
            status:           {json_path: $.status}
      # `id` внутри shipping_json — идентификатор АДРЕСА доставки, а не заказа.
            shipping_id: {json_path: $._embedded.shippingAddress.id}
            full_sum:         {json_path: $.fullSum}
            created_at_mp:    {python_func: lam_ord_created}
            updated_at_mp:    {python_func: lam_ord_updated}
            delivery_date_mp: {python_func: lam_ord_delivery}
            region:           {python_func: lam_ord_region}
            city:             {python_func: lam_ord_city}
            items_count:      {python_func: lam_ord_items_count}
            items_json:       {python_func: lam_ord_items_json}
            shipping_json:    {python_func: lam_ord_shipping_json}
            # --- Листья блоба shipping_json вне массивов (13 полей), замер 09.08.2026 ---
            # Блоб = (item._embedded or {}).get('shippingAddress') — путь к данным НЕ
            # совпадает с путём внутри блоба. Внутри блоба $.apartment, в item'е —
            # $._embedded.shippingAddress.apartment. Предыдущая работа ошибочно брала
            # $._detail.shipping.* — см. RULES.md пункт «Грабли» в .task.md.
            # id пропущен: колонка id = AUTO_INCREMENT PK.
            apartment:             {json_path: $._embedded.shippingAddress.apartment}
            buildingId:            {json_path: $._embedded.shippingAddress.buildingId}
            cityId:                {json_path: $._embedded.shippingAddress.cityId}
            coordinate_latitude:   {json_path: $._embedded.shippingAddress.coordinate.latitude}
            coordinate_longitude:  {json_path: $._embedded.shippingAddress.coordinate.longitude}
            houseNum:              {json_path: $._embedded.shippingAddress.houseNum}
            isEditable:            {json_path: $._embedded.shippingAddress.isEditable}
            isValid:               {json_path: $._embedded.shippingAddress.isValid}
            pickupPointId:         {json_path: $._embedded.shippingAddress.pickupPointId}
            street:                {json_path: $._embedded.shippingAddress.street}
            streetId:              {json_path: $._embedded.shippingAddress.streetId}
            zipcode:               {json_path: $._embedded.shippingAddress.zipcode}
        - table: lamoda_orders_items         # позиции заказа (дочерняя)
          explode:
            listFieldAny: [_detail._embedded.items]
            parentFields: {order_id: id}
          fields:
            payload_id:            {var: payload_id}
            mp_id:                 {var: mp_id}
            order_id:              {json_path: $.order_id}
            item_id:               {json_path: $.id}
            basePrice:             {json_path: $.basePrice}
            basePriceRub:          {json_path: $.basePriceRub}
            canBeCancelled:        {json_path: $.canBeCancelled}
            couponDiscount:        {json_path: $.couponDiscount}
            couponDiscountRub:     {json_path: $.couponDiscountRub}
            datamatrix:            {json_path: $.datamatrix}
            datamatrixCode:        {json_path: $.datamatrixCode}
            description:           {json_path: $.description}
            discrepancyAct:        {json_path: $.discrepancyAct}
            finalERPStatus:        {json_path: $.finalERPStatus}
            lamodaSku:             {json_path: $.lamodaSku}
            loyaltyDiscount:       {json_path: $.loyaltyDiscount}
            loyaltyDiscountRub:    {json_path: $.loyaltyDiscountRub}
            otherDiscounts:        {json_path: $.otherDiscounts}
            otherDiscountsRub:     {json_path: $.otherDiscountsRub}
            paidPrice:             {json_path: $.paidPrice}
            paidPriceRub:          {json_path: $.paidPriceRub}
            partnerAgreedDiscount: {json_path: $.partnerAgreedDiscount}
            partnerAgreedDiscountRub: {json_path: $.partnerAgreedDiscountRub}
            partnerAgreedPrice:    {json_path: $.partnerAgreedPrice}
            partnerAgreedPriceRub: {json_path: $.partnerAgreedPriceRub}
            platformDiscounts:     {json_path: $.platformDiscounts}
            salePrice:             {json_path: $.salePrice}
            salePriceRub:          {json_path: $.salePriceRub}
            size:                  {json_path: $.size}
            sku:                   {json_path: $.sku}
            status:                {json_path: $.status}
            statusChangeReason:    {json_path: $.statusChangeReason}
            totalDiscount:         {json_path: $.totalDiscount}
            totalDiscountRub:      {json_path: $.totalDiscountRub}
