<UserControl x:Class="PerformanceMonitorLite.Controls.RecommendationsTab"
             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:PerformanceMonitorLite.Controls"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <ResourceDictionary>
            <BooleanToVisibilityConverter x:Key="BoolToVis"/>

            <!-- Shared empty/insufficient-state text style (Lite has no global EmptyStateMessage). -->
            <Style x:Key="LiteEmptyStateMessage" TargetType="TextBlock">
                <Setter Property="Foreground" Value="{DynamicResource ForegroundMutedBrush}"/>
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="Visibility" Value="Collapsed"/>
            </Style>

            <!-- One card: severity badge + database + title + advice, then advise-only affordances.
                 Lite is advise-only: "Copy fix" (when a statement is derivable) + "Ask AI" (copies an
                 MCP investigation prompt). NO Apply, NO Open-in-Active-Queries, NO Mute. -->
            <DataTemplate x:Key="LiteRecommendationCardTemplate">
                <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 (advise-only): Copy fix (when derivable) + Ask AI. -->
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,8,0,0">
                            <Button Content="Copy fix" Padding="10,3" Margin="0,0,8,0"
                                    Visibility="{Binding ShowCopyFix, Converter={StaticResource BoolToVis}}"
                                    Click="CopyFix_Click"
                                    ToolTip="Copy the suggested T-SQL to the clipboard"/>
                            <Button Content="Ask AI" Padding="10,3"
                                    Visibility="{Binding ShowAskAi, Converter={StaticResource BoolToVis}}"
                                    Click="AskAi_Click"
                                    ToolTip="Copy an MCP investigation prompt to the clipboard"/>
                        </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="LiteRecommendationSectionTemplate">
                <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 LiteRecommendationCardTemplate}"
                                  Margin="0,8,0,0"/>
                </Expander>
            </DataTemplate>
        </ResourceDictionary>
    </UserControl.Resources>

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

        <!-- Header bar: server selector + Refresh + Generate now. -->
        <Border Grid.Row="0" Background="{DynamicResource BackgroundBrush}"
                BorderBrush="{DynamicResource BorderBrush}" BorderThickness="0,0,0,1"
                Padding="10,8">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Server:" VerticalAlignment="Center" Margin="0,0,6,0"
                           FontWeight="SemiBold" Foreground="{DynamicResource ForegroundBrush}"/>
                <ComboBox x:Name="ServerSelector" Width="260" DisplayMemberPath="DisplayNameWithIntent"
                          SelectionChanged="ServerSelector_SelectionChanged"/>
                <Button x:Name="RefreshButton" Content="Refresh" Padding="12,4"
                        Margin="12,0,0,0"
                        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 LiteRecommendationSectionTemplate}"/>
            </ScrollViewer>

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

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

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