<Window x:Class="PerformanceMonitorDashboard.TracePatternHistoryWindow"
        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:ScottPlot="clr-namespace:ScottPlot.WPF;assembly=ScottPlot.WPF"
        Title="Trace Pattern History" Height="900" Width="1600"
        WindowStartupLocation="CenterOwner"
        Background="{DynamicResource BackgroundBrush}">
    <Window.Resources>
        <!-- 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>
            <Separator/>
            <MenuItem Header="Get Actual Plan" Click="GetActualPlan_Click">
                <MenuItem.Icon><TextBlock Text="&#x25B6;"/></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}" TextWrapping="Wrap"/>
                <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="Duration (ms)" Tag="DurationMs" IsSelected="True"/>
                        <ComboBoxItem Content="CPU (ms)" Tag="CpuMs"/>
                        <ComboBoxItem Content="Reads (pages)" Tag="Reads"/>
                        <ComboBoxItem Content="Writes (pages)" Tag="Writes"/>
                        <ComboBoxItem Content="Row Counts" Tag="RowCounts"/>
                    </ComboBox>
                </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"
                  ContextMenu="{DynamicResource DataGridContextMenu}"
                  Margin="10">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding EndTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="EndTime" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="End Time" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <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 StartTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                    <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 EventName}" Width="120">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="EventName" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Event" 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 DurationMs, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DurationMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Duration (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding CpuMs, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="90">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CpuMs" 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 RowCounts, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="RowCounts" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Row Counts" 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 NtUserName}" Width="120">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="NtUserName" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="NT User" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding ApplicationName}" Width="150">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ApplicationName" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Application" 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 Spid}" ElementStyle="{StaticResource NumericCell}" Width="60">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Spid" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="SPID" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding ObjectId}" ElementStyle="{StaticResource NumericCell}" Width="90">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ObjectId" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Object ID" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTemplateColumn Width="*" MinWidth="300">
                    <DataGridTemplateColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SqlText" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="SQL Text" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTemplateColumn.Header>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding SqlText}" TextWrapping="Wrap" MaxHeight="90" Margin="5,2" ToolTip="{Binding SqlText}"/>
                        </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>
