<div class="dashboard-wrapper">
  <div class="stats-section">
    <div class="stat-card">
      <div class="stat-label">TOTAL TIME</div>
      <div class="stat-value" data-stat="total_time"><%= ApplicationController.helpers.short_time_simple(@total_time) %></div>
    </div>

    <% dev_tool do %>
      <div class="stat-card">
        <div class="stat-label">TOTAL HEARTBEATS</div>
        <div class="stat-value" data-stat="total_heartbeats">
          <%= number_with_delimiter(@total_heartbeats) %>
        </div>
      </div>
    <% end %>

    <div class="stat-card">
      <div class="stat-label">TOP PROJECT</div>
      <div class="stat-value" data-stat="top_project">
        <%= @top_project || "None" %>
        <% if @singular_project %>
          <span class="super"><%= FlavorText.obvious.sample %></span>
        <% end %>
      </div>
    </div>

    <div class="stat-card">
      <div class="stat-label">TOP LANGUAGE</div>
      <div class="stat-value" data-stat="top_language">
        <%= display_language_name(@top_language) || "Unknown" %>
        <% if @singular_language %>
          <span class="super"><%= FlavorText.obvious.sample %></span>
        <% end %>
      </div>
    </div>

    <div class="stat-card">
      <div class="stat-label">TOP OS</div>
      <div class="stat-value" data-stat="top_operating_system">
        <%= display_os_name(@top_operating_system) || "Unknown" %>
        <% if @singular_operating_system %>
          <span class="super"><%= FlavorText.obvious.sample %></span>
        <% end %>
      </div>
    </div>

    <div class="stat-card">
      <div class="stat-label">TOP EDITOR</div>
      <div class="stat-value" data-stat="top_editor">
        <%= display_editor_name(@top_editor) || "Unknown" %>
        <% if @singular_editor %>
          <span class="super"><%= FlavorText.obvious.sample %></span>
        <% end %>
      </div>
    </div>
  </div>

  <div class="dashboard-grid">
    <% if @project_durations&.size&.> 1 %>
    <div class="card">
      <h2>Project Durations</h2>
      <div class="bar-graph">
        <%
          max_duration = @project_durations.values.max
          min_duration = @project_durations.values.min

          # Use logarithmic scale for better visibility of smaller values
          # Add 1 to avoid log(0), scale to 15-100 range
          def log_scale(value, max_val)
            return 0 if value == 0
            min_percent = 5  # Minimum bar width percentage
            max_percent = 100 # Maximum bar width percentage

            # Mix linear and logarithmic scaling
            # 80% linear, 20% logarithmic
            linear_ratio = value.to_f / max_val
            log_ratio = Math.log(value + 1) / Math.log(max_val + 1)

            linear_weight = 0.8
            log_weight = 0.2

            scaled = min_percent + (linear_weight * linear_ratio + log_weight * log_ratio) * (max_percent - min_percent)
            [scaled, max_percent].min.round
          end
        %>

        <% @project_durations.each do |project, duration| %>
          <div class="bar-row">
            <div class="bar-label"><%= h(project.presence || "Unknown") %></div>
            <div class="bar-container">
              <div class="bar" style="width: <%= log_scale(duration, max_duration) %>%">
                <span class="bar-value"><%= ApplicationController.helpers.short_time_simple(duration) %></span>
              </div>
            </div>
          </div>
        <% end %>
      </div>
    </div>
    <% end %>

    <%# Language distribution %>
    <% if @language_stats.present? %>
      <div class="card">
        <h2>Languages</h2>
        <div class="chart-container">
          <canvas id="languageChart" data-stats="<%= @language_stats.to_json %>"></canvas>
        </div>
      </div>
    <% end %>

    <%# Editor distribution %>
    <% if @editor_stats.present? %>
      <div class="card">
        <h2>Editors</h2>
        <div class="chart-container">
          <canvas id="editorChart" data-stats="<%= @editor_stats.to_json %>"></canvas>
        </div>
      </div>
    <% end %>

    <%# OS distribution %>
    <% if @operating_system_stats.present? %>
      <div class="card">
        <h2>Operating Systems</h2>
        <div class="chart-container">
          <canvas id="operatingSystemChart" data-stats="<%= @operating_system_stats.to_json %>"></canvas>
        </div>
      </div>
    <% end %>

    <div class="card">
      <h2>Project Timeline</h2>
      <div class="chart-container">
        <canvas id="projectTimelineChart" data-stats="<%= @weekly_project_stats.to_json %>"></canvas>
      </div>
    </div>
  </div>
</div>
