{%- comment -%}
  Maison Fayard — Wine Club customization portal (section)
  Drop into a page template: create page /pages/wine-club-customize and assign a
  template that renders this section, or add it to the page via the theme editor.

  Depends on:
    - snippets/wine-club-customize-data.liquid  (data contract)
    - assets/wine-club-customize.css
    - assets/wine-club-customize.js
    - metaobjects: wineclub_cycle, wineclub_tier_pool  (see /metaobjects)
{%- endcomment -%}

{{ 'wine-club-customize.css' | asset_url | stylesheet_tag }}

{%- liquid
  # --- Resolve the active (open) cycle ------------------------------------
  assign active_cycle = nil
  for c in shop.metaobjects.wineclub_cycle.values
    if c.status == 'open'
      assign active_cycle = c
    endif
  endfor

  # --- Resolve the customer's tier from their tags (wc-tier:<key>) --------
  assign customer_tier = nil
  for tag in customer.tags
    if tag contains 'wc-tier:'
      assign customer_tier = tag | remove: 'wc-tier:'
    endif
  endfor

  # --- Find the tier pool for (active_cycle × customer_tier) --------------
  assign tier_pool = nil
  if active_cycle and customer_tier
    for p in active_cycle.tier_pools.value
      if p.tier == customer_tier
        assign tier_pool = p
      endif
    endfor
  endif
-%}

{%- if customer == nil -%}
  <div class="wc-page wc-empty">
    <p class="wc-empty-msg">Please <a href="/account/login">log in</a> to customize your shipment.</p>
  </div>

{%- elsif active_cycle == nil -%}
  <div class="wc-page wc-empty">
    <p class="wc-empty-msg">There's no open customization window right now. We'll email you when the next one opens.</p>
  </div>

{%- elsif tier_pool == nil -%}
  <div class="wc-page wc-empty">
    <p class="wc-empty-msg">We couldn't find an active membership on your account. Please <a href="/pages/contact">contact us</a> and we'll sort it out.</p>
  </div>

{%- else -%}
  {%- render 'wine-club-customize-data', active_cycle: active_cycle, tier_pool: tier_pool -%}

  <div class="wc-page"
       data-wc-root
       data-proxy="{{ section.settings.app_proxy_base | default: '/apps/wineclub' }}">
    <div class="wc-eyebrow">{{ active_cycle.name }} · {{ tier_pool.tier_label }}</div>
    <h1 class="wc-title">{{ section.settings.heading | default: 'Customize your shipment' }}</h1>
    <p class="wc-sub">{{ section.settings.subheading | default: 'Swap any bottle for a wine from your club selection. Price adjusts to reflect your choice.' }}</p>

    <div class="wc-deadline-bar" data-wc-deadline>Customization closes {{ active_cycle.close_at | date: '%B %e, %Y' }}</div>

    <div class="wc-tier-badge">
      {{ tier_pool.tier_label }} · <span data-wc-btl-count>{{ tier_pool.bottle_count }}</span> bottles
    </div>

    <div class="wc-section-label">Your selection</div>
    <div class="wc-bottle-list" data-wc-bottle-list></div>

    <div class="wc-catalog-panel" data-wc-catalog-panel hidden>
      <div class="wc-catalog-header">
        <span class="wc-catalog-header-label">Choose a wine</span>
        <button class="wc-close-btn" type="button" data-wc-close>× cancel</button>
      </div>
      <div class="wc-filter-row" data-wc-filter-row></div>
      <div data-wc-catalog-items></div>
    </div>

    <hr class="wc-hr">

    <div class="wc-section-label">Upgrade</div>
    <div class="wc-upgrade-box">
      <div class="wc-upgrade-row">
        <div>
          <div class="wc-upgrade-label">Add a bottle to this shipment</div>
          <div class="wc-upgrade-sub">One-time — just for {{ active_cycle.name }}</div>
        </div>
        <button class="wc-upgrade-btn" type="button" data-wc-add-bottle>+ Add bottle</button>
      </div>
      <div class="wc-upgrade-sep" data-wc-upgrade-sep hidden></div>
      <div class="wc-upgrade-row" data-wc-switch-row hidden>
        <div>
          <div class="wc-upgrade-label">Switch to <span data-wc-upgrade-label></span></div>
          <div class="wc-upgrade-sub">Permanent · from $<span data-wc-upgrade-from></span> / shipment</div>
        </div>
        <button class="wc-upgrade-btn" type="button" data-wc-switch-tier>Switch tier</button>
      </div>
    </div>

    <div class="wc-summary-box">
      <div class="wc-summary-row"><span class="wc-summary-label">Bottles selected</span><span class="wc-summary-val" data-wc-s-count></span></div>
      <div class="wc-summary-row"><span class="wc-summary-label">Base price</span><span class="wc-summary-val" data-wc-s-base></span></div>
      <div class="wc-summary-row wc-summary-row--last"><span class="wc-summary-label">Adjustments</span><span class="wc-summary-val" data-wc-s-adj></span></div>
      <div class="wc-summary-divider"></div>
      <div class="wc-summary-row wc-summary-row--last">
        <span class="wc-summary-total-label">Shipment total</span>
        <span class="wc-summary-total-val" data-wc-s-total></span>
      </div>
      <div class="wc-min-warning" data-wc-min-warning hidden>⚠ Minimum <span data-wc-min></span> bottles required</div>
    </div>

    <button class="wc-confirm-btn" type="button" data-wc-confirm>Lock in my selection</button>
    <div class="wc-note" data-wc-note>No charge until we process your shipment. You can return and edit until {{ active_cycle.close_at | date: '%B %e' }}.</div>
  </div>

  {{ 'appstle-adapter.js' | asset_url | script_tag }}
  {{ 'wine-club-customize.js' | asset_url | script_tag }}
{%- endif -%}

{% schema %}
{
  "name": "Wine Club Customize",
  "settings": [
    { "type": "text", "id": "heading", "label": "Heading", "default": "Customize your shipment" },
    { "type": "textarea", "id": "subheading", "label": "Subheading", "default": "Swap any bottle for a wine from your club selection. Price adjusts to reflect your choice." },
    { "type": "text", "id": "app_proxy_base", "label": "App Proxy base path", "default": "/apps/wineclub" }
  ],
  "presets": [{ "name": "Wine Club Customize" }]
}
{% endschema %}
