{%- comment -%}
  wine-club-customize-data.liquid
  Server → client data contract for the customization portal.

  Resolves: logged-in customer → their tier → the active (open) cycle → that
  tier's pool. Emits a single JSON blob consumed by wine-club-customize.js.

  Prices are read LIVE from Shopify variants (decision #3) — never stored.
  Requires the customer to be tagged/segmented with their tier, e.g. a customer
  tag `wc-tier:classic-3` set by Appstle/Flow on subscription. Adjust the
  `customer_tier` lookup to match how Appstle exposes the plan on the store.

  Inputs (assigns expected before render):
    - active_cycle : a wineclub_cycle metaobject (status == "open")
    - tier_pool    : the wineclub_tier_pool metaobject matching customer's tier
{%- endcomment -%}

{%- liquid
  assign saved = customer.metafields.wineclub.selection.value
  assign saved_for_cycle = nil
  if saved
    assign saved_for_cycle = saved[active_cycle.handle]
  endif

  # Wine-preference (mixed | reds | white) from a `wc-pref:<value>` customer tag.
  # Defaults to "mixed" so nothing breaks if the tag isn't set. At runtime the JS also
  # re-derives this from the Appstle subscription variant when the portal loads a contract.
  assign wc_pref = 'mixed'
  for tag in customer.tags
    if tag contains 'wc-pref:'
      assign wc_pref = tag | remove: 'wc-pref:'
    endif
  endfor
-%}

<script id="wc-data" type="application/json">
{
  "cycle": {
    "handle": {{ active_cycle.handle | json }},
    "name": {{ active_cycle.name | json }},
    "closeAt": {{ active_cycle.close_at | json }},
    "status": {{ active_cycle.status | json }}
  },
  "tier": {
    "key": {{ tier_pool.tier | json }},
    "label": {{ tier_pool.tier_label | json }},
    "bottleCount": {{ tier_pool.bottle_count | json }},
    "basePrice": {{ tier_pool.base_price.value.amount | default: 0 | json }},
    "preference": {{ wc_pref | json }}
  },
  "customer": {
    "id": {{ customer.id | json }},
    "firstName": {{ customer.first_name | json }},
    "loggedIn": {% if customer %}true{% else %}false{% endif %}
  },
  "defaultBox": [
    {%- for product in tier_pool.default_wines.value -%}
      {%- assign v = product.selected_or_first_available_variant -%}
      {
        "productId": {{ product.id | json }},
        "variantId": {{ v.id | json }},
        "name": {{ product.title | json }},
        "vintage": {{ product.metafields.wineclub.vintage | default: "" | json }},
        "type": {{ product.metafields.wineclub.wine_type | default: "red" | json }},
        "price": {{ v.price | money_without_currency | strip | json }},
        "available": {{ v.available | json }}
      }{%- unless forloop.last -%},{%- endunless -%}
    {%- endfor -%}
  ],
  "swapPool": [
    {%- for product in tier_pool.swap_pool.value -%}
      {%- assign v = product.selected_or_first_available_variant -%}
      {
        "productId": {{ product.id | json }},
        "variantId": {{ v.id | json }},
        "name": {{ product.title | json }},
        "vintage": {{ product.metafields.wineclub.vintage | default: "" | json }},
        "type": {{ product.metafields.wineclub.wine_type | default: "red" | json }},
        "price": {{ v.price | money_without_currency | strip | json }},
        "group": {{ product.metafields.wineclub.catalog_group | default: "Store catalog" | json }},
        "available": {{ v.available | json }}
      }{%- unless forloop.last -%},{%- endunless -%}
    {%- endfor -%}
  ],
  "upgrade": {
    {%- assign ut = tier_pool.upgrade_target.value -%}
    "hasTarget": {% if ut %}true{% else %}false{% endif %},
    "targetLabel": {{ ut.tier_label | default: "" | json }},
    "targetKey": {{ ut.tier | default: "" | json }},
    "targetFromPrice": {{ ut.base_price.value.amount | default: 0 | json }}
  },
  "saved": {{ saved_for_cycle | default: nil | json }}
}
</script>
