{% extends "_base.html" %}

{% block title %}Daily Recap — {{ _brand_name }}{% endblock %}

{% block content %}
  {#- Compute totals for AI summary -#}
  {% set ns = namespace(cluster_count=0, attention_count=0, total_savings=total_potential_savings|default(0)) %}
  {% for account in insights.data.cloud_accounts %}
    {% set insights_list = insights.data.insight | selectattr('account_id', 'equalto', account.id) | list %}
    {% set recommendations = highlight1.data.recommendation_groupings.rows | selectattr('account_id', 'equalto', account.id) | list %}
    {% set security_events = insights.data.recommendation_security_groupings_v2.rows | selectattr('account_id', 'equalto', account.id) | list %}
    {% set current_app_event_row = highlight1.data.app_events.rows | selectattr('account_id', 'equalto', account.id) | first %}
    {% set current_node_event_row = highlight1.data.node_events.rows | selectattr('account_id', 'equalto', account.id) | first %}
    {% set current_pod_event_row = highlight1.data.pod_events.rows | selectattr('account_id', 'equalto', account.id) | first %}
    {% set ev_total = (current_app_event_row.event_count if current_app_event_row else 0) + (current_node_event_row.event_count if current_node_event_row else 0) + (current_pod_event_row.event_count if current_pod_event_row else 0) %}
    {% set has_any_data = (ev_total > 0) or (recommendations | length > 0) or (security_events | length > 0) %}
    {% if has_any_data %}
      {% set ns.cluster_count = ns.cluster_count + 1 %}
      {% if (insights_list | length + recommendations | length + security_events | length + ev_total) > 0 %}
        {% set ns.attention_count = ns.attention_count + 1 %}
      {% endif %}
    {% endif %}
  {% endfor %}

  <!-- AI SUMMARY OPENING -->
  <h1 class="subject-h" style="font-family:'DM Serif Display',Georgia,serif; font-size:26px; color:#0D1B2A; line-height:1.25; margin:0 0 20px; letter-spacing:-0.3px; font-weight:400;">
    Daily Recap
  </h1>

  <p style="font-family:'DM Sans',Arial,sans-serif; font-size:15.5px; color:#3D3D3D; line-height:1.85; font-weight:300; margin:0 0 20px;">
    I reviewed <strong style="color:#0D1B2A;">{{ ns.cluster_count }}</strong> cluster{{ 's' if ns.cluster_count != 1 else '' }} overnight.
    {% if ns.attention_count > 0 %}
      <strong style="color:#0D1B2A;">{{ ns.attention_count }}</strong> need{{ 's' if ns.attention_count == 1 else '' }} your attention
    {% else %}
      Everything looks healthy
    {% endif %}
    {% if ns.total_savings > 0 %}
      and there's <strong style="color:#22C55E;">${{ ns.total_savings | round(2) }}/mo</strong> in savings you haven't acted on yet{% if total_opportunity_lost and total_opportunity_lost > 0 %} (<strong style="color:#EF4444;">${{ total_opportunity_lost | round(2) }}</strong> lost in the last 30 days){% endif %}.
    {% else %}.{% endif %}
  </p>

  <!-- OVERVIEW LABEL -->
  <div style="font-family:'DM Sans',Arial,sans-serif; font-size:11px; font-weight:600; color:#6B7280; letter-spacing:0.8px; text-transform:uppercase; margin:24px 0 8px;">
    OVERVIEW
  </div>

  <!-- OVERVIEW TABLE (data table — proper th headers) -->
  <table style="border-collapse:collapse; border-spacing:0; width:100%; font-family:'DM Sans',Arial,sans-serif;">
    <thead>
      <tr style="background:#F8F9FA;">
        <th style="padding:10px 12px; text-align:left; font-size:12px; font-weight:600; color:#6B7280; border-bottom:1px solid #F0F0F0;">Cluster</th>
        <th style="padding:10px 12px; text-align:center; font-size:12px; font-weight:600; color:#6B7280; border-bottom:1px solid #F0F0F0;">Events</th>
        <th style="padding:10px 12px; text-align:center; font-size:12px; font-weight:600; color:#6B7280; border-bottom:1px solid #F0F0F0;">Savings</th>
        <th style="padding:10px 12px; text-align:center; font-size:12px; font-weight:600; color:#6B7280; border-bottom:1px solid #F0F0F0;">Security</th>
        <th style="padding:10px 12px; text-align:center; font-size:12px; font-weight:600; color:#6B7280; border-bottom:1px solid #F0F0F0;">&nbsp;</th>
      </tr>
    </thead>
    <tbody>
      {% for account in insights.data.cloud_accounts %}
        {% set c_app = (highlight1.data.app_events.rows | selectattr('account_id', 'equalto', account.id) | first) %}
        {% set c_node = (highlight1.data.node_events.rows | selectattr('account_id', 'equalto', account.id) | first) %}
        {% set c_pod = (highlight1.data.pod_events.rows | selectattr('account_id', 'equalto', account.id) | first) %}
        {% set ev_count = (c_app.event_count if c_app else 0) + (c_node.event_count if c_node else 0) + (c_pod.event_count if c_pod else 0) %}
        {% set recs = highlight1.data.recommendation_groupings.rows | selectattr('account_id', 'equalto', account.id) | list %}
        {% set cluster_savings = recs | map(attribute='sum_estimated_savings') | sum %}
        {% set sec_events = insights.data.recommendation_security_groupings_v2.rows | selectattr('account_id', 'equalto', account.id) | list %}
        {% set sec_critical = sec_events | map(attribute='count_severity_critical') | sum %}
        {% set sec_high = sec_events | map(attribute='count_severity_high') | sum %}
        {% set has_data = (ev_count > 0) or (recs | length > 0) or (sec_events | length > 0) %}
        {% if has_data %}
      <tr style="border-bottom:1px solid #F0F0F0;">
        <td style="padding:10px 12px; font-size:13px; color:#0D1B2A; font-weight:500;">
          <a href="{{ base_url }}/kubernetes/details/{{ account.id }}" style="color:#0D1B2A; text-decoration:none;">{{ account.account_name }}</a>
        </td>
        <td style="padding:10px 12px; text-align:center; font-size:13px; font-weight:500; color:{% if ev_count > 20 %}#EF4444{% elif ev_count > 5 %}#F59E0B{% else %}#6B7280{% endif %};">
          {{ ev_count }}
        </td>
        <td style="padding:10px 12px; text-align:center; font-size:13px; font-weight:500; color:#22C55E;">
          {% if cluster_savings > 0 %}${{ cluster_savings | round(2) }}{% else %}&mdash;{% endif %}
        </td>
        <td style="padding:10px 12px; text-align:center; font-size:13px;">
          {% if sec_critical > 0 or sec_high > 0 %}
            <span style="color:#EF4444; font-weight:500;">{{ sec_critical }}C</span>
            <span style="color:#6B7280;">/</span>
            <span style="color:#F59E0B; font-weight:500;">{{ sec_high }}H</span>
          {% else %}
            <span style="color:#22C55E;">&#10003;</span>
          {% endif %}
        </td>
        <td style="padding:10px 12px; text-align:center;">
          <a href="{{ base_url }}/kubernetes/details/{{ account.id }}?utm=email"
            style="font-family:'DM Sans',Arial,sans-serif; font-size:12px; font-weight:500; color:#3B82F6; text-decoration:none;">
            View &rarr;
          </a>
        </td>
      </tr>
        {% endif %}
      {% endfor %}
    </tbody>
  </table>

  <!-- DETAIL SECTIONS — only clusters with data -->
  {% for account in insights.data.cloud_accounts %}
    {% set current_app_event_row = highlight1.data.app_events.rows | selectattr('account_id', 'equalto', account.id) | first %}
    {% set previous_app_event_row = highlight2.data.app_events.rows | selectattr('account_id', 'equalto', account.id) | first %}
    {% set current_app_events = current_app_event_row.event_count if current_app_event_row else 0 %}
    {% set previous_app_events = previous_app_event_row.event_count if previous_app_event_row else 0 %}

    {% set current_node_event_row = highlight1.data.node_events.rows | selectattr('account_id', 'equalto', account.id) | first %}
    {% set previous_node_event_row = highlight2.data.node_events.rows | selectattr('account_id', 'equalto', account.id) | first %}
    {% set current_node_events = current_node_event_row.event_count if current_node_event_row else 0 %}
    {% set previous_node_events = previous_node_event_row.event_count if previous_node_event_row else 0 %}

    {% set current_pod_event_row = highlight1.data.pod_events.rows | selectattr('account_id', 'equalto', account.id) | first %}
    {% set previous_pod_event_row = highlight2.data.pod_events.rows | selectattr('account_id', 'equalto', account.id) | first %}
    {% set current_pod_events = current_pod_event_row.event_count if current_pod_event_row else 0 %}
    {% set previous_pod_events = previous_pod_event_row.event_count if previous_pod_event_row else 0 %}

    {% set total_highlights = current_app_events + current_node_events + current_pod_events %}
    {% set recommendations = highlight1.data.recommendation_groupings.rows | selectattr('account_id', 'equalto', account.id) | list %}
    {% set total_recommendations = recommendations | length %}
    {% set insights_list = insights.data.insight | selectattr('account_id', 'equalto', account.id) | rejectattr('type', 'equalto', 'Optimization') | list %}
    {% set total_insights = insights_list | length %}
    {% set security_events = insights.data.recommendation_security_groupings_v2.rows | selectattr('account_id', 'equalto', account.id) | list %}
    {% set total_security_events = security_events | length %}
    {% set total_data = total_highlights + total_recommendations + total_insights + total_security_events %}

    {% if total_data > 0 %}
    <!-- Cluster divider -->
    <div style="border-top:2px dotted #D1D5DB; margin:28px 0 0;"></div>

    <!-- Cluster heading -->
    <h2 style="font-family:'DM Serif Display',Georgia,serif; font-size:20px; color:#0D1B2A; font-weight:400; letter-spacing:-0.3px; margin:20px 0 16px;">
      <a href="{{ base_url }}/kubernetes/details/{{ account.id }}" style="color:#0D1B2A; text-decoration:none;">{{ account.account_name }}</a>
    </h2>

    {#- INSIGHTS -#}
    {% if total_insights > 0 %}
    <div style="font-family:'DM Sans',Arial,sans-serif; font-size:11px; font-weight:600; color:#6B7280; letter-spacing:0.8px; text-transform:uppercase; margin:0 0 6px;">
      INSIGHTS
    </div>
    {% for insight in insights_list %}
    <p style="font-family:'DM Sans',Arial,sans-serif; font-size:13.5px; color:#3D3D3D; line-height:1.6; margin:0 0 4px; padding-left:16px;">
      <span style="color:#F59E0B; margin-right:6px;">&#8226;</span>
      {% if insight.type == 'Troubleshooting' %}
        {{ insight.title }} detected for ({{ insight.applications | length }}) workloads
      {% else %}
        {{ insight.title }}
      {% endif %}
    </p>
    {% endfor %}
    {% endif %}

    {#- RECOMMENDED ACTIONS -#}
    {% if total_recommendations > 0 %}
    <div style="font-family:'DM Sans',Arial,sans-serif; font-size:11px; font-weight:600; color:#6B7280; letter-spacing:0.8px; text-transform:uppercase; margin:12px 0 6px;">
      RECOMMENDED ACTIONS
    </div>
    {% set sorted_recommendations = recommendations | sort(attribute='sum_estimated_savings', reverse=True) %}
    {% for recommendation in sorted_recommendations[:5] %}
      {% set savings = recommendation.sum_estimated_savings %}
      {% set rule = recommendation.rule_name %}
      {% if rule == 'eks_cluster_upgrade' %}
        {% set link_hash = 'security/cluster-upgrade' %}
      {% elif rule == 'unused_pvc' %}
        {% set link_hash = 'optimize/unused-volume' %}
      {% elif rule == 'pod_right_sizing' %}
        {% set link_hash = 'optimize/right-sizing' %}
      {% elif rule == 'pv_rightsize' %}
        {% set link_hash = 'optimize/pv-rightsizing' %}
      {% elif rule == 'replica-rightsizing' %}
        {% set link_hash = 'optimize/replica-rightsizing' %}
      {% elif rule == 'abandoned-resources' %}
        {% set link_hash = 'optimize/abandoned-resources' %}
      {% else %}
        {% set link_hash = 'optimize/summary' %}
      {% endif %}
      <p style="font-family:'DM Sans',Arial,sans-serif; font-size:13.5px; color:#3D3D3D; line-height:1.6; margin:0 0 4px; padding-left:16px;">
        <span style="color:#3B82F6; margin-right:6px;">&#8226;</span>
        <strong>{{ recommendation.count }}</strong>
        <a href="{{ base_url }}/kubernetes/details/{{ account.id }}?accountId={{ account.id }}&amp;utm=email#{{ link_hash }}"
          style="color:#3B82F6; text-decoration:none; font-weight:500;">
          {{ rule.replace('_', ' ') }}
        </a>
        {% if savings > 0 %}
          — save <strong style="color:#22C55E;">${{ savings | round(2) }}/mo</strong>
        {% endif %}
        &nbsp;
        <a href="{{ base_url }}/kubernetes/details/{{ account.id }}?accountId={{ account.id }}&amp;utm=email#{{ link_hash }}"
          style="display:inline-block; background-color:#DBEAFE; color:#3B82F6; text-decoration:none; padding:2px 8px; border-radius:4px; font-size:11px; font-weight:500;">
          {% if 'unused' in rule or 'abandoned' in rule %}Clean up{% elif 'right' in rule or 'replica' in rule %}Apply{% else %}Review{% endif %} &rarr;
        </a>
      </p>
    {% endfor %}
    {% endif %}

    {#- HIGHLIGHTS -#}
    {% if total_highlights > 0 %}
    <div style="font-family:'DM Sans',Arial,sans-serif; font-size:11px; font-weight:600; color:#6B7280; letter-spacing:0.8px; text-transform:uppercase; margin:12px 0 6px;">
      HIGHLIGHTS
    </div>
      {% if current_app_events > 0 %}
      <p style="font-family:'DM Sans',Arial,sans-serif; font-size:13.5px; color:#3D3D3D; line-height:1.6; margin:0 0 4px; padding-left:16px;">
        <span style="color:#6B7280; margin-right:6px;">&#8226;</span>
        <a href="{{ base_url }}/kubernetes/details/{{ account.id }}?accountId={{ account.id }}&amp;utm=email#events/app-errors" style="color:#3B82F6; text-decoration:none;">Application Errors</a>: {{ current_app_events }}
        {% set diff_app_events = previous_app_events - current_app_events %}
        {% if previous_app_events == 0 %}
          (-)
        {% elif diff_app_events > 0 %}
          (<span style="color:#22C55E;">&#9660; {{ ((diff_app_events / previous_app_events) * 100) | round(0) }}%</span>)
        {% elif diff_app_events < 0 %}
          (<span style="color:#EF4444;">&#9650; {{ ((-diff_app_events / previous_app_events) * 100) | round(0) }}%</span>)
        {% else %}
          (-)
        {% endif %}
      </p>
      {% endif %}
      {% if current_node_events > 0 %}
      <p style="font-family:'DM Sans',Arial,sans-serif; font-size:13.5px; color:#3D3D3D; line-height:1.6; margin:0 0 4px; padding-left:16px;">
        <span style="color:#6B7280; margin-right:6px;">&#8226;</span>
        <a href="{{ base_url }}/kubernetes/details/{{ account.id }}?accountId={{ account.id }}&amp;utm=email#events/node-errors" style="color:#3B82F6; text-decoration:none;">Node Errors</a>: {{ current_node_events }}
        {% set diff_node_events = previous_node_events - current_node_events %}
        {% if previous_node_events == 0 %}
          (-)
        {% elif diff_node_events > 0 %}
          (<span style="color:#22C55E;">&#9660; {{ ((diff_node_events / previous_node_events) * 100) | round(0) }}%</span>)
        {% elif diff_node_events < 0 %}
          (<span style="color:#EF4444;">&#9650; {{ ((-diff_node_events / previous_node_events) * 100) | round(0) }}%</span>)
        {% else %}
          (-)
        {% endif %}
      </p>
      {% endif %}
      {% if current_pod_events > 0 %}
      <p style="font-family:'DM Sans',Arial,sans-serif; font-size:13.5px; color:#3D3D3D; line-height:1.6; margin:0 0 4px; padding-left:16px;">
        <span style="color:#6B7280; margin-right:6px;">&#8226;</span>
        <a href="{{ base_url }}/kubernetes/details/{{ account.id }}?accountId={{ account.id }}&amp;utm=email#events/pod-errors" style="color:#3B82F6; text-decoration:none;">Pod Errors</a>: {{ current_pod_events }}
        {% set diff_pod_events = previous_pod_events - current_pod_events %}
        {% if previous_pod_events == 0 %}
          (-)
        {% elif diff_pod_events > 0 %}
          (<span style="color:#22C55E;">&#9660; {{ ((diff_pod_events / previous_pod_events) * 100) | round(0) }}%</span>)
        {% elif diff_pod_events < 0 %}
          (<span style="color:#EF4444;">&#9650; {{ ((-diff_pod_events / previous_pod_events) * 100) | round(0) }}%</span>)
        {% else %}
          (-)
        {% endif %}
      </p>
      {% endif %}
    {% endif %}

    {#- SECURITY -#}
    {% if total_security_events > 0 %}
    <div style="font-family:'DM Sans',Arial,sans-serif; font-size:11px; font-weight:600; color:#6B7280; letter-spacing:0.8px; text-transform:uppercase; margin:12px 0 6px;">
      SECURITY
    </div>
    {% for security_event in security_events %}
    <p style="font-family:'DM Sans',Arial,sans-serif; font-size:13.5px; color:#3D3D3D; line-height:1.6; margin:0 0 4px; padding-left:16px;">
      <span style="color:#EF4444; margin-right:6px;">&#8226;</span>
      <span style="color:#EF4444; font-weight:600;">{{ security_event.count_severity_critical }} Critical</span>,
      <span style="color:#F59E0B; font-weight:600;">{{ security_event.count_severity_high }} High</span>,
      {{ security_event.count_severity_medium }} Medium,
      {{ security_event.count_severity_low }} Low
      across <strong>{{ security_event.count_image }}</strong> images
      &nbsp;
      <a href="{{ base_url }}/kubernetes/details/{{ account.id }}?accountId={{ account.id }}&amp;utm=email#security/vulnerabilities"
        style="display:inline-block; background-color:#FEF2F2; color:#EF4444; text-decoration:none; padding:2px 8px; border-radius:4px; font-size:11px; font-weight:500;">
        Fix now &rarr;
      </a>
    </p>
    {% endfor %}
    {% endif %}

    {% endif %}
  {% endfor %}
{% endblock %}
