<UserControl x:Class="PerformanceMonitorDashboard.Controls.ResourceMetricsContent"
             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:local="clr-namespace:PerformanceMonitorDashboard.Controls"
             mc:Ignorable="d"
             d:DesignHeight="600" d:DesignWidth="1200">
    <UserControl.Resources>
        <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>
    </UserControl.Resources>

    <TabControl x:Name="SubTabControl" TabStripPlacement="Top" Margin="0,5,0,0" ItemContainerStyle="{DynamicResource SubTabItemStyle}">
        <!-- Server Trends Sub-Tab — Correlated Timeline Lanes -->
        <TabItem Header="Server Trends">
            <DockPanel>
                <local:CorrelatedTimelineLanesControl x:Name="CorrelatedLanes"/>
            </DockPanel>
        </TabItem>

        <!-- Wait Stats Detail Sub-Tab -->
        <TabItem Header="Wait Stats">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="280"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <!-- Wait Type Selection Panel -->
                <Border Grid.Column="0" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="5,5,5,2">
                            <TextBlock Text="Select Wait Types" FontWeight="Bold" VerticalAlignment="Center" Foreground="{DynamicResource ForegroundBrush}"/>
                            <Button Content="Refresh" Click="WaitStatsDetail_Refresh_Click" Margin="10,0,0,0" Padding="8,2" Style="{DynamicResource SuccessButton}" FontSize="11"/>
                        </StackPanel>
                        <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="5,2">
                            <Button Content="Top Waits" Click="WaitTypes_SelectAll_Click" Padding="6,2" FontSize="10" Margin="0,0,5,0"/>
                            <Button Content="Clear All" Click="WaitTypes_ClearAll_Click" Padding="6,2" FontSize="10"/>
                            <TextBlock x:Name="WaitTypeCountText" Text="0 / 30 selected" FontSize="10" Foreground="{DynamicResource ForegroundMutedBrush}" VerticalAlignment="Center" Margin="8,0,0,0"/>
                        </StackPanel>
                        <TextBox Grid.Row="2" x:Name="WaitTypeSearchBox" Margin="5,2" FontSize="11"
                                 TextChanged="WaitTypeSearch_TextChanged"
                                 ToolTip="Type to filter wait types"/>
                        <ListBox Grid.Row="3" x:Name="WaitTypesList" Margin="5" Background="{DynamicResource BackgroundDarkBrush}" Foreground="{DynamicResource ForegroundBrush}"
                                 SelectionMode="Multiple"
                                 ScrollViewer.VerticalScrollBarVisibility="Auto"
                                 SelectionChanged="WaitTypesList_SelectionChanged">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"
                                              Checked="WaitType_CheckChanged"
                                              Unchecked="WaitType_CheckChanged"
                                              FontSize="11" Foreground="{DynamicResource ForegroundBrush}">
                                        <TextBlock Text="{Binding DisplayName}"/>
                                    </CheckBox>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Grid>
                </Border>

                <!-- Chart -->
                <Border Grid.Column="1" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,5">
                            <TextBlock Text="Wait Stats Over Time" FontWeight="Bold" VerticalAlignment="Center" Margin="0,0,12,0"/>
                            <TextBlock Text="Metric:" VerticalAlignment="Center" Margin="0,0,4,0" FontSize="11"/>
                            <ComboBox x:Name="WaitStatsMetricCombo" SelectedIndex="0" SelectionChanged="WaitStatsMetric_SelectionChanged" FontSize="11" Padding="4,2">
                                <ComboBoxItem Content="Wait Time (ms/sec)"/>
                                <ComboBoxItem Content="Avg Wait Time (ms/wait)"/>
                            </ComboBox>
                        </StackPanel>
                        <ScottPlot:WpfPlot Grid.Row="1" x:Name="WaitStatsDetailChart"/>
                    </Grid>
                </Border>
            </Grid>
        </TabItem>
        <!-- TempDB Stats Sub-Tab -->
        <TabItem Header="TempDB Stats">
            <!-- TempDB Stats - usage / size / latency charts + summary -->
            <Grid Margin="5">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <!-- TempDB Usage Chart -->
                <Border Grid.Row="0" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="0,0,0,2">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Text="TempDB Usage Over Time" FontWeight="Bold" FontSize="11" Margin="5,3" Foreground="{DynamicResource ForegroundBrush}"/>
                        <ScottPlot:WpfPlot Grid.Row="1" x:Name="TempdbStatsChart"/>
                    </Grid>
                </Border>

                <!-- TempDB Allocated Size Chart (growth trend, own scale) -->
                <Border Grid.Row="1" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="0,2,0,2">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Text="TempDB Allocated Size Over Time (MB)" FontWeight="Bold" FontSize="11" Margin="5,3" Foreground="{DynamicResource ForegroundBrush}"/>
                        <ScottPlot:WpfPlot Grid.Row="1" x:Name="TempdbSizeChart"/>
                    </Grid>
                </Border>

                <!-- TempDB Latency Chart (Read + Write combined) -->
                <Border Grid.Row="2" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="0,2,0,0">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Text="TempDB Latency (ms)" FontWeight="Bold" FontSize="11" Margin="5,3" Foreground="{DynamicResource ForegroundBrush}"/>
                        <ScottPlot:WpfPlot Grid.Row="1" x:Name="TempDbLatencyChart"/>
                    </Grid>
                </Border>

                <!-- Summary Panel -->
                <Border Grid.Row="3" Background="{DynamicResource BackgroundBrush}" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="0,1,0,0" Padding="10,8">
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                        <TextBlock Text="Sessions Using TempDB:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                        <TextBlock x:Name="TempdbSessionsText" Text="N/A" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center" Margin="0,0,20,0"/>
                        <TextBlock Text="Warnings:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                        <TextBlock x:Name="TempdbWarningsText" Text="None" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"/>
                    </StackPanel>
                </Border>
            </Grid>
        </TabItem>

        <!-- File I/O Sub-Tab -->
        <TabItem Header="File I/O">
            <TabControl Background="Transparent" BorderThickness="0" ItemContainerStyle="{DynamicResource SubTabItemStyle}">
                <!-- File I/O Latency sub-tab -->
                <TabItem Header="File I/O Latency">
                    <Grid Margin="5">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>

                        <!-- User DB Read Latency -->
                        <Border Grid.Row="0" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="0,0,0,2">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <TextBlock Grid.Row="0" Text="User DB Read Latency (ms)" FontWeight="Bold" FontSize="11" Margin="5,3" Foreground="{DynamicResource ForegroundBrush}"/>
                                <ScottPlot:WpfPlot x:Name="UserDbReadLatencyChart" Grid.Row="1"/>
                            </Grid>
                        </Border>

                        <!-- User DB Write Latency -->
                        <Border Grid.Row="1" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="0,2,0,0">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <TextBlock Grid.Row="0" Text="User DB Write Latency (ms)" FontWeight="Bold" FontSize="11" Margin="5,3" Foreground="{DynamicResource ForegroundBrush}"/>
                                <ScottPlot:WpfPlot x:Name="UserDbWriteLatencyChart" Grid.Row="1"/>
                            </Grid>
                        </Border>
                    </Grid>
                </TabItem>

                <!-- File I/O Throughput sub-tab -->
                <TabItem Header="File I/O Throughput">
                    <Grid Margin="5">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>

                        <!-- User DB Read Throughput -->
                        <Border Grid.Row="0" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="0,0,0,2">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <TextBlock Grid.Row="0" Text="User DB Read Throughput (MB/s)" FontWeight="Bold" FontSize="11" Margin="5,3" Foreground="{DynamicResource ForegroundBrush}"/>
                                <ScottPlot:WpfPlot x:Name="FileIoReadThroughputChart" Grid.Row="1"/>
                            </Grid>
                        </Border>

                        <!-- User DB Write Throughput -->
                        <Border Grid.Row="1" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="0,2,0,0">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <TextBlock Grid.Row="0" Text="User DB Write Throughput (MB/s)" FontWeight="Bold" FontSize="11" Margin="5,3" Foreground="{DynamicResource ForegroundBrush}"/>
                                <ScottPlot:WpfPlot x:Name="FileIoWriteThroughputChart" Grid.Row="1"/>
                            </Grid>
                        </Border>
                    </Grid>
                </TabItem>
            </TabControl>
        </TabItem>

        <!-- Perfmon Counters Sub-Tab -->
        <TabItem Header="Perfmon Counters">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="280"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <!-- Counter Selection Panel -->
                <Border Grid.Column="0" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="5,5,5,2">
                            <TextBlock Text="Select Counters" FontWeight="Bold" VerticalAlignment="Center" Foreground="{DynamicResource ForegroundBrush}"/>
                            <Button Content="Refresh" Click="PerfmonCounters_Refresh_Click" Margin="10,0,0,0" Padding="8,2" Style="{DynamicResource SuccessButton}" FontSize="11"/>
                        </StackPanel>
                        <ComboBox Grid.Row="1" x:Name="PerfmonPackCombo"
                                  SelectionChanged="PerfmonPack_SelectionChanged"
                                  Margin="5,2" FontSize="11"/>
                        <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="5,2">
                            <Button Content="Select All" Click="PerfmonCounters_SelectAll_Click" Padding="6,2" FontSize="10" Margin="0,0,5,0"/>
                            <Button Content="Clear All" Click="PerfmonCounters_ClearAll_Click" Padding="6,2" FontSize="10"/>
                        </StackPanel>
                        <TextBox Grid.Row="3" x:Name="PerfmonCounterSearchBox" Margin="5,2" Height="24" FontSize="11"
                                 TextChanged="PerfmonCounterSearch_TextChanged"
                                 ToolTip="Type to filter counters"/>
                        <ListBox Grid.Row="4" x:Name="PerfmonCountersList" Margin="5" Background="{DynamicResource BackgroundDarkBrush}" Foreground="{DynamicResource ForegroundBrush}"
                                 SelectionMode="Multiple"
                                 ScrollViewer.VerticalScrollBarVisibility="Auto"
                                 SelectionChanged="PerfmonCountersList_SelectionChanged">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"
                                              Checked="PerfmonCounter_CheckChanged"
                                              Unchecked="PerfmonCounter_CheckChanged"
                                              FontSize="11" Foreground="{DynamicResource ForegroundBrush}">
                                        <TextBlock Text="{Binding DisplayName}"/>
                                    </CheckBox>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Grid>
                </Border>

                <!-- Chart -->
                <Border Grid.Column="1" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Text="Performance Counters Over Time" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,5"/>
                        <ScottPlot:WpfPlot Grid.Row="1" x:Name="PerfmonCountersChart"/>
                    </Grid>
                </Border>
            </Grid>
        </TabItem>

        <!-- Session Stats Sub-Tab -->
        <TabItem Header="Session Stats">
            <!-- Session Stats Chart -->
            <Border BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="Session Counts Over Time" FontWeight="Bold" FontSize="14" Margin="10,5" Foreground="{DynamicResource ForegroundBrush}"/>
                    <ScottPlot:WpfPlot Grid.Row="1" x:Name="SessionStatsChart" Margin="5"/>
                    <!-- Summary Panel for non-chartable 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="Top Application:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                            <TextBlock x:Name="SessionStatsTopAppText" Text="N/A" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center" Margin="0,0,20,0"/>
                            <TextBlock Text="Top Host:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                            <TextBlock x:Name="SessionStatsTopHostText" Text="N/A" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center" Margin="0,0,20,0"/>
                            <TextBlock Text="Databases:" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundDimBrush}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                            <TextBlock x:Name="SessionStatsDatabasesText" Text="N/A" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"/>
                        </StackPanel>
                    </Border>
                </Grid>
            </Border>
        </TabItem>

        <!-- Latch Stats Sub-Tab -->
        <TabItem Header="Latch Stats">
            <!-- Latch Stats Chart -->
            <Border BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Row="0" Text="Latch Wait Time Over Time" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,5"/>
                    <ScottPlot:WpfPlot Grid.Row="1" x:Name="LatchStatsChart"/>
                </Grid>
            </Border>
        </TabItem>

        <!-- Spinlock Stats Sub-Tab -->
        <TabItem Header="Spinlock Stats">
            <!-- Spinlock Stats Chart -->
            <Border BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Row="0" Text="Spinlock Collisions Over Time" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,5"/>
                    <ScottPlot:WpfPlot Grid.Row="1" x:Name="SpinlockStatsChart"/>
                </Grid>
            </Border>
        </TabItem>
    </TabControl>
</UserControl>
