<Window x:Class="PerformanceMonitorDashboard.WaitDrillDownWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Wait Drill-Down" Width="1500" Height="700"
        WindowStartupLocation="CenterOwner"
        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>

            <Style x:Key="DefaultRowStyle" TargetType="DataGridRow">
                <Setter Property="ContextMenu" Value="{StaticResource DataGridContextMenu}"/>
            </Style>
        </ResourceDictionary>
    </Window.Resources>

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

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

        <!-- Warning banner (hidden by default) -->
        <Border x:Name="WarningBanner" Grid.Row="1" Background="#3D332200"
                BorderBrush="#665C3300" BorderThickness="1" CornerRadius="3"
                Padding="8,4" Margin="0,0,0,6" Visibility="Collapsed">
            <TextBlock x:Name="WarningText" FontSize="11"
                       Foreground="#FFCC66" TextWrapping="Wrap"/>
        </Border>

        <!-- Data Grid -->
        <DataGrid Grid.Row="2" x:Name="ResultsDataGrid"
                  AutoGenerateColumns="False" IsReadOnly="True"
                  CanUserSortColumns="True" CanUserReorderColumns="True" CanUserResizeColumns="True"
                  GridLinesVisibility="Horizontal"
                  ScrollViewer.CanContentScroll="True"
                  ScrollViewer.HorizontalScrollBarVisibility="Auto"
                  ScrollViewer.VerticalScrollBarVisibility="Auto"
                  RowStyle="{DynamicResource DefaultRowStyle}"
                  ContextMenu="{StaticResource DataGridContextMenu}">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding CollectionTime, StringFormat='MM/dd HH:mm:ss'}" Width="140">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CollectionTime" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Collected" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding SessionId}" ElementStyle="{StaticResource NumericCell}" Width="60">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SessionId" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="SPID" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Duration}" Width="100" SortMemberPath="DurationSort">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Duration" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Duration" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Status}" Width="80">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Status" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Status" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding WaitInfo}" Width="180">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="WaitInfo" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Wait Info" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding BlockingSessionId}" ElementStyle="{StaticResource NumericCell}" Width="80">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="BlockingSessionId" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Blocking" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding BlockedSessionCount}" ElementStyle="{StaticResource NumericCell}" Width="80">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="BlockedSessionCount" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Blocked" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Cpu}" ElementStyle="{StaticResource NumericCell}" Width="80">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Cpu" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Reads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Reads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Writes, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Writes" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Writes (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding PhysicalReads}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PhysicalReads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Physical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding ContextSwitches}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ContextSwitches" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Ctx Switches" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding UsedMemoryMb, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="UsedMemoryMb" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Used Mem (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding TempdbCurrentMb, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TempdbCurrentMb" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="TempDB Cur (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding TempdbAllocations, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TempdbAllocations" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="TempDB Alloc" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding TranLogWrites}" ElementStyle="{StaticResource NumericCell}" Width="100" SortMemberPath="TranLogWritesSort">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TranLogWrites" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Tran Log" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding OpenTranCount}" ElementStyle="{StaticResource NumericCell}" Width="80">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="OpenTranCount" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Open Tran" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding PercentComplete, StringFormat='{}{0:N1}%'}" ElementStyle="{StaticResource NumericCell}" Width="80">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PercentComplete" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="% Complete" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding StartTime, StringFormat='MM/dd HH:mm:ss'}" Width="140">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="StartTime" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Start Time" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding TranStartTime, StringFormat='MM/dd HH:mm:ss'}" Width="140">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TranStartTime" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Tran Start" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding RequestId}" ElementStyle="{StaticResource NumericCell}" Width="70">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="RequestId" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Req ID" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding DatabaseName}" Width="120">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding LoginName}" Width="120">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LoginName" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Login" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding HostName}" Width="120">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="HostName" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Host" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding ProgramName}" Width="140">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ProgramName" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Program" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTemplateColumn Width="500">
                    <DataGridTemplateColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="QueryText" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Query Text" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTemplateColumn.Header>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding QueryText}" TextWrapping="Wrap" MaxHeight="90" Margin="5,2"
                                       ToolTip="{Binding QueryText}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Width="300">
                    <DataGridTemplateColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SqlCommand" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="SQL Command" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTemplateColumn.Header>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding SqlCommand}" TextWrapping="Wrap" MaxHeight="90" Margin="5,2"
                                       ToolTip="{Binding SqlCommand}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

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