<UserControl x:Class="PerformanceMonitorDashboard.Controls.RecommendationsContent"
             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>
            <BooleanToVisibilityConverter x:Key="BoolToVis"/>

            <!-- One card: severity badge + database + title + advice, then affordances.
                 Incidents (Setting==None) send the operator to the dashboard / AI; config-fixes
                 (Setting!=None) offer "Copy fix" (the ALTER). Apply (live: confirm gate +
                 two-sided informed consent for destructive fixes) and Mute (engine rows) are wired
                 in WS1b-2. Mirrors AlertDetailWindow's per-row visual conventions. -->
            <DataTemplate x:Key="RecommendationCardTemplate">
                <Border Background="{DynamicResource BackgroundLightBrush}"
                        BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1"
                        CornerRadius="4" Padding="10" Margin="0,0,0,8">
                    <StackPanel>
                        <!-- Header row: severity badge + [database] -->
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,6">
                            <Border x:Name="SeverityBadge" CornerRadius="3" Padding="6,1"
                                    VerticalAlignment="Center"
                                    Background="{DynamicResource InfoBrush}">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding SeverityGlyph}"
                                               FontFamily="Segoe MDL2 Assets" FontSize="11"
                                               Foreground="#1A1A1A" VerticalAlignment="Center"
                                               Margin="0,0,4,0"/>
                                    <TextBlock Text="{Binding SeverityLabel}" FontWeight="Bold"
                                               FontSize="11" Foreground="#1A1A1A"
                                               VerticalAlignment="Center"/>
                                </StackPanel>
                            </Border>
                            <TextBlock Text="{Binding DatabaseBracketed}"
                                       Visibility="{Binding HasDatabase, Converter={StaticResource BoolToVis}}"
                                       FontFamily="Consolas" FontSize="12" FontWeight="SemiBold"
                                       Foreground="{DynamicResource AccentBrush}"
                                       VerticalAlignment="Center" Margin="8,0,0,0"/>
                        </StackPanel>

                        <!-- Title -->
                        <TextBlock Text="{Binding Title}" FontWeight="SemiBold" FontSize="13"
                                   TextWrapping="Wrap"
                                   Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>

                        <!-- Advice prose -->
                        <TextBlock Text="{Binding AdviceText}" TextWrapping="Wrap"
                                   Visibility="{Binding HasAdvice, Converter={StaticResource BoolToVis}}"
                                   Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>

                        <!-- Affordances. Incidents: Open in Active Queries + Ask AI (+ Apply when
                             a remediation exists). Config-fixes: Apply + Copy fix. Engine rows also
                             offer Mute. Apply opens the confirm gate (two-sided consent if destructive). -->
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,8,0,0">
                            <Button Content="Open in Active Queries →" Padding="10,3" Margin="0,0,8,0"
                                    Visibility="{Binding ShowOpenInActiveQueries, Converter={StaticResource BoolToVis}}"
                                    Click="OpenInActiveQueries_Click"
                                    ToolTip="Jump to Active Queries scoped to this finding's time window"/>
                            <Button Content="Ask AI" Padding="10,3" Margin="0,0,8,0"
                                    Visibility="{Binding ShowAskAi, Converter={StaticResource BoolToVis}}"
                                    Click="AskAi_Click"
                                    ToolTip="Copy an MCP investigation prompt to the clipboard"/>
                            <Button Content="Copy fix" Padding="10,3" Margin="0,0,8,0"
                                    Visibility="{Binding ShowCopyFix, Converter={StaticResource BoolToVis}}"
                                    Click="CopyFix_Click"
                                    ToolTip="Copy the ALTER statement to the clipboard"/>
                            <Button Content="Apply…" Padding="10,3" Margin="0,0,8,0"
                                    Visibility="{Binding ShowApply, Converter={StaticResource BoolToVis}}"
                                    Click="ApplyButton_Click"
                                    ToolTip="Apply this fix — you review and confirm before anything runs"/>
                            <Button Content="Mute" Padding="10,3"
                                    Visibility="{Binding ShowMute, Converter={StaticResource BoolToVis}}"
                                    Click="MuteButton_Click"
                                    ToolTip="Stop showing this recommendation (mutes the pattern for this server)"/>
                        </StackPanel>
                    </StackPanel>
                </Border>

                <DataTemplate.Triggers>
                    <!-- Severity-driven badge colour, from the theme status brushes. -->
                    <DataTrigger Binding="{Binding Severity}" Value="Critical">
                        <Setter TargetName="SeverityBadge" Property="Background"
                                Value="{DynamicResource ErrorBrush}"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Severity}" Value="Warning">
                        <Setter TargetName="SeverityBadge" Property="Background"
                                Value="{DynamicResource WarningBrush}"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Severity}" Value="Info">
                        <Setter TargetName="SeverityBadge" Property="Background"
                                Value="{DynamicResource InfoBrush}"/>
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>

            <!-- One severity section: a collapsible Expander whose header carries the count and
                 whose body is the card list for that band. -->
            <DataTemplate x:Key="RecommendationSectionTemplate">
                <Expander Header="{Binding Header}" IsExpanded="{Binding IsExpanded, Mode=OneTime}"
                          Foreground="{DynamicResource ForegroundBrush}" FontWeight="SemiBold"
                          FontSize="14" Margin="0,0,0,10">
                    <ItemsControl ItemsSource="{Binding Cards}"
                                  ItemTemplate="{StaticResource RecommendationCardTemplate}"
                                  Margin="0,8,0,0"/>
                </Expander>
            </DataTemplate>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Header bar: Refresh + Generate now. -->
        <Border Grid.Row="0" Background="{DynamicResource BackgroundBrush}"
                BorderBrush="{DynamicResource BorderBrush}" BorderThickness="0,0,0,1"
                Padding="10,8">
            <StackPanel Orientation="Horizontal">
                <Button x:Name="RefreshButton" Content="Refresh" Padding="12,4"
                        Click="RefreshButton_Click"
                        ToolTip="Re-read recommendations for this server"/>
                <Button x:Name="GenerateNowButton" Content="Generate now" Padding="12,4"
                        Margin="8,0,0,0"
                        Click="GenerateNowButton_Click"
                        ToolTip="Run an on-demand analysis for this server, then refresh"/>
                <TextBlock x:Name="StatusText" VerticalAlignment="Center" Margin="12,0,0,0"
                           FontStyle="Italic" Foreground="{DynamicResource ForegroundMutedBrush}"/>
            </StackPanel>
        </Border>

        <!-- Content area: exactly one region is visible per state. -->
        <Grid Grid.Row="1">
            <!-- Loaded: scrollable, grouped, collapsible sections. -->
            <ScrollViewer x:Name="SectionsScroll" VerticalScrollBarVisibility="Auto"
                          Padding="10" Visibility="Collapsed">
                <ItemsControl x:Name="SectionsList"
                              ItemTemplate="{StaticResource RecommendationSectionTemplate}"/>
            </ScrollViewer>

            <!-- Empty: all-clear. -->
            <TextBlock x:Name="EmptyMessage" Style="{StaticResource EmptyStateMessage}"
                       Text="All clear — no current recommendations."/>

            <!-- Insufficient data: engine has not collected its minimum window yet. -->
            <TextBlock x:Name="InsufficientDataMessage" Style="{StaticResource EmptyStateMessage}"
                       MaxWidth="520" TextWrapping="Wrap" TextAlignment="Center"/>

            <!-- Loading spinner. -->
            <controls:LoadingOverlay x:Name="LoadingOverlay"/>
        </Grid>
    </Grid>
</UserControl>
