<UserControl x:Class="PerformanceMonitorDashboard.Controls.MemoryContent"
             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"
             xmlns:controls="clr-namespace:PerformanceMonitorDashboard.Controls"
             mc:Ignorable="d"
             d:DesignHeight="600" d:DesignWidth="1200">
    <UserControl.Resources>
        <ResourceDictionary>
            <!-- Context Menu for DataGrid Copy/Export -->
            <ContextMenu x:Key="DataGridContextMenu">
                <MenuItem Header="Copy Cell" Click="CopyCell_Click">
                    <MenuItem.Icon><TextBlock Text="📋"/></MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Copy Row" Click="CopyRow_Click">
                    <MenuItem.Icon><TextBlock Text="📄"/></MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Copy All Rows" Click="CopyAllRows_Click">
                    <MenuItem.Icon><TextBlock Text="📑"/></MenuItem.Icon>
                </MenuItem>
                <Separator/>
                <MenuItem Header="Export to CSV..." Click="ExportToCsv_Click">
                    <MenuItem.Icon><TextBlock Text="📊"/></MenuItem.Icon>
                </MenuItem>
            </ContextMenu>

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

    <TabControl x:Name="SubTabControl" TabStripPlacement="Top" Margin="0,5,0,0" ItemContainerStyle="{DynamicResource SubTabItemStyle}">
        <!-- Memory Overview Sub-Tab -->
        <TabItem Header="Memory Overview">
            <!-- Chart + Summary Panel (grid removed) -->
            <Border BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Row="0" Text="Memory Usage Over Time" FontWeight="Bold" FontSize="14" Margin="10,5" Foreground="{DynamicResource ForegroundBrush}"/>
                    <ScottPlot:WpfPlot Grid.Row="1" x:Name="MemoryStatsOverviewChart"/>
                    <!-- Summary Panel with absolute GB values and pressure status -->
                    <Border Grid.Row="2" Background="{DynamicResource BackgroundBrush}" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="0,1,0,0" Padding="10,6">
                        <WrapPanel HorizontalAlignment="Center">
                            <!-- Physical Memory -->
                            <StackPanel Orientation="Horizontal" Margin="0,0,24,0">
                                <TextBlock Text="Physical Memory:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                <TextBlock x:Name="MemoryStatsPhysicalText" Text="N/A" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"/>
                            </StackPanel>
                            <!-- SQL Server Memory (committed) -->
                            <StackPanel Orientation="Horizontal" Margin="0,0,24,0">
                                <TextBlock Text="SQL Server Memory:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                <TextBlock x:Name="MemoryStatsSqlServerText" Text="N/A" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"/>
                            </StackPanel>
                            <!-- Target Memory -->
                            <StackPanel Orientation="Horizontal" Margin="0,0,24,0">
                                <TextBlock Text="Target Memory:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                <TextBlock x:Name="MemoryStatsTargetText" Text="N/A" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"/>
                            </StackPanel>
                            <!-- Buffer Pool -->
                            <StackPanel Orientation="Horizontal" Margin="0,0,24,0">
                                <TextBlock Text="Buffer Pool:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                <TextBlock x:Name="MemoryStatsBPPercentText" Text="N/A" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"/>
                            </StackPanel>
                            <!-- Plan Cache -->
                            <StackPanel Orientation="Horizontal" Margin="0,0,24,0">
                                <TextBlock Text="Plan Cache:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                <TextBlock x:Name="MemoryStatsPCPercentText" Text="N/A" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"/>
                            </StackPanel>
                            <!-- Utilization -->
                            <StackPanel Orientation="Horizontal" Margin="0,0,24,0">
                                <TextBlock Text="Utilization:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                <TextBlock x:Name="MemoryStatsUtilPercentText" Text="N/A" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"/>
                            </StackPanel>
                            <!-- Pressure -->
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Pressure:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                <TextBlock x:Name="MemoryStatsPressureText" Text="None" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"/>
                            </StackPanel>
                        </WrapPanel>
                    </Border>
                </Grid>
            </Border>
        </TabItem>

        <!-- Memory Grants Sub-Tab -->
        <TabItem Header="Memory Grants">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <!-- Chart 1: Memory Grant Sizing -->
                <Border Grid.Row="0" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5,5,5,2">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Text="Memory Grant Sizing Over Time" FontWeight="Bold" FontSize="14" Margin="10,5" Foreground="{DynamicResource ForegroundBrush}"/>
                        <ScottPlot:WpfPlot Grid.Row="1" x:Name="MemoryGrantSizingChart"/>
                        <TextBlock x:Name="MemoryGrantSizingNoData" Grid.Row="1" Style="{StaticResource EmptyStateMessage}"
                                   Text="No memory grant data in selected time range"/>
                        <controls:LoadingOverlay x:Name="MemoryGrantSizingLoading" Grid.Row="1"/>
                    </Grid>
                </Border>
                <!-- Chart 2: Memory Grant Activity -->
                <Border Grid.Row="1" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5,2,5,5">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Text="Memory Grant Activity Over Time" FontWeight="Bold" FontSize="14" Margin="10,5" Foreground="{DynamicResource ForegroundBrush}"/>
                        <ScottPlot:WpfPlot Grid.Row="1" x:Name="MemoryGrantActivityChart"/>
                        <TextBlock x:Name="MemoryGrantActivityNoData" Grid.Row="1" Style="{StaticResource EmptyStateMessage}"
                                   Text="No memory grant data in selected time range"/>
                        <controls:LoadingOverlay x:Name="MemoryGrantActivityLoading" Grid.Row="1"/>
                    </Grid>
                </Border>
            </Grid>
        </TabItem>

        <!-- Memory Clerks Sub-Tab -->
        <TabItem Header="Memory Clerks">
            <Border BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="220"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <!-- Clerk Type Picker -->
                    <Border Grid.Column="0" Background="{DynamicResource BackgroundBrush}" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="0,0,1,0" Padding="8">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Text="Select Memory Clerks" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
                            <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="0,0,0,4">
                                <Button Content="Top Clerks" Click="MemoryClerkSelectTop_Click" Padding="6,2" Margin="0,0,4,0" FontSize="11"/>
                                <Button Content="Clear All" Click="MemoryClerkClearAll_Click" Padding="6,2" FontSize="11"/>
                                <TextBlock x:Name="MemoryClerkCountText" Text="0 selected" FontSize="10" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="8,0,0,0"/>
                            </StackPanel>
                            <TextBox Grid.Row="2" x:Name="MemoryClerkSearchBox" TextChanged="MemoryClerkSearch_TextChanged"
                                     Margin="0,0,0,4" Padding="4,2"/>
                            <ListBox Grid.Row="3" x:Name="MemoryClerksList" Background="Transparent" BorderThickness="0">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"
                                                  Checked="MemoryClerk_CheckChanged" Unchecked="MemoryClerk_CheckChanged"
                                                  Foreground="{DynamicResource ForegroundBrush}" FontSize="11">
                                            <TextBlock Text="{Binding DisplayName}"/>
                                        </CheckBox>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                        </Grid>
                    </Border>

                    <!-- Chart + Summary -->
                    <Grid Grid.Column="1">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Text="Memory Clerks Over Time" FontWeight="Bold" FontSize="14" Margin="10,5" Foreground="{DynamicResource ForegroundBrush}"/>
                        <ScottPlot:WpfPlot Grid.Row="1" x:Name="MemoryClerksChart"/>
                        <!-- Summary Panel (excludes buffer pool) -->
                        <Border Grid.Row="2" Background="{DynamicResource BackgroundBrush}" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="0,1,0,0" Padding="10,8">
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                <TextBlock Text="Non-BP Total:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                <TextBlock x:Name="MemoryClerksTotalText" Text="N/A" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center" Margin="0,0,20,0"/>
                                <TextBlock Text="Top Non-BP Clerk:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                <TextBlock x:Name="MemoryClerksTopText" Text="N/A" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"/>
                            </StackPanel>
                        </Border>
                        <TextBlock x:Name="MemoryClerksNoDataMessage" Grid.Row="1" Style="{StaticResource EmptyStateMessage}"
                                   Text="No memory clerk data in selected time range"/>
                        <controls:LoadingOverlay x:Name="MemoryClerksLoading" Grid.Row="1"/>
                    </Grid>
                </Grid>
            </Border>
        </TabItem>

        <!-- Plan Cache Sub-Tab -->
        <TabItem Header="Plan Cache">
            <!-- Plan Cache Chart -->
            <Border BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="Plan Cache Size (Single-Use vs Multi-Use)" FontWeight="Bold" FontSize="14" Margin="10,5" Foreground="{DynamicResource ForegroundBrush}"/>
                    <ScottPlot:WpfPlot Grid.Row="1" x:Name="PlanCacheChart"/>
                    <!-- Summary Panel for stability metrics -->
                    <Border Grid.Row="2" Background="{DynamicResource BackgroundBrush}" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="0,1,0,0" Padding="10,8">
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <TextBlock Text="Total Plans:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                            <TextBlock x:Name="PlanCacheTotalPlansText" Text="N/A" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center" Margin="0,0,20,0"/>
                            <TextBlock Text="Oldest Plan Age:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                            <TextBlock x:Name="PlanCacheOldestPlanText" Text="N/A" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"/>
                        </StackPanel>
                    </Border>
                </Grid>
            </Border>
        </TabItem>

        <!-- Memory Pressure Events Sub-Tab -->
        <TabItem Header="Memory Pressure Events">
            <!-- Memory Pressure Events Chart -->
            <Border BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="Memory Pressure Events Over Time" FontWeight="Bold" FontSize="14" Margin="10,5" Foreground="{DynamicResource ForegroundBrush}"/>
                    <ScottPlot:WpfPlot Grid.Row="1" x:Name="MemoryPressureEventsChart" Margin="5"/>
                </Grid>
            </Border>
        </TabItem>
    </TabControl>
</UserControl>
