<Window x:Class="PerformanceMonitorLite.Windows.AlertDetailWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Alert Details"
        Width="480" SizeToContent="Height"
        WindowStartupLocation="CenterOwner"
        ResizeMode="NoResize"
        Icon="/EDD.ico"
        Background="{DynamicResource BackgroundBrush}">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVis"/>
    </Window.Resources>
    <Grid Margin="20">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- Header -->
        <TextBlock Grid.Row="0" Text="Alert Details" FontSize="16" FontWeight="SemiBold"
                   Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,16"/>

        <!-- Detail fields -->
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="130"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <TextBlock Grid.Row="0" Grid.Column="0" Text="Time:" FontWeight="SemiBold"
                       Foreground="{DynamicResource ForegroundBrush}" Margin="0,4"/>
            <TextBlock Grid.Row="0" Grid.Column="1" x:Name="TimeText"
                       Foreground="{DynamicResource ForegroundBrush}" Margin="0,4" TextWrapping="Wrap"/>

            <TextBlock Grid.Row="1" Grid.Column="0" Text="Server:" FontWeight="SemiBold"
                       Foreground="{DynamicResource ForegroundBrush}" Margin="0,4"/>
            <TextBlock Grid.Row="1" Grid.Column="1" x:Name="ServerText"
                       Foreground="{DynamicResource ForegroundBrush}" Margin="0,4" TextWrapping="Wrap"/>

            <TextBlock Grid.Row="2" Grid.Column="0" Text="Metric:" FontWeight="SemiBold"
                       Foreground="{DynamicResource ForegroundBrush}" Margin="0,4"/>
            <TextBlock Grid.Row="2" Grid.Column="1" x:Name="MetricText"
                       Foreground="{DynamicResource ForegroundBrush}" Margin="0,4" TextWrapping="Wrap"/>

            <TextBlock Grid.Row="3" Grid.Column="0" Text="Current Value:" FontWeight="SemiBold"
                       Foreground="{DynamicResource ForegroundBrush}" Margin="0,4"/>
            <TextBlock Grid.Row="3" Grid.Column="1" x:Name="CurrentValueText"
                       Foreground="{DynamicResource ForegroundBrush}" Margin="0,4" TextWrapping="Wrap"/>

            <TextBlock Grid.Row="4" Grid.Column="0" Text="Threshold:" FontWeight="SemiBold"
                       Foreground="{DynamicResource ForegroundBrush}" Margin="0,4"/>
            <TextBlock Grid.Row="4" Grid.Column="1" x:Name="ThresholdText"
                       Foreground="{DynamicResource ForegroundBrush}" Margin="0,4" TextWrapping="Wrap"/>

            <TextBlock Grid.Row="5" Grid.Column="0" Text="Notification:" FontWeight="SemiBold"
                       Foreground="{DynamicResource ForegroundBrush}" Margin="0,4"/>
            <TextBlock Grid.Row="5" Grid.Column="1" x:Name="NotificationText"
                       Foreground="{DynamicResource ForegroundBrush}" Margin="0,4" TextWrapping="Wrap"/>

            <TextBlock Grid.Row="6" Grid.Column="0" Text="Status:" FontWeight="SemiBold"
                       Foreground="{DynamicResource ForegroundBrush}" Margin="0,4"/>
            <TextBlock Grid.Row="6" Grid.Column="1" x:Name="StatusText"
                       Foreground="{DynamicResource ForegroundBrush}" Margin="0,4" TextWrapping="Wrap"/>
        </Grid>

        <!-- Muted indicator -->
        <Border Grid.Row="2" x:Name="MutedBanner" Visibility="Collapsed"
                CornerRadius="4" Padding="8" Margin="0,12,0,0">
            <Border.Background>
                <SolidColorBrush Color="{StaticResource WarningColor}" Opacity="0.2"/>
            </Border.Background>
            <TextBlock Text="🔇 This alert was muted by a mute rule"
                       Foreground="{DynamicResource ForegroundBrush}" FontStyle="Italic"/>
        </Border>

        <!-- Context-specific details -->
        <Border Grid.Row="3" x:Name="DetailPanel" Visibility="Collapsed"
                Background="{DynamicResource BackgroundBrush}" BorderBrush="{DynamicResource BorderBrush}"
                BorderThickness="1" CornerRadius="4" Padding="10" Margin="0,12,0,0">
            <StackPanel>
                <TextBlock Text="Details" FontWeight="SemiBold" FontSize="13"
                           Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,6"/>

                <!-- Structured detail items (advice prose, remediation T-SQL, drill-down).
                     Shown when the persisted context_json deserializes. -->
                <ScrollViewer x:Name="DetailItemsScroll" Visibility="Collapsed"
                              MaxHeight="380" VerticalScrollBarVisibility="Auto">
                    <ItemsControl x:Name="DetailItems">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Margin="0,0,0,10">
                                    <TextBlock Text="{Binding Heading}" FontWeight="SemiBold"
                                               Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>

                                    <!-- Remediation T-SQL: monospace, read-only, with Copy. -->
                                    <StackPanel Visibility="{Binding IsCodeBlock, Converter={StaticResource BoolToVis}}">
                                        <Button Content="Copy" HorizontalAlignment="Right"
                                                Padding="8,2" Margin="0,0,0,4"
                                                Click="CopyTsqlButton_Click"/>
                                        <TextBox Text="{Binding Body, Mode=OneWay}" IsReadOnly="True"
                                                 FontFamily="Consolas" FontSize="12" TextWrapping="NoWrap"
                                                 MaxHeight="240"
                                                 VerticalScrollBarVisibility="Auto"
                                                 HorizontalScrollBarVisibility="Auto"
                                                 Background="Transparent"
                                                 Foreground="{DynamicResource ForegroundBrush}"
                                                 BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1"/>
                                    </StackPanel>

                                    <!-- Advice prose. -->
                                    <TextBlock Text="{Binding Body}" TextWrapping="Wrap"
                                               Visibility="{Binding IsProse, Converter={StaticResource BoolToVis}}"
                                               Foreground="{DynamicResource ForegroundBrush}"/>

                                    <!-- Label/value field rows. -->
                                    <ItemsControl ItemsSource="{Binding Fields}"
                                                  Visibility="{Binding HasFields, Converter={StaticResource BoolToVis}}">
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <Grid Margin="0,1">
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="130"/>
                                                        <ColumnDefinition Width="*"/>
                                                    </Grid.ColumnDefinitions>
                                                    <TextBlock Grid.Column="0" Text="{Binding Label}"
                                                               Foreground="{DynamicResource ForegroundBrush}"
                                                               TextWrapping="Wrap"/>
                                                    <TextBlock Grid.Column="1" Text="{Binding Value}"
                                                               Foreground="{DynamicResource ForegroundBrush}"
                                                               FontFamily="Consolas" FontSize="12"
                                                               TextWrapping="Wrap"/>
                                                </Grid>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </ScrollViewer>

                <!-- Legacy / no-context fallback: flat persisted detail_text. -->
                <TextBox x:Name="DetailTextBox" IsReadOnly="True" TextWrapping="Wrap"
                         Visibility="Collapsed"
                         Background="Transparent" BorderThickness="0"
                         Foreground="{DynamicResource ForegroundBrush}" FontFamily="Consolas"
                         FontSize="12" MaxHeight="300"
                         VerticalScrollBarVisibility="Auto"/>
            </StackPanel>
        </Border>
    </Grid>
</Window>
