{{ config(
    indexes = [{'columns':['_airbyte_emitted_at'],'type':'btree'}],
    unique_key = '_airbyte_ab_id',
    schema = "_airbyte_unibag",
    tags = [ "top-level-intermediate" ]
) }}
-- SQL model to parse JSON blob stored in a single column and extract into separated field columns as described by the JSON Schema
-- depends_on: {{ source('unibag', '_airbyte_raw_orders') }}
select
    {{ json_extract_scalar('_airbyte_data', ['_id'], ['_id']) }} as _id,
    {{ json_extract_scalar('_airbyte_data', ['code'], ['code']) }} as code,
    {{ json_extract_scalar('_airbyte_data', ['date'], ['date']) }} as {{ adapter.quote('date') }},
    {{ json_extract_scalar('_airbyte_data', ['hour'], ['hour']) }} as {{ adapter.quote('hour') }},
    {{ json_extract_scalar('_airbyte_data', ['note'], ['note']) }} as note,
    {{ json_extract_array('_airbyte_data', ['skus'], ['skus']) }} as skus,
    {{ json_extract_array('_airbyte_data', ['tags'], ['tags']) }} as tags,
    {{ json_extract_scalar('_airbyte_data', ['user'], ['user']) }} as {{ adapter.quote('user') }},
    {{ json_extract('table_alias', '_airbyte_data', ['price'], ['price']) }} as price,
    {{ json_extract_scalar('_airbyte_data', ['banned'], ['banned']) }} as banned,
    {{ json_extract_scalar('_airbyte_data', ['reason'], ['reason']) }} as reason,
    {{ json_extract_scalar('_airbyte_data', ['source'], ['source']) }} as {{ adapter.quote('source') }},
    {{ json_extract_scalar('_airbyte_data', ['status'], ['status']) }} as status,
    {{ json_extract_scalar('_airbyte_data', ['update'], ['update']) }} as {{ adapter.quote('update') }},
    {{ json_extract('table_alias', '_airbyte_data', ['payment'], ['payment']) }} as payment,
    {{ json_extract_scalar('_airbyte_data', ['remarks'], ['remarks']) }} as remarks,
    {{ json_extract_scalar('_airbyte_data', ['customer'], ['customer']) }} as customer,
    {{ json_extract('table_alias', '_airbyte_data', ['delivery'], ['delivery']) }} as delivery,
    {{ json_extract_scalar('_airbyte_data', ['pickupAt'], ['pickupAt']) }} as pickupat,
    {{ json_extract_array('_airbyte_data', ['products'], ['products']) }} as products,
    {{ json_extract('table_alias', '_airbyte_data', ['supplier'], ['supplier']) }} as supplier,
    {{ json_extract_scalar('_airbyte_data', ['approveAt'], ['approveAt']) }} as approveat,
    {{ json_extract_scalar('_airbyte_data', ['createdAt'], ['createdAt']) }} as createdat,
    {{ json_extract('table_alias', '_airbyte_data', ['inventory'], ['inventory']) }} as inventory,
    {{ json_extract_scalar('_airbyte_data', ['isDeleted'], ['isDeleted']) }} as isdeleted,
    {{ json_extract('table_alias', '_airbyte_data', ['promotion'], ['promotion']) }} as promotion,
    {{ json_extract_scalar('_airbyte_data', ['requestId'], ['requestId']) }} as requestid,
    {{ json_extract('table_alias', '_airbyte_data', ['sendEmail'], ['sendEmail']) }} as sendemail,
    {{ json_extract_scalar('_airbyte_data', ['totalItem'], ['totalItem']) }} as totalitem,
    {{ json_extract_scalar('_airbyte_data', ['updatedAt'], ['updatedAt']) }} as updatedat,
    {{ json_extract('table_alias', '_airbyte_data', ['userAgent'], ['userAgent']) }} as useragent,
    {{ json_extract_scalar('_airbyte_data', ['wholesale'], ['wholesale']) }} as wholesale,
    {{ json_extract_scalar('_airbyte_data', ['approvedAt'], ['approvedAt']) }} as approvedat,
    {{ json_extract_scalar('_airbyte_data', ['cashbackAt'], ['cashbackAt']) }} as cashbackat,
    {{ json_extract_scalar('_airbyte_data', ['codeOsiris'], ['codeOsiris']) }} as codeosiris,
    {{ json_extract_scalar('_airbyte_data', ['isPreorder'], ['isPreorder']) }} as ispreorder,
    {{ json_extract_scalar('_airbyte_data', ['membership'], ['membership']) }} as membership,
    {{ json_extract_scalar('_airbyte_data', ['rejectedAt'], ['rejectedAt']) }} as rejectedat,
    {{ json_extract_scalar('_airbyte_data', ['deliveredAt'], ['deliveredAt']) }} as deliveredat,
    {{ json_extract_scalar('_airbyte_data', ['deliveringAt'], ['deliveringAt']) }} as deliveringat,
    {{ json_extract_scalar('_airbyte_data', ['deliveryCode'], ['deliveryCode']) }} as deliverycode,
    {{ json_extract_scalar('_airbyte_data', ['searchString'], ['searchString']) }} as searchstring,
    {{ json_extract_scalar('_airbyte_data', ['staffApprove'], ['staffApprove']) }} as staffapprove,
    {{ json_extract_scalar('_airbyte_data', ['trackingCode'], ['trackingCode']) }} as trackingcode,
    {{ json_extract_scalar('_airbyte_data', ['processStatus'], ['processStatus']) }} as processstatus,
    {{ json_extract_scalar('_airbyte_data', ['hookTimeLastAt'], ['hookTimeLastAt']) }} as hooktimelastat,
    {{ json_extract_scalar('_airbyte_data', ['trackingCodeURL'], ['trackingCodeURL']) }} as trackingcodeurl,
    {{ json_extract_scalar('_airbyte_data', ['approvePaymentAt'], ['approvePaymentAt']) }} as approvepaymentat,
    {{ json_extract_scalar('_airbyte_data', ['isChangeDelivery'], ['isChangeDelivery']) }} as ischangedelivery,
    {{ json_extract_scalar('_airbyte_data', ['trackingOrderCode'], ['trackingOrderCode']) }} as trackingordercode,
    {{ json_extract_scalar('_airbyte_data', ['estimateCashbackAt'], ['estimateCashbackAt']) }} as estimatecashbackat,
    {{ json_extract_scalar('_airbyte_data', ['isWaitingCancelled'], ['isWaitingCancelled']) }} as iswaitingcancelled,
    {{ json_extract_scalar('_airbyte_data', ['fromNewActiveSeller'], ['fromNewActiveSeller']) }} as fromnewactiveseller,
    _airbyte_ab_id,
    _airbyte_emitted_at,
    {{ current_timestamp() }} as _airbyte_normalized_at
from {{ source('unibag', '_airbyte_raw_orders') }} as table_alias
-- orders
where 1 = 1

