{# Human-readable labels for enum values — resolved from translation keys #}
{% set timeline_labels = {'asap': t.sd_timeline_asap, '3_6_months': t.sd_timeline_3_6mo, '6_12_months': t.sd_timeline_6_12mo, '12_plus': t.sd_timeline_12plus, 'exploring': t.sd_timeline_exploring} %}
{% set phase_labels = {'permit_granted': t.sd_phase_permit_granted, 'lease_signed': t.sd_phase_lease_signed, 'permit_pending': t.sd_phase_permit_pending, 'converting_existing': t.sd_phase_converting, 'permit_not_filed': t.sd_phase_permit_not_filed, 'location_found': t.sd_phase_location_found, 'searching': t.sd_phase_searching} %}
{% set financing_labels = {'self_funded': t.sd_financing_self_funded, 'loan_approved': t.sd_financing_loan_approved, 'seeking': t.sd_financing_seeking, 'not_started': t.sd_financing_not_started} %}
{% set decision_labels = {'solo': t.sd_decision_solo, 'partners': t.sd_decision_partners, 'board': t.sd_decision_board, 'investor': t.sd_decision_investor} %}
{% set contact_labels = {'received_quotes': t.sd_contact_received_quotes, 'contacted': t.sd_contact_contacted, 'none': t.sd_contact_none} %}
{% set stakeholder_labels = {'owner': t.sd_stakeholder_owner, 'investor': t.sd_stakeholder_investor, 'developer': t.sd_stakeholder_developer, 'club': t.sd_stakeholder_club, 'other': t.sd_stakeholder_other} %}

<div class="lf-card lf-card--unlocked">
  <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:0.75rem">
    <span class="lf-card__heat lf-card__heat--{{ lead.heat_score or 'cool' }}">{{ (lead.heat_score or 'cool') | upper }}</span>
    <span style="font-size:0.6875rem;color:#16A34A;font-weight:600">&#10003; {{ t.sd_unlocked_badge }}</span>
  </div>

  {# --- Project --- #}
  <div class="lf-section">
    <div class="lf-section__title">{{ t.sd_unlocked_section_project }}</div>
    <dl class="lf-card__meta">
      <dt>{{ t.sd_unlocked_label_facility }}</dt>
      <dd>{{ lead.facility_type or '-' }}{% if lead.build_context %} ({{ lead.build_context }}){% endif %}</dd>
      <dt>{{ t.sd_unlocked_label_courts }}</dt>
      <dd>{{ lead.court_count or '-' }}</dd>
      <dt>{{ t.sd_unlocked_label_glass }}</dt>
      <dd>{{ lead.glass_type or '-' }}</dd>
      <dt>{{ t.sd_unlocked_label_lighting }}</dt>
      <dd>{{ lead.lighting_type or '-' }}</dd>
      <dt>{{ t.sd_unlocked_label_budget }}</dt>
      <dd>{% if lead.budget_estimate %}&euro;{{ lead.budget_estimate }}{% else %}-{% endif %}</dd>
      <dt>{{ t.sd_unlocked_label_services }}</dt>
      <dd>{{ lead.services_needed or '-' }}</dd>
    </dl>
  </div>

  {# --- Location & Timeline --- #}
  <div class="lf-section">
    <div class="lf-section__title">{{ t.sd_unlocked_section_location }}</div>
    <dl class="lf-card__meta">
      <dt>{{ t.sd_unlocked_label_location }}</dt>
      <dd>{{ lead.location or '-' }}, {{ lead.country or '-' }}</dd>
      <dt>{{ t.sd_unlocked_label_timeline }}</dt>
      <dd>{{ timeline_labels.get(lead.timeline, lead.timeline) or '-' }}</dd>
      <dt>{{ t.sd_unlocked_label_phase }}</dt>
      <dd>{{ phase_labels.get(lead.location_status, lead.location_status) or '-' }}</dd>
    </dl>
  </div>

  {# --- Readiness --- #}
  <div class="lf-section">
    <div class="lf-section__title">{{ t.sd_unlocked_section_readiness }}</div>
    <dl class="lf-card__meta">
      <dt>{{ t.sd_unlocked_label_financing }}</dt>
      <dd>{{ financing_labels.get(lead.financing_status, lead.financing_status) or '-' }}</dd>
      <dt>{{ t.sd_unlocked_label_wants_financing }}</dt>
      <dd>{{ t.sd_unlocked_yes if lead.wants_financing_help else t.sd_unlocked_no }}</dd>
      <dt>{{ t.sd_unlocked_label_decision }}</dt>
      <dd>{{ decision_labels.get(lead.decision_process, lead.decision_process) or '-' }}</dd>
      <dt>{{ t.sd_unlocked_label_prior_contact }}</dt>
      <dd>{{ contact_labels.get(lead.previous_supplier_contact, lead.previous_supplier_contact) or '-' }}</dd>
    </dl>
  </div>

  {% if lead.additional_info %}
  <div class="lf-section">
    <div class="lf-section__title">{{ t.sd_unlocked_section_notes }}</div>
    <p style="font-size:0.75rem;color:#475569;background:#F8FAFC;padding:8px;border-radius:6px;margin:0">{{ lead.additional_info }}</p>
  </div>
  {% endif %}

  {# --- Contact --- #}
  <div class="lf-contact">
    <div class="lf-section__title" style="margin-bottom:6px">{{ t.sd_unlocked_section_contact }}</div>
    <dl style="display:grid;grid-template-columns:80px 1fr;gap:2px 8px">
      <dt>{{ t.sd_unlocked_label_name }}</dt>
      <dd>{{ lead.contact_name or '-' }}</dd>
      <dt>{{ t.sd_unlocked_label_email }}</dt>
      <dd><a href="mailto:{{ lead.contact_email }}" style="color:#1D4ED8">{{ lead.contact_email or '-' }}</a></dd>
      <dt>{{ t.sd_unlocked_label_phone }}</dt>
      <dd>{% if lead.contact_phone %}<a href="tel:{{ lead.contact_phone }}" style="color:#1D4ED8">{{ lead.contact_phone }}</a>{% else %}-{% endif %}</dd>
      <dt>{{ t.sd_unlocked_label_company }}</dt>
      <dd>{{ lead.contact_company or '-' }}</dd>
      <dt>{{ t.sd_unlocked_label_role }}</dt>
      <dd>{{ stakeholder_labels.get(lead.stakeholder_type, lead.stakeholder_type) or '-' }}</dd>
    </dl>
  </div>

  {# --- Scenario link --- #}
  {% set sid = scenario_id if scenario_id is defined else (scenario_ids.get(lead.user_id) if scenario_ids is defined else none) %}
  {% if sid %}
  <a href="{{ url_for('planner.index') }}?scenario={{ sid }}" target="_blank"
     style="display:block;text-align:center;margin-top:0.75rem;font-size:0.75rem;font-weight:600;color:#1D4ED8;text-decoration:none">
    {{ t.sd_unlocked_view_plan }} &rarr;
  </a>
  {% endif %}

  {% if credit_cost is defined %}
  <p style="font-size:0.6875rem;color:#94A3B8;margin-top:0.5rem;text-align:center">{{ credit_cost }} {{ t.sd_unlocked_credits_used }} &middot; {{ supplier.credit_balance }} {{ t.sd_unlocked_remaining }}</p>
  {% endif %}

  {# --- Lead-Back Guarantee button ---
     Show after 3 days, hide after 30 days, hide if already claimed.
     forward_id comes from routes (unlock response) or from lead.forward_id (dashboard feed).
  #}
  {% set _fid = forward_id if forward_id is defined else lead.get('forward_id') %}
  {% set _claimed_at = (guarantee_claimed_at if guarantee_claimed_at is defined else lead.get('guarantee_claimed_at')) %}
  {% set _forward_created = lead.get('forward_created_at') or lead.get('created_at') %}

  {% if guarantee_claimed %}
  <p style="font-size:0.8125rem;color:#16A34A;font-weight:600;margin-top:0.75rem;text-align:center">
    &#10003; {{ guarantee_success_msg or t.sd_guarantee_success }}
  </p>
  {% elif _fid and not _claimed_at %}
  {# JS calculates age client-side to avoid server-side timezone math in template #}
  <div id="guarantee-btn-{{ _fid }}" style="margin-top:0.75rem">
    <script>
    (function() {
      var fwd = "{{ _fid }}";
      var createdAt = "{{ _forward_created }}";
      if (!createdAt) return;
      var ageDays = (Date.now() - new Date(createdAt).getTime()) / 86400000;
      if (ageDays < 3 || ageDays > 30) return;
      var container = document.getElementById("guarantee-btn-" + fwd);
      if (!container) return;
      container.innerHTML = '<details style="border:1px solid #D1FAE5;border-radius:10px;padding:0.75rem;">' +
        '<summary style="cursor:pointer;font-size:0.8125rem;color:#16A34A;font-weight:600;list-style:none">' +
        '&#x1F6E1;&#xFE0F; {{ t.sd_guarantee_btn | e }}</summary>' +
        '<form hx-post="{{ url_for("suppliers.guarantee_claim", forward_id=0) }}'.replace("/0/", "/" + fwd + "/") +
        '" hx-target="#lead-card-' + (typeof lead_card_id !== "undefined" ? lead_card_id : fwd) + '" hx-swap="innerHTML"' +
        ' style="margin-top:0.75rem">' +
        '<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">' +
        '<p style="font-size:0.8125rem;color:#475569;margin:0 0 0.5rem">{{ t.sd_guarantee_contact_label | e }}</p>' +
        '<div style="display:flex;flex-direction:column;gap:6px;margin-bottom:0.75rem">' +
        '<label style="font-size:0.8125rem"><input type="radio" name="contact_method" value="email" required> {{ t.sd_guarantee_contact_email | e }}</label>' +
        '<label style="font-size:0.8125rem"><input type="radio" name="contact_method" value="phone"> {{ t.sd_guarantee_contact_phone | e }}</label>' +
        '<label style="font-size:0.8125rem"><input type="radio" name="contact_method" value="both"> {{ t.sd_guarantee_contact_both | e }}</label>' +
        '</div>' +
        '<button type="submit" style="width:100%;padding:8px;font-size:0.8125rem;font-weight:600;background:#16A34A;color:white;border:none;border-radius:8px;cursor:pointer;font-family:inherit">{{ t.sd_guarantee_submit | e }}</button>' +
        '</form></details>';
    })();
    </script>
  </div>
  {% elif _claimed_at %}
  <p style="font-size:0.75rem;color:#94A3B8;margin-top:0.5rem;text-align:center">{{ t.sd_guarantee_already_claimed }}</p>
  {% endif %}
</div>

{% if credit_cost is defined %}
{# OOB: update sidebar credits #}
<div id="sidebar-credits" hx-swap-oob="innerHTML">
  {{ supplier.credit_balance }} {{ t.sd_unlocked_credits }}
</div>
{# OOB: update header credits #}
<div id="dl-credit-balance" hx-swap-oob="innerHTML">
  {{ supplier.credit_balance }} {{ t.sd_unlocked_credits }}
  <a href="{{ url_for('suppliers.dashboard', tab='boosts') }}"
     hx-get="{{ url_for('suppliers.dashboard_boosts') }}"
     hx-target="#dashboard-content"
     hx-push-url="{{ url_for('suppliers.dashboard', tab='boosts') }}">{{ t.sd_unlocked_buy_more }}</a>
</div>
{% endif %}
