<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:vm="using:ClawdDotNet.Desktop.ViewModels"
             xmlns:ctrl="using:ClawdDotNet.Desktop.Controls"
             mc:Ignorable="d" d:DesignWidth="1100" d:DesignHeight="700"
             x:Class="ClawdDotNet.Desktop.Views.TokenUsagePageView"
             x:DataType="vm:TokenUsagePageViewModel">

  <ScrollViewer VerticalScrollBarVisibility="Auto">
    <StackPanel Spacing="20">

      <!-- Kennzahlen -->
      <StackPanel Orientation="Horizontal" Spacing="12">
        <Border Classes="card" MinWidth="160">
          <StackPanel Spacing="6">
            <TextBlock Classes="kicker" Text="TOKENS (7 TAGE)" />
            <TextBlock Classes="h2" Text="{Binding TotalTokens}" />
          </StackPanel>
        </Border>
        <Border Classes="card" MinWidth="160">
          <StackPanel Spacing="6">
            <TextBlock Classes="kicker" Text="KOSTEN (7 TAGE)" />
            <TextBlock Classes="h2" Text="{Binding TotalCost}" />
          </StackPanel>
        </Border>
        <Border Classes="card" MinWidth="160">
          <StackPanel Spacing="6">
            <TextBlock Classes="kicker" Text="RUNS (7 TAGE)" />
            <TextBlock Classes="h2" Text="{Binding TotalRuns}" />
          </StackPanel>
        </Border>

        <Button Classes="toolbar" Command="{Binding RefreshCommand}" VerticalAlignment="Bottom">
          <StackPanel Orientation="Horizontal" Spacing="6">
            <ctrl:StrokeIcon Data="{StaticResource IconRefresh}" Width="14" Height="14" />
            <TextBlock Text="Aktualisieren" />
          </StackPanel>
        </Button>
      </StackPanel>

      <!-- Hinweis, solange nichts verzeichnet ist -->
      <TextBlock Classes="caption" Text="{Binding StatusText}" TextWrapping="Wrap"
                 IsVisible="{Binding StatusText, Converter={x:Static StringConverters.IsNotNullOrEmpty}}" />

      <!-- Tagesbalken -->
      <StackPanel Spacing="12" IsVisible="{Binding !IsEmpty}">
        <TextBlock Classes="h2" Text="Tokens pro Tag" />

        <Border BorderBrush="{DynamicResource DividerBrush}" BorderThickness="0,0,0,1"
                Padding="0,0,0,2">
          <ItemsControl ItemsSource="{Binding Days7}">
            <ItemsControl.ItemsPanel>
              <ItemsPanelTemplate>
                <UniformGrid Rows="1" />
              </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>

            <ItemsControl.ItemTemplate>
              <DataTemplate DataType="vm:TokenDayItem">
                <StackPanel Spacing="6" VerticalAlignment="Bottom"
                            HorizontalAlignment="Center" Margin="8,0">
                  <TextBlock Classes="caption" Text="{Binding TokensLabel}"
                             HorizontalAlignment="Center" />
                  <Rectangle Width="28" Height="{Binding BarHeight}"
                             Fill="{DynamicResource AccentBrush}"
                             VerticalAlignment="Bottom" />
                  <TextBlock FontSize="11.5" Text="{Binding DayLabel}"
                             HorizontalAlignment="Center" />
                </StackPanel>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>
        </Border>
      </StackPanel>

      <!-- Aufschlüsselung -->
      <StackPanel Spacing="10" MaxWidth="860" HorizontalAlignment="Left"
                  IsVisible="{Binding !IsEmpty}">
        <TextBlock Classes="h2" Text="Aufschlüsselung nach Agent &amp; Modell" />

        <Border Classes="thead">
          <Grid ColumnDefinitions="2*,2*,1.2*,1.2*,0.8*">
            <TextBlock Grid.Column="0" Classes="th" Text="AGENT" />
            <TextBlock Grid.Column="1" Classes="th" Text="MODELL" />
            <TextBlock Grid.Column="2" Classes="th" Text="TOKENS" />
            <TextBlock Grid.Column="3" Classes="th" Text="KOSTEN" />
            <TextBlock Grid.Column="4" Classes="th" Text="RUNS" />
          </Grid>
        </Border>

        <ItemsControl ItemsSource="{Binding ByAgent}" Margin="0,-10,0,0">
          <ItemsControl.ItemTemplate>
            <DataTemplate DataType="vm:TokenAgentItem">
              <Border Classes="tr" Padding="7">
                <Grid ColumnDefinitions="2*,2*,1.2*,1.2*,0.8*">
                  <TextBlock Grid.Column="0" Classes="td" FontWeight="SemiBold"
                             Text="{Binding AgentName}" />
                  <TextBlock Grid.Column="1" Classes="td dim" Text="{Binding Model}" />
                  <TextBlock Grid.Column="2" Classes="td" Text="{Binding TokensLabel}" />
                  <TextBlock Grid.Column="3" Classes="td" Text="{Binding CostLabel}" />
                  <TextBlock Grid.Column="4" Classes="td" Text="{Binding Runs}" />
                </Grid>
              </Border>
            </DataTemplate>
          </ItemsControl.ItemTemplate>
        </ItemsControl>
      </StackPanel>

    </StackPanel>
  </ScrollViewer>

</UserControl>
