<Window x:Class="PerformanceMonitorLite.Windows.QueryStoreHistoryWindow"
        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 Store History" Width="1200" 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>
            </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="AvgCpuTimeMs" IsSelected="True"/>
                    <ComboBoxItem Content="Avg Duration (ms)" Tag="AvgDurationMs"/>
                    <ComboBoxItem Content="Avg Logical Reads" Tag="AvgLogicalReads"/>
                    <ComboBoxItem Content="Avg Rows" Tag="AvgRowcount"/>
                    <ComboBoxItem Content="Execution Count" Tag="ExecutionCount"/>
                    <ComboBoxItem Content="Total CPU (ms)" Tag="TotalCpuMs"/>
                    <ComboBoxItem Content="Total Duration (ms)" Tag="TotalDurationMs"/>
                </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="Executions" Binding="{Binding ExecutionCount, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="90"/>
                <DataGridTextColumn Header="Total Duration (ms)" Binding="{Binding TotalDurationMs, StringFormat=N1}" ElementStyle="{StaticResource NumericCell}" Width="130"/>
                <DataGridTextColumn Header="Avg Duration (ms)" Binding="{Binding AvgDurationMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="120"/>
                <DataGridTextColumn Header="Total CPU (ms)" Binding="{Binding TotalCpuMs, StringFormat=N1}" ElementStyle="{StaticResource NumericCell}" Width="110"/>
                <DataGridTextColumn Header="Avg CPU (ms)" Binding="{Binding AvgCpuTimeMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="100"/>
                <DataGridTextColumn Header="Avg Reads" Binding="{Binding AvgLogicalReads, StringFormat=N1}" ElementStyle="{StaticResource NumericCell}" Width="90"/>
                <DataGridTextColumn Header="Avg Writes" Binding="{Binding AvgLogicalWrites, StringFormat=N1}" ElementStyle="{StaticResource NumericCell}" Width="90"/>
                <DataGridTextColumn Header="Avg Physical Reads" Binding="{Binding AvgPhysicalReads, StringFormat=N1}" ElementStyle="{StaticResource NumericCell}" Width="120"/>
                <DataGridTextColumn Header="Avg Rows" Binding="{Binding AvgRowcount, StringFormat=N1}" ElementStyle="{StaticResource NumericCell}" Width="90"/>
                <DataGridTextColumn Header="Last Execution" Binding="{Binding LastExecutionTimeLocal}" Width="130"/>
                <DataGridTextColumn Header="Min DOP" Binding="{Binding MinDop}" Width="70"/>
                <DataGridTextColumn Header="Max DOP" Binding="{Binding MaxDop}" Width="70"/>
                <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>
