# DataProductSpec — lamoda.status_dates: tracked return/claim statuses из B2B
# orders. Intake-only: legacy UPDATE mp_orders_daily намеренно НЕ переносится.
apiVersion: specs.gwptd.io/v1alpha1
kind: DataProduct
metadata:
  name: lamoda.status_dates
  generation: 2
  lifecycle: active
contract:
  version: 1.1.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
  totalPagesPath: "$.pages"
  shortPageStop: true
  # Live 2026-07-13: API сообщает 449 страниц. 500 оставляет bounded reserve;
  # hard-cap без штатного stop теперь принудительно делает CollectionRun partial.
  maxPages: 500

extract:
  itemsPath: "$._embedded.orders"
  businessKeyPaths: ["$.id"]
  filter:
    field: status
    values: [Returned, Claimed, "Claimed ok", "Claimed defective", "Claimed used"]
    dateField: updatedAt
    windowDays: 14
  enrich:
    pathTemplate: "/api/v1/orders/{id}"
    idField: id
    mergeKey: _detail

outputs:
  intakeByKind:
    field: _kind
    defaultKind: status_dates
    targets:
      status_dates:
        - table: lamoda_status_dates
          explode:
            # current replay хранит items сверху; live SpecCollector — в detail.
            listFieldAny: [items, _detail._embedded.items]
            fallbackEmpty: true
            parentFields:
              _order: [_order, "$"]
              order_id: [order_id, id]
              # Не писать order.status поверх item.status: item-level статус нужен
              # для точной семантики единицы, а order-level остаётся отдельно.
              order_status: status
              status_date: [status_date, updatedAt]
          fields:
            payload_id:             {var: payload_id}
            mp_id:                  {static: 9}
            order_id:               {json_path: $.order_id, default: ""}
            offer_id:               {python_func: lam_status_offer_id}
            item_id:                {python_func: lam_status_item_id}
            item_status:            {json_path: $.status}
            status:                 {json_path: $.order_status}
            status_date:            {python_func: lam_status_date}
            return_date:            {python_func: lam_status_return_date}
            claimed_date:           {python_func: lam_status_claimed_date}
            claimed_ok_date:        {python_func: lam_status_claimed_ok_date}
            claimed_defective_date: {python_func: lam_status_claimed_defective_date}
            claimed_used_date:      {python_func: lam_status_claimed_used_date}
            raw_json:               {python_func: lam_status_raw_order}
            # --- поля первого уровня заказа (вне массивов), развёрнуты 08.08.2026 ---
            # Каждое поле площадки получает свою колонку, имя = путь ключей площадки через
            # подчёркивание.  _embedded.*, _links.* — вложенные объекты, не массивы.
            basePriceRub:                   {json_path: $._order.basePriceRub}
            comment:                        {json_path: $._order.comment}
            couponDiscountRub:              {json_path: $._order.couponDiscountRub}
            createdAt:                      {json_path: $._order.createdAt}
            currency:                       {json_path: $._order.currency}
            dates_delivered:                {json_path: $._order.dates.delivered}
            dates_shipped:                  {json_path: $._order.dates.shipped}
            deliveryPrice:                  {json_path: $._order.deliveryPrice}
            deliveryPriceRub:               {json_path: $._order.deliveryPriceRub}
            deprecatedId:                   {json_path: $._order.deprecatedId}
            exchangeRate:                   {json_path: $._order.exchangeRate}
            fullSum:                        {json_path: $._order.fullSum}
            fullSumRub:                     {json_path: $._order.fullSumRub}
            hasDiscrepancyActs:             {json_path: $._order.hasDiscrepancyActs}
            isConfirmed:                    {json_path: $._order.isConfirmed}
            isEditable:                     {json_path: $._order.isEditable}
            itemQuantity:                   {json_path: $._order.itemQuantity}
            loyaltyDiscountRub:             {json_path: $._order.loyaltyDiscountRub}
            orderNr:                        {json_path: $._order.orderNr}
            otherDiscountsRub:              {json_path: $._order.otherDiscountsRub}
            paidPriceRub:                   {json_path: $._order.paidPriceRub}
            partnerAgreedDiscountRub:       {json_path: $._order.partnerAgreedDiscountRub}
            partnerAgreedPriceRub:          {json_path: $._order.partnerAgreedPriceRub}
            paymentDate:                    {json_path: $._order.paymentDate}
            paymentMethod:                  {json_path: $._order.paymentMethod}
            rejectPrice:                    {json_path: $._order.rejectPrice}
            salePriceRub:                   {json_path: $._order.salePriceRub}
            totalDiscountRub:               {json_path: $._order.totalDiscountRub}
            updatedAt:                      {json_path: $._order.updatedAt}
            # _embedded.customer
            _embedded_customer_email:       {json_path: $._order._embedded.customer.email}
            _embedded_customer_firstName:   {json_path: $._order._embedded.customer.firstName}
            _embedded_customer_isEditable:  {json_path: $._order._embedded.customer.isEditable}
            _embedded_customer_lastName:    {json_path: $._order._embedded.customer.lastName}
            _embedded_customer_middleName:  {json_path: $._order._embedded.customer.middleName}
            _embedded_customer_phone:       {json_path: $._order._embedded.customer.phone}
            # _embedded.deliveryMethod
            _embedded_deliveryMethod_cutOff:                {json_path: $._order._embedded.deliveryMethod.cutOff}
            _embedded_deliveryMethod_deliveryDate:           {json_path: $._order._embedded.deliveryMethod.deliveryDate}
            _embedded_deliveryMethod_deliveryDateMax:        {json_path: $._order._embedded.deliveryMethod.deliveryDateMax}
            _embedded_deliveryMethod_deliveryDateMin:        {json_path: $._order._embedded.deliveryMethod.deliveryDateMin}
            _embedded_deliveryMethod_deliveryIntervalFrom:   {json_path: $._order._embedded.deliveryMethod.deliveryIntervalFrom}
            _embedded_deliveryMethod_deliveryIntervalId:     {json_path: $._order._embedded.deliveryMethod.deliveryIntervalId}
            _embedded_deliveryMethod_deliveryIntervalTo:     {json_path: $._order._embedded.deliveryMethod.deliveryIntervalTo}
            _embedded_deliveryMethod_deliveryPrice:          {json_path: $._order._embedded.deliveryMethod.deliveryPrice}
            _embedded_deliveryMethod_hasIntervals:           {json_path: $._order._embedded.deliveryMethod.hasIntervals}
            _embedded_deliveryMethod_isDeliveryPriceCustom:  {json_path: $._order._embedded.deliveryMethod.isDeliveryPriceCustom}
            _embedded_deliveryMethod_isEditable:             {json_path: $._order._embedded.deliveryMethod.isEditable}
            _embedded_deliveryMethod_serviceLevelTypeCode:   {json_path: $._order._embedded.deliveryMethod.serviceLevelTypeCode}
            _embedded_deliveryMethod_serviceLevelTypeName:   {json_path: $._order._embedded.deliveryMethod.serviceLevelTypeName}
            _embedded_deliveryMethod_shippingMethodCode:     {json_path: $._order._embedded.deliveryMethod.shippingMethodCode}
            _embedded_deliveryMethod_shippingMethodName:     {json_path: $._order._embedded.deliveryMethod.shippingMethodName}
            _embedded_deliveryMethod_standardDeliveryPrice:  {json_path: $._order._embedded.deliveryMethod.standardDeliveryPrice}
            _embedded_deliveryMethod_storageDays:            {json_path: $._order._embedded.deliveryMethod.storageDays}
            _embedded_externalDeliveryInfo:                  {json_path: $._order._embedded.externalDeliveryInfo}
            # _embedded.partner
            _embedded_partner_code:         {json_path: $._order._embedded.partner.code}
            _embedded_partner_partnerId:    {json_path: $._order._embedded.partner.partnerId}
            _embedded_partner_shopName:     {json_path: $._order._embedded.partner.shopName}
            # _embedded.shippingAddress
            _embedded_shippingAddress_apartment:             {json_path: $._order._embedded.shippingAddress.apartment}
            _embedded_shippingAddress_buildingId:            {json_path: $._order._embedded.shippingAddress.buildingId}
            _embedded_shippingAddress_city:                  {json_path: $._order._embedded.shippingAddress.city}
            _embedded_shippingAddress_cityId:                {json_path: $._order._embedded.shippingAddress.cityId}
            _embedded_shippingAddress_coordinate_latitude:   {json_path: $._order._embedded.shippingAddress.coordinate.latitude}
            _embedded_shippingAddress_coordinate_longitude:  {json_path: $._order._embedded.shippingAddress.coordinate.longitude}
            _embedded_shippingAddress_houseNum:              {json_path: $._order._embedded.shippingAddress.houseNum}
            _embedded_shippingAddress_id:                    {json_path: $._order._embedded.shippingAddress.id}
            _embedded_shippingAddress_isEditable:            {json_path: $._order._embedded.shippingAddress.isEditable}
            _embedded_shippingAddress_isValid:               {json_path: $._order._embedded.shippingAddress.isValid}
            _embedded_shippingAddress_pickupPointId:         {json_path: $._order._embedded.shippingAddress.pickupPointId}
            _embedded_shippingAddress_region:                {json_path: $._order._embedded.shippingAddress.region}
            _embedded_shippingAddress_street:                {json_path: $._order._embedded.shippingAddress.street}
            _embedded_shippingAddress_streetId:              {json_path: $._order._embedded.shippingAddress.streetId}
            _embedded_shippingAddress_zipcode:               {json_path: $._order._embedded.shippingAddress.zipcode}
            # _links
            _links_self_href:               {json_path: $._order._links.self.href}
        - table: lamoda_status_dates_payments
          explode:
            listFieldAny: [_embedded.payments]
            parentFields:
              order_id: [order_id, id]
          fields:
            payload_id: {var: payload_id}
            mp_id: {static: 9}
            order_id: {json_path: "$.order_id"}
            amount: {json_path: $.amount}
            failureRedirect: {json_path: $.failureRedirect}
            method: {json_path: $.method}
            paymentUrl: {json_path: $.paymentUrl}
            status: {json_path: $.status}
            successfulRedirect: {json_path: $.successfulRedirect}
            template: {json_path: $.template}

slo:
  # Daily starts at 08:00 UTC and the exact-detail walk has reached 55 min.
  # A literal 24h SLA therefore alerts during an otherwise healthy next run.
  freshnessHours: 27
