<Window x:Class="PerformanceMonitorDashboard.QueryExecutionHistoryWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:converters="clr-namespace:PerformanceMonitorDashboard.Converters"
        xmlns:ScottPlot="clr-namespace:ScottPlot.WPF;assembly=ScottPlot.WPF"
        Title="Query Execution History" Height="900" Width="1600"
        WindowStartupLocation="CenterOwner"
        Background="{DynamicResource BackgroundBrush}">
    <Window.Resources>
        <converters:NullToBooleanConverter x:Key="NullToBooleanConverter"/>

        <!-- Context Menu for DataGrid Copy/Export -->
        <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>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="3*" MinHeight="300"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- Header -->
        <Border Grid.Row="0" Background="{DynamicResource BackgroundLightBrush}" Padding="10" Margin="10,10,10,0">
            <StackPanel>
                <TextBlock x:Name="QueryIdentifierText" FontSize="16" FontWeight="Bold" Foreground="{DynamicResource ForegroundBrush}"/>
                <TextBlock x:Name="SummaryText" Margin="0,5,0,0" Foreground="{DynamicResource ForegroundBrush}"/>
            </StackPanel>
        </Border>

        <!-- Chart Area -->
        <Border Grid.Row="1" Background="{DynamicResource BackgroundLightBrush}" Margin="10,10,10,0" Padding="10">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <!-- Chart Controls -->
                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="0,0,0,10">
                    <TextBlock Text="Metric:" VerticalAlignment="Center" Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,0"/>
                    <ComboBox x:Name="MetricSelector" Width="200" SelectionChanged="MetricSelector_SelectionChanged">
                        <ComboBoxItem Content="Avg Duration (ms)" Tag="AvgDurationMs" IsSelected="True"/>
                        <ComboBoxItem Content="Avg CPU (ms)" Tag="AvgCpuTimeMs"/>
                        <ComboBoxItem Content="Avg Logical Reads" Tag="AvgLogicalReads"/>
                        <ComboBoxItem Content="Avg Logical Writes" Tag="AvgLogicalWrites"/>
                        <ComboBoxItem Content="Avg Physical Reads" Tag="AvgPhysicalReads"/>
                        <ComboBoxItem Content="Avg Memory (MB)" Tag="AvgMemoryMb"/>
                        <ComboBoxItem Content="Avg Rows" Tag="AvgRowcount"/>
                        <ComboBoxItem Content="Executions" Tag="CountExecutions"/>
                    </ComboBox>
                    <TextBlock x:Name="ChartLegendText" Margin="20,0,0,0" VerticalAlignment="Center" Foreground="{DynamicResource ForegroundDimBrush}" FontSize="11"/>
                </StackPanel>

                <!-- Chart -->
                <ScottPlot:WpfPlot x:Name="HistoryChart" Grid.Row="1"/>
            </Grid>
        </Border>

        <!-- Data Grid -->
        <DataGrid Grid.Row="2" x:Name="HistoryDataGrid"
                  AutoGenerateColumns="False"
                  IsReadOnly="True"
                  CanUserSortColumns="True"
                  AlternatingRowBackground="{DynamicResource BackgroundLightBrush}"
                  Background="{DynamicResource BackgroundBrush}"
                  Foreground="{DynamicResource ForegroundBrush}"
                  RowBackground="{DynamicResource BackgroundBrush}"
                  GridLinesVisibility="All"
                  RowHeight="35"
                  ContextMenu="{DynamicResource DataGridContextMenu}"
                  Margin="10">
            <DataGrid.Columns>
                <!-- Collection Info -->
                <DataGridTextColumn Binding="{Binding CollectionTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CollectionTime" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Collection Time" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding PlanId}" ElementStyle="{StaticResource NumericCell}" Width="70">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PlanId" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Plan ID" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding CountExecutions, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="90">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CountExecutions" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Executions" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Duration (ms) -->
                <DataGridTextColumn Binding="{Binding AvgDurationMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgDurationMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Avg Duration (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MinDurationMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinDurationMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min Duration (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxDurationMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxDurationMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max Duration (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- CPU (ms) -->
                <DataGridTextColumn Binding="{Binding AvgCpuTimeMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgCpuTimeMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Avg CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MinCpuTimeMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinCpuTimeMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxCpuTimeMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxCpuTimeMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Logical Reads -->
                <DataGridTextColumn Binding="{Binding AvgLogicalReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgLogicalReads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Avg Logical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MinLogicalReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinLogicalReads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min Logical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxLogicalReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxLogicalReads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max Logical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Logical Writes -->
                <DataGridTextColumn Binding="{Binding AvgLogicalWrites, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgLogicalWrites" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Avg Logical Writes" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MinLogicalWrites, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinLogicalWrites" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min Logical Writes" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxLogicalWrites, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxLogicalWrites" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max Logical Writes" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Physical Reads -->
                <DataGridTextColumn Binding="{Binding AvgPhysicalReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="120">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgPhysicalReads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Avg Physical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MinPhysicalReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="120">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinPhysicalReads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min Physical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxPhysicalReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="120">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxPhysicalReads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max Physical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- DOP -->
                <DataGridTextColumn Binding="{Binding MinDop}" ElementStyle="{StaticResource NumericCell}" Width="70">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinDop" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min DOP" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxDop}" ElementStyle="{StaticResource NumericCell}" Width="70">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxDop" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max DOP" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Memory (MB) -->
                <DataGridTextColumn Binding="{Binding AvgMemoryMb, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgMemoryMb" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Avg Memory (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MinMemoryMb, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinMemoryMb" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min Memory (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxMemoryMb, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxMemoryMb" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max Memory (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Row Count -->
                <DataGridTextColumn Binding="{Binding AvgRowcount, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="90">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgRowcount" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Avg Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MinRowcount, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="90">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinRowcount" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxRowcount, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="90">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxRowcount" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- TempDB (MB) -->
                <DataGridTextColumn Binding="{Binding AvgTempdbMb, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgTempdbMb" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Avg TempDB (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MinTempdbMb, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinTempdbMb" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min TempDB (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxTempdbMb, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxTempdbMb" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max TempDB (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Plan Info -->
                <DataGridTextColumn Binding="{Binding PlanType}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PlanType" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Plan Type" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridCheckBoxColumn Binding="{Binding IsForcedPlan}" Width="60">
                    <DataGridCheckBoxColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IsForcedPlan" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Forced" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridCheckBoxColumn.Header>
                </DataGridCheckBoxColumn>
                <DataGridTextColumn Binding="{Binding ForceFailureCount}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ForceFailureCount" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Force Failures" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding LastForceFailureReasonDesc}" Width="150">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LastForceFailureReasonDesc" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Last Failure Reason" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding PlanForcingType}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PlanForcingType" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Forcing Type" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding CompatibilityLevel}" ElementStyle="{StaticResource NumericCell}" Width="90">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CompatibilityLevel" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Compat Level" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Query Identifiers -->
                <DataGridTextColumn Binding="{Binding QueryHash}" Width="160">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="QueryHash" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Query Hash" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding QueryPlanHash}" Width="160">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="QueryPlanHash" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Query Plan Hash" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Download Plan Button -->
                <DataGridTemplateColumn Header="Query Plan" Width="Auto" MinWidth="90">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Download" Click="DownloadPlan_Click"
                                    HorizontalAlignment="Center" VerticalAlignment="Center"
                                    Padding="8,2"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

        <!-- Footer -->
        <Border Grid.Row="3" Background="{DynamicResource BackgroundLightBrush}" Padding="10" Margin="10,0,10,10">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <Button Content="Close" Width="100" Height="30" Click="Close_Click"/>
            </StackPanel>
        </Border>
    </Grid>
</Window>
