<UserControl x:Class="PerformanceMonitorDashboard.Controls.DailySummaryContent"
             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:controls="clr-namespace:PerformanceMonitorDashboard.Controls"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
    <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>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Date Selection Controls -->
        <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,5" Background="{DynamicResource BackgroundLightBrush}" Height="40">
            <TextBlock Text="Summary Date:" VerticalAlignment="Center" Margin="5,0" FontWeight="Bold"/>
            <Button x:Name="DailySummaryTodayButton" Content="Today" Click="DailySummaryToday_Click" Margin="5,0" Padding="8,4" MinWidth="60" Style="{DynamicResource AccentButton}"/>
            <DatePicker x:Name="DailySummaryDatePicker" Width="120" Margin="5,0" SelectedDateChanged="DailySummaryDate_Changed" CalendarOpened="DatePicker_CalendarOpened"/>
            <Button Content="Refresh" Click="DailySummary_Refresh_Click" Margin="10,0,2,0" Padding="10,4" MinWidth="70" Style="{DynamicResource SuccessButton}"/>
            <TextBlock x:Name="DailySummaryIndicator"
                       Text="Showing: Today"
                       Margin="10,0"
                       VerticalAlignment="Center"
                       FontStyle="Italic"
                       Foreground="{DynamicResource AccentBrush}"
                       FontWeight="SemiBold"/>
        </StackPanel>

        <Grid Grid.Row="1">
        <DataGrid x:Name="DailySummaryDataGrid"
                  AutoGenerateColumns="False"
                  IsReadOnly="True"
                  CanUserSortColumns="True"
                  GridLinesVisibility="All" CanUserResizeColumns="True"
                  RowStyle="{StaticResource DefaultRowStyle}">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding SummaryDate, StringFormat='{}{0:yyyy-MM-dd}'}" Width="120">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SummaryDate" Click="DailySummaryFilter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Summary Date" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding OverallHealth}" Width="150">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="OverallHealth" Click="DailySummaryFilter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Overall Health" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding TotalWaitTimeSec, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="130">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalWaitTimeSec" Click="DailySummaryFilter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Total Wait (sec)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding TopWaitType}" Width="180">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TopWaitType" Click="DailySummaryFilter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Top Wait Type" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding ExpensiveQueriesCount, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="130">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ExpensiveQueriesCount" Click="DailySummaryFilter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Expensive Queries" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding DeadlockCount, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DeadlockCount" Click="DailySummaryFilter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Deadlocks" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding BlockingEventsCount, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="130">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="BlockingEventsCount" Click="DailySummaryFilter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Blocking Events" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MemoryPressureEvents, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="140">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MemoryPressureEvents" Click="DailySummaryFilter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Memory Pressure" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding HighCpuEvents, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="120">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="HighCpuEvents" Click="DailySummaryFilter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="High CPU Events" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding CollectorsFailing, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="130">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CollectorsFailing" Click="DailySummaryFilter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Collectors Failing" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
        <TextBlock x:Name="DailySummaryNoDataMessage" Style="{StaticResource EmptyStateMessage}"
                   Text="No daily summary data. Try selecting a different date."/>
        <controls:LoadingOverlay x:Name="DailySummaryLoading"/>
        </Grid>
    </Grid>
</UserControl>
