<div>
  <%# Export Controls %>
  <div class="flex items-center justify-end mb-4 flex-wrap gap-3">
    <%
      # Build params hash for links
      base_params = {
        period_type: period_type,
        start_date: start_date,
        end_date: end_date,
        sort_by: params[:sort_by],
        sort_direction: params[:sort_direction]
      }.compact
    %>

    <%# Export Options %>
    <div class="flex items-center gap-2">
      <%= link_to export_transactions_reports_path(base_params.merge(format: :csv)),
                  class: "font-medium whitespace-nowrap inline-flex items-center gap-1 rounded-lg px-3 py-2 text-sm text-primary border border-secondary bg-transparent hover:bg-surface-hover" do %>
        <%= icon("download", class: "w-3 h-3") %>
        <span><%= t("reports.transactions_breakdown.export.csv") %></span>
      <% end %>
      <%= link_to google_sheets_instructions_reports_path(base_params),
                  class: "font-medium whitespace-nowrap inline-flex items-center gap-1 rounded-lg px-3 py-2 text-sm text-primary border border-secondary bg-transparent hover:bg-surface-hover",
                  data: { turbo_frame: "modal" } do %>
        <%= icon("external-link", class: "w-3 h-3") %>
        <span><%= t("reports.transactions_breakdown.export.google_sheets") %></span>
      <% end %>
    </div>
  </div>

  <%# Transactions Tables - Split by Income and Expenses %>
  <% if transactions.any? %>
    <%
      # Separate income and expenses
      income_groups = transactions.select { |g| g[:type] == "income" }
      expense_groups = transactions.select { |g| g[:type] == "expense" }

      # Calculate totals
      income_total = income_groups.sum { |g| g[:total] }
      expense_total = expense_groups.sum { |g| g[:total] }

      # Determine sort direction for Amount column
      current_sort_by = params[:sort_by]
      current_sort_direction = params[:sort_direction]

      # Toggle sort direction: if currently sorting by amount desc, switch to asc; otherwise default to desc
      next_sort_direction = (current_sort_by == "amount" && current_sort_direction == "desc") ? "asc" : "desc"

      # Build params for amount sort link
      amount_sort_params = base_params.merge(sort_by: "amount", sort_direction: next_sort_direction)
    %>

    <div class="space-y-8">
      <%# Income Section %>
      <% if income_groups.any? %>
        <%= render "reports/breakdown_table",
              groups: income_groups,
              total: income_total,
              type: :income,
              amount_sort_params: amount_sort_params,
              current_sort_by: current_sort_by,
              current_sort_direction: current_sort_direction %>
      <% end %>

      <%# Expenses Section %>
      <% if expense_groups.any? %>
        <%= render "reports/breakdown_table",
              groups: expense_groups,
              total: expense_total,
              type: :expense,
              amount_sort_params: amount_sort_params,
              current_sort_by: current_sort_by,
              current_sort_direction: current_sort_direction %>
      <% end %>
    </div>

    <%# Summary Stats %>
    <div class="mt-4 text-xs text-subdued">
      <%= t("reports.transactions_breakdown.pagination.showing", count: transactions.sum { |g| g[:count] }) %>
    </div>
  <% else %>
    <div class="text-center py-8 text-subdued">
      <%= t("reports.transactions_breakdown.no_transactions") %>
    </div>
  <% end %>
</div>
