<%# locals: (investment_metrics:) %> <% if investment_metrics[:has_investments] %>
<%# Investment Summary Cards %>
<%# Portfolio Value Card %>
<%= icon("briefcase", size: "sm") %> <%= t("reports.investment_performance.portfolio_value") %>

<%= format_money(investment_metrics[:portfolio_value]) %>

<%# Total Return Card %>
<%= icon("trending-up", size: "sm") %> <%= t("reports.investment_performance.total_return") %>
<% if investment_metrics[:unrealized_trend] %>

<%= format_money(Money.new(investment_metrics[:unrealized_trend].value, Current.family.currency)) %> (<%= investment_metrics[:unrealized_trend].percent_formatted %>)

<% else %>

<%= t("reports.investment_performance.no_data") %>

<% end %>
<%# Period Contributions Card %>
<%= icon("arrow-down-to-line", size: "sm") %> <%= t("reports.investment_performance.contributions") %>

<%= format_money(investment_metrics[:period_contributions]) %>

<%# Period Withdrawals Card %>
<%= icon("arrow-up-from-line", size: "sm") %> <%= t("reports.investment_performance.withdrawals") %>

<%= format_money(investment_metrics[:period_withdrawals]) %>

<%# Top Holdings Table %> <% if investment_metrics[:top_holdings].any? %>

<%= t("reports.investment_performance.top_holdings") %>

<%= t("reports.investment_performance.holding") %>
<%= t("reports.investment_performance.weight") %>
<%= t("reports.investment_performance.value") %>
<%= t("reports.investment_performance.return") %>
<% investment_metrics[:top_holdings].each_with_index do |holding, idx| %>
<% if holding.security.brandfetch_icon_url.present? %> <%= holding.ticker %> <% elsif holding.security.logo_url.present? %> <%= holding.ticker %> <% else %>
<%= holding.ticker[0..1] %>
<% end %>

<%= holding.ticker %>

<%= truncate(holding.name, length: 25) %>

<%= number_to_percentage(holding.weight || 0, precision: 1) %>
<%= format_money(holding.amount_money) %>
<% if holding.trend %> <%= holding.trend.percent_formatted %> <% else %> <%= t("reports.investment_performance.no_data") %> <% end %>
<% if idx < investment_metrics[:top_holdings].size - 1 %> <%= render "shared/ruler", classes: "mx-3 lg:mx-4" %> <% end %> <% end %>
<% end %> <%# Gains by Tax Treatment %> <% if investment_metrics[:gains_by_tax_treatment].present? %>

<%= t("reports.investment_performance.gains_by_tax_treatment") %>

<% investment_metrics[:gains_by_tax_treatment].each do |treatment, data| %>
"> <%= t("accounts.tax_treatments.#{treatment}") %> <%= format_money(data[:total_gain]) %>
<%= t("reports.investment_performance.unrealized_gains") %> <%= format_money(data[:unrealized_gain]) %>
<%= t("reports.investment_performance.realized_gains") %> <%= format_money(data[:realized_gain]) %>
<% if treatment == :taxable && data[:realized_gain].amount > 0 %>

<%= icon("alert-triangle", size: "sm") %> <%= t("reports.investment_performance.taxable_realized_note") %>

<% end %>
<%= icon "chevron-right", size: "sm", class: "group-open:rotate-90 transition-transform" %> <%= t("reports.investment_performance.view_details") %> (<%= t("reports.investment_performance.holdings_count", count: data[:holdings].count) %>, <%= t("reports.investment_performance.sells_count", count: data[:sell_trades].count) %>)
<% if data[:holdings].any? %>

<%= t("reports.investment_performance.holdings") %>

<% data[:holdings].first(5).each do |holding| %>
<%= holding.ticker %> " style="<%= holding.trend ? "color: #{holding.trend.color}" : "" %>"> <%= holding.trend ? format_money(Money.new(holding.trend.value, Current.family.currency)) : t("reports.investment_performance.no_data") %>
<% end %> <% if data[:holdings].count > 5 %>

<%= t("reports.investment_performance.and_more", count: data[:holdings].count - 5) %>

<% end %>
<% end %> <% if data[:sell_trades].any? %>

<%= t("reports.investment_performance.sell_trades") %>

<% data[:sell_trades].first(5).each do |trade| %> <% gain = trade.realized_gain_loss %>
<%= trade.security.ticker %> " style="<%= gain ? "color: #{gain.color}" : "" %>"> <%= gain ? format_money(Money.new(gain.value, Current.family.currency)) : t("reports.investment_performance.no_data") %>
<% end %> <% if data[:sell_trades].count > 5 %>

<%= t("reports.investment_performance.and_more", count: data[:sell_trades].count - 5) %>

<% end %>
<% end %>
<% end %>
<% end %> <%# Investment Accounts Summary %> <% if investment_metrics[:accounts].any? %>

<%= t("reports.investment_performance.accounts") %>

<% investment_metrics[:accounts].each do |account| %> <%= link_to account_path(account), class: "bg-container-inset rounded-lg p-4 flex items-center justify-between hover:bg-container-hover transition-colors" do %>
<%= render "accounts/logo", account: account, size: "sm" %>

<%= account.name %>

<%= account.short_subtype_label %>

<%= format_money(account.balance_money) %>

<% end %> <% end %>
<% end %>
<% end %>