<Window x:Class="PerformanceMonitorLite.Windows.QueryStatsHistoryWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:scottplot="clr-namespace:ScottPlot.WPF;assembly=ScottPlot.WPF"
        Title="Query Stats History" Width="1400" Height="800"
        WindowStartupLocation="CenterOwner"
        TextOptions.TextFormattingMode="Display"
        UseLayoutRounding="True"
        Background="{DynamicResource BackgroundBrush}">

    <Window.Resources>
        <ResourceDictionary>
            <ContextMenu x:Key="DataGridContextMenu">
                <MenuItem Header="Copy Cell" Click="CopyCell_Click">
                    <MenuItem.Icon><TextBlock Text="&#x1F4CB;"/></MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Copy Row" Click="CopyRow_Click">
                    <MenuItem.Icon><TextBlock Text="&#x1F4C4;"/></MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Copy All Rows" Click="CopyAllRows_Click">
                    <MenuItem.Icon><TextBlock Text="&#x1F4D1;"/></MenuItem.Icon>
                </MenuItem>
                <Separator/>
                <MenuItem Header="Export to CSV..." Click="ExportToCsv_Click">
                    <MenuItem.Icon><TextBlock Text="&#x1F4CA;"/></MenuItem.Icon>
                </MenuItem>
                <Separator/>
                <MenuItem Header="View Plan" Click="ViewPlan_Click">
                    <MenuItem.Icon><TextBlock Text="&#x1F50D;"/></MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Get Actual Plan" Click="GetActualPlan_Click">
                    <MenuItem.Icon><TextBlock Text="&#x25B6;"/></MenuItem.Icon>
                </MenuItem>
            </ContextMenu>
        </ResourceDictionary>
    </Window.Resources>

    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="250"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- Header -->
        <StackPanel Grid.Row="0" Margin="0,0,0,10">
            <TextBlock x:Name="QueryIdentifierText" FontSize="16" FontWeight="Bold"
                       Foreground="{DynamicResource ForegroundBrush}"/>
            <TextBlock x:Name="SummaryText" FontSize="12" Margin="0,4,0,0"
                       Foreground="{DynamicResource ForegroundDimBrush}"/>
        </StackPanel>

        <!-- Chart -->
        <Border Grid.Row="1" Background="{DynamicResource BackgroundLightBrush}"
                BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" CornerRadius="3">
            <Grid>
                <ComboBox x:Name="MetricSelector" Width="180" Height="26"
                          VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,6,10,0"
                          SelectionChanged="MetricSelector_SelectionChanged">
                    <ComboBoxItem Content="Avg CPU (ms)" Tag="AvgCpuMs" IsSelected="True"/>
                    <ComboBoxItem Content="Avg Duration (ms)" Tag="AvgElapsedMs"/>
                    <ComboBoxItem Content="Avg Logical Reads" Tag="AvgReads"/>
                    <ComboBoxItem Content="Execution Count Delta" Tag="DeltaExecutions"/>
                    <ComboBoxItem Content="Total CPU (ms)" Tag="DeltaCpuMs"/>
                    <ComboBoxItem Content="Total Logical Reads" Tag="DeltaLogicalReads"/>
                    <ComboBoxItem Content="Total Spills" Tag="TotalSpills"/>
                </ComboBox>
                <scottplot:WpfPlot x:Name="HistoryChart" Margin="0,0,0,0"/>
            </Grid>
        </Border>

        <!-- Data Grid -->
        <DataGrid Grid.Row="2" x:Name="HistoryDataGrid" Margin="0,10,0,0"
                  AutoGenerateColumns="False" IsReadOnly="True"
                  CanUserSortColumns="True" CanUserReorderColumns="True"
                  HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
                  ContextMenu="{StaticResource DataGridContextMenu}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Collection Time" Binding="{Binding CollectionTimeLocal}" Width="130"/>
                <DataGridTextColumn Header="Exec Delta" Binding="{Binding DeltaExecutions, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="90"/>
                <DataGridTextColumn Header="CPU Delta (ms)" Binding="{Binding DeltaCpuMs, StringFormat=N1}" ElementStyle="{StaticResource NumericCell}" Width="110"/>
                <DataGridTextColumn Header="Duration Delta (ms)" Binding="{Binding DeltaElapsedMs, StringFormat=N1}" ElementStyle="{StaticResource NumericCell}" Width="130"/>
                <DataGridTextColumn Header="Avg CPU (ms)" Binding="{Binding AvgCpuMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="100"/>
                <DataGridTextColumn Header="Avg Duration (ms)" Binding="{Binding AvgElapsedMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="120"/>
                <DataGridTextColumn Header="Logical Reads" Binding="{Binding DeltaLogicalReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="100"/>
                <DataGridTextColumn Header="Avg Reads" Binding="{Binding AvgReads, StringFormat=N1}" ElementStyle="{StaticResource NumericCell}" Width="90"/>
                <DataGridTextColumn Header="Rows" Binding="{Binding DeltaRows, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="80"/>
                <DataGridTextColumn Header="Writes" Binding="{Binding DeltaLogicalWrites, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="80"/>
                <DataGridTextColumn Header="Physical Reads" Binding="{Binding DeltaPhysicalReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="100"/>
                <DataGridTextColumn Header="Spills" Binding="{Binding DeltaSpills, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="70"/>
                <DataGridTextColumn Header="Min CPU (ms)" Binding="{Binding MinCpuMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="100"/>
                <DataGridTextColumn Header="Max CPU (ms)" Binding="{Binding MaxCpuMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="100"/>
                <DataGridTextColumn Header="Min Duration (ms)" Binding="{Binding MinElapsedMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="120"/>
                <DataGridTextColumn Header="Max Duration (ms)" Binding="{Binding MaxElapsedMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="120"/>
                <DataGridTextColumn Header="Min DOP" Binding="{Binding MinDop}" Width="70"/>
                <DataGridTextColumn Header="Max DOP" Binding="{Binding MaxDop}" Width="70"/>
                <DataGridTextColumn Header="Plan Hash" Binding="{Binding QueryPlanHash}" Width="140"/>
                <DataGridTemplateColumn Header="Plan" Width="Auto" MinWidth="80">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Download" Click="DownloadPlan_Click"
                                    HorizontalAlignment="Center" VerticalAlignment="Center"
                                    Padding="6,2"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

        <!-- Footer -->
        <StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,0,0">
            <Button Content="Close" Padding="20,6" Click="Close_Click"/>
        </StackPanel>
    </Grid>
</Window>
