<Window x:Class="PerformanceMonitorDashboard.SettingsWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Settings"
        Height="600" Width="680"
        MinHeight="500" MinWidth="600"
        WindowStartupLocation="CenterOwner"
        ResizeMode="CanResizeWithGrip"
        Background="{DynamicResource BackgroundBrush}">
    <Grid Margin="15">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- Tabbed Settings -->
        <TabControl Grid.Row="0" Background="{DynamicResource BackgroundBrush}">

            <!-- General Tab -->
            <TabItem Header="General">
                <ScrollViewer VerticalScrollBarVisibility="Auto" Padding="15">
                    <StackPanel>
                        <!-- Server Health Overview Section -->
                        <TextBlock Text="Server Health Overview" FontWeight="Bold" FontSize="13" Margin="0,0,0,10"/>
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,20">
                            <TextBlock Text="Refresh every:" VerticalAlignment="Center" Margin="0,0,10,0"/>
                            <ComboBox x:Name="NocRefreshIntervalComboBox" Width="120" SelectionChanged="NocRefreshIntervalComboBox_Changed">
                                <ComboBoxItem Content="10 seconds" Tag="10"/>
                                <ComboBoxItem Content="30 seconds" Tag="30"/>
                                <ComboBoxItem Content="1 minute" Tag="60"/>
                                <ComboBoxItem Content="2 minutes" Tag="120"/>
                                <ComboBoxItem Content="5 minutes" Tag="300"/>
                            </ComboBox>
                        </StackPanel>

                        <!-- Dashboard Auto-Refresh Section -->
                        <TextBlock Text="Dashboard Auto-Refresh" FontWeight="Bold" FontSize="13" Margin="0,0,0,10"/>
                        <StackPanel Margin="0,0,0,20">
                            <CheckBox x:Name="AutoRefreshCheckBox"
                                      Content="Enable auto-refresh"
                                      Margin="0,0,0,10"
                                      Checked="AutoRefreshCheckBox_Changed"
                                      Unchecked="AutoRefreshCheckBox_Changed"/>
                            <StackPanel Orientation="Horizontal" Margin="20,0,0,0">
                                <TextBlock Text="Refresh every:" VerticalAlignment="Center" Margin="0,0,10,0"/>
                                <ComboBox x:Name="RefreshIntervalComboBox" Width="120" SelectionChanged="RefreshIntervalComboBox_Changed">
                                    <ComboBoxItem Content="10 seconds" Tag="10"/>
                                    <ComboBoxItem Content="30 seconds" Tag="30"/>
                                    <ComboBoxItem Content="1 minute" Tag="60"/>
                                    <ComboBoxItem Content="2 minutes" Tag="120"/>
                                    <ComboBoxItem Content="5 minutes" Tag="300"/>
                                </ComboBox>
                            </StackPanel>
                        </StackPanel>

                        <!-- Default Time Range Section -->
                        <TextBlock Text="Default Time Range" FontWeight="Bold" FontSize="13" Margin="0,0,0,10"/>
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,20">
                            <TextBlock Text="Show data from last:" VerticalAlignment="Center" Margin="0,0,10,0"/>
                            <ComboBox x:Name="DefaultTimeRangeComboBox" Width="120" SelectionChanged="DefaultTimeRangeComboBox_Changed">
                                <ComboBoxItem Content="1 hour" Tag="1"/>
                                <ComboBoxItem Content="4 hours" Tag="4"/>
                                <ComboBoxItem Content="8 hours" Tag="8"/>
                                <ComboBoxItem Content="12 hours" Tag="12"/>
                                <ComboBoxItem Content="24 hours" Tag="24"/>
                                <ComboBoxItem Content="7 days" Tag="168"/>
                                <ComboBoxItem Content="30 days" Tag="720"/>
                            </ComboBox>
                        </StackPanel>

                        <!-- CSV Export Section -->
                        <TextBlock Text="CSV Export" FontWeight="Bold" FontSize="13" Margin="0,0,0,10"/>
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,20">
                            <TextBlock Text="Separator:" VerticalAlignment="Center" Margin="0,0,10,0"/>
                            <ComboBox x:Name="CsvSeparatorComboBox" Width="140">
                                <ComboBoxItem Content="Comma (,)" Tag=","/>
                                <ComboBoxItem Content="Semicolon (;)" Tag=";"/>
                                <ComboBoxItem Content="Tab" Tag="&#x9;"/>
                            </ComboBox>
                            <TextBlock Text="auto-detected from locale" FontSize="11" FontStyle="Italic"
                                       Foreground="Gray" VerticalAlignment="Center" Margin="8,0,0,0"
                                       x:Name="CsvSeparatorHint"/>
                        </StackPanel>

                        <!-- Navigation Section -->
                        <TextBlock Text="Navigation" FontWeight="Bold" FontSize="13" Margin="0,0,0,10"/>
                        <CheckBox x:Name="FocusServerTabCheckBox"
                                  Content="Focus server tab when clicking a server card"
                                  Margin="0,0,0,20"/>

                        <!-- Time Display Section -->
                        <TextBlock Text="Time Display" FontWeight="Bold" FontSize="13" Margin="0,0,0,10"/>
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,20">
                            <TextBlock Text="Show timestamps in:" VerticalAlignment="Center" Margin="0,0,10,0"/>
                            <ComboBox x:Name="TimeDisplayModeComboBox" Width="140">
                                <ComboBoxItem Content="Server Time" Tag="ServerTime"/>
                                <ComboBoxItem Content="Local Time" Tag="LocalTime"/>
                                <ComboBoxItem Content="UTC" Tag="UTC"/>
                            </ComboBox>
                        </StackPanel>

                        <!-- Color Theme Section -->
                        <TextBlock Text="Color Theme" FontWeight="Bold" FontSize="13" Margin="0,0,0,10"/>
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,0">
                            <TextBlock Text="Theme:" VerticalAlignment="Center" Margin="0,0,10,0"/>
                            <ComboBox x:Name="ColorThemeComboBox" Width="120"
                                      SelectionChanged="ColorThemeComboBox_Changed">
                                <ComboBoxItem Content="Dark" Tag="Dark"/>
                                <ComboBoxItem Content="Light" Tag="Light"/>
                                <ComboBoxItem Content="Cool Breeze" Tag="CoolBreeze"/>
                            </ComboBox>
                        </StackPanel>
                    </StackPanel>
                </ScrollViewer>
            </TabItem>

            <!-- Notifications Tab -->
            <TabItem Header="Notifications">
                <ScrollViewer VerticalScrollBarVisibility="Auto" Padding="15">
                    <StackPanel>
                        <!-- System Tray Section -->
                        <TextBlock Text="System Tray" FontWeight="Bold" FontSize="13" Margin="0,0,0,10"/>
                        <StackPanel Margin="0,0,0,20">
                            <CheckBox x:Name="MinimizeToTrayCheckBox"
                                      Content="Minimize to system tray"
                                      Margin="0,0,0,8"
                                      Checked="MinimizeToTrayCheckBox_Changed"
                                      Unchecked="MinimizeToTrayCheckBox_Changed"/>
                            <CheckBox x:Name="NotificationsEnabledCheckBox"
                                      Content="Enable notifications"
                                      Margin="0,0,0,0"
                                      Checked="NotificationsEnabledCheckBox_Changed"
                                      Unchecked="NotificationsEnabledCheckBox_Changed"/>
                        </StackPanel>

                        <!-- Connection Alerts Section -->
                        <TextBlock Text="Connection Alerts" FontWeight="Bold" FontSize="13" Margin="0,0,0,10"/>
                        <StackPanel Margin="0,0,0,20">
                            <CheckBox x:Name="NotifyConnectionLostCheckBox"
                                      Content="Notify when server connection lost"
                                      Margin="0,0,0,8"
                                      Checked="NotifyConnectionLostCheckBox_Changed"
                                      Unchecked="NotifyConnectionLostCheckBox_Changed"/>
                            <CheckBox x:Name="NotifyConnectionRestoredCheckBox"
                                      Content="Notify when server connection restored"
                                      Margin="0,0,0,0"
                                      Checked="NotifyConnectionRestoredCheckBox_Changed"
                                      Unchecked="NotifyConnectionRestoredCheckBox_Changed"/>
                        </StackPanel>

                        <!-- Performance Alerts Section -->
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
                            <TextBlock Text="Performance Alerts" FontWeight="Bold" FontSize="13" VerticalAlignment="Center"/>
                            <Button x:Name="RestoreAlertDefaultsButton" Content="Restore Defaults" Padding="8,2" Margin="12,0,0,0"
                                    FontSize="11" VerticalAlignment="Center" Click="RestoreAlertDefaultsButton_Click"/>
                        </StackPanel>
                        <StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="0,0,0,8">
                                <CheckBox x:Name="NotifyOnBlockingCheckBox"
                                          Content="Blocking over"
                                          VerticalAlignment="Center"
                                          Checked="NotifyOnBlockingCheckBox_Changed"
                                          Unchecked="NotifyOnBlockingCheckBox_Changed"/>
                                <TextBox x:Name="BlockingThresholdTextBox"
                                         Width="50"
                                         Margin="8,0,8,0"
                                         VerticalAlignment="Center"
                                         TextAlignment="Center"/>
                                <TextBlock Text="seconds" VerticalAlignment="Center"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="0,0,0,8">
                                <CheckBox x:Name="NotifyOnDeadlockCheckBox"
                                          Content="Deadlocks over"
                                          VerticalAlignment="Center"
                                          Checked="NotifyOnDeadlockCheckBox_Changed"
                                          Unchecked="NotifyOnDeadlockCheckBox_Changed"/>
                                <TextBox x:Name="DeadlockThresholdTextBox"
                                         Width="50"
                                         Margin="8,0,8,0"
                                         VerticalAlignment="Center"
                                         TextAlignment="Center"/>
                                <TextBlock Text="since last check" VerticalAlignment="Center"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <CheckBox x:Name="NotifyOnHighCpuCheckBox"
                                          Content="CPU over"
                                          VerticalAlignment="Center"
                                          Checked="NotifyOnHighCpuCheckBox_Changed"
                                          Unchecked="NotifyOnHighCpuCheckBox_Changed"/>
                                <TextBox x:Name="CpuThresholdTextBox"
                                         Width="50"
                                         Margin="8,0,8,0"
                                         VerticalAlignment="Center"
                                         TextAlignment="Center"/>
                                <TextBlock Text="%" VerticalAlignment="Center"/>
                                <TextBlock Text="measured as" VerticalAlignment="Center" Margin="8,0,4,0"/>
                                <ComboBox x:Name="CpuAlertModeBox"
                                          Width="240"
                                          VerticalAlignment="Center">
                                    <ComboBoxItem Content="Total non-idle CPU (SQL + other processes)" Tag="Total"/>
                                    <ComboBoxItem Content="SQL Server only (scheduler %)" Tag="SqlOnly"/>
                                </ComboBox>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                                <CheckBox x:Name="NotifyOnPoisonWaitsCheckBox"
                                          Content="Poison waits (THREADPOOL, etc.) avg wait &#x2265;"
                                          VerticalAlignment="Center"
                                          Checked="NotifyOnPoisonWaitsCheckBox_Changed"
                                          Unchecked="NotifyOnPoisonWaitsCheckBox_Changed"/>
                                <TextBox x:Name="PoisonWaitThresholdTextBox"
                                         Width="50"
                                         Margin="8,0,8,0"
                                         VerticalAlignment="Center"
                                         TextAlignment="Center"/>
                                <TextBlock Text="ms" VerticalAlignment="Center"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                                <CheckBox x:Name="NotifyOnLongRunningQueriesCheckBox"
                                          Content="Long-running queries over"
                                          VerticalAlignment="Center"
                                          Checked="NotifyOnLongRunningQueriesCheckBox_Changed"
                                          Unchecked="NotifyOnLongRunningQueriesCheckBox_Changed"/>
                                <TextBox x:Name="LongRunningQueryThresholdTextBox"
                                         Width="50"
                                         Margin="8,0,8,0"
                                         VerticalAlignment="Center"
                                         TextAlignment="Center"/>
                                <TextBlock Text="minutes," VerticalAlignment="Center"/>
                                <TextBlock Text="return up to" VerticalAlignment="Center" Margin="8,0,8,0"/>
                                <TextBox x:Name="LongRunningQueryMaxResultsTextBox"
                                         Width="50"
                                         VerticalAlignment="Center"
                                         TextAlignment="Center"/>
                                <TextBlock Text="results" VerticalAlignment="Center" Margin="8,0,0,0"/>
                            </StackPanel>
                            <!-- Long Running Query Filters -->
                            <TextBlock Text="Long Running Query Filters" FontWeight="Bold" FontSize="12" Margin="0,12,0,6"/>
                            <CheckBox x:Name="LrqExcludeSpServerDiagnosticsCheckBox"
                                      Content="Exclude SP_SERVER_DIAGNOSTICS"
                                      Margin="0,0,0,4"/>
                            <CheckBox x:Name="LrqExcludeWaitForCheckBox"
                                      Content="Exclude WAITFOR / BROKER_RECEIVE_WAITFOR"
                                      Margin="0,0,0,4"/>
                            <CheckBox x:Name="LrqExcludeBackupsCheckBox"
                                      Content="Exclude backup waits (BACKUPTHREAD, BACKUPIO)"
                                      Margin="0,0,0,4"/>
                            <CheckBox x:Name="LrqExcludeMiscWaitsCheckBox"
                                      Content="Exclude miscellaneous waits (XE_LIVE_TARGET_TVF)"
                                      Margin="0,0,0,4"/>
                            <CheckBox x:Name="LrqExcludeCdcCheckBox"
                                      Content="Exclude CDC capture jobs (sp_MScdc_capture_job, sp_cdc_scan)"
                                      Margin="0,0,0,0"/>
                            <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                                <CheckBox x:Name="NotifyOnTempDbSpaceCheckBox"
                                          Content="TempDB space usage over"
                                          VerticalAlignment="Center"
                                          Checked="NotifyOnTempDbSpaceCheckBox_Changed"
                                          Unchecked="NotifyOnTempDbSpaceCheckBox_Changed"/>
                                <TextBox x:Name="TempDbSpaceThresholdTextBox"
                                         Width="50"
                                         Margin="8,0,8,0"
                                         VerticalAlignment="Center"
                                         TextAlignment="Center"/>
                                <TextBlock Text="%" VerticalAlignment="Center"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                                <CheckBox x:Name="NotifyOnLowDiskCheckBox"
                                          Content="Volume free space under"
                                          VerticalAlignment="Center"
                                          Checked="NotifyOnLowDiskCheckBox_Changed"
                                          Unchecked="NotifyOnLowDiskCheckBox_Changed"/>
                                <TextBox x:Name="LowDiskThresholdPercentTextBox"
                                         Width="50"
                                         Margin="8,0,4,0"
                                         VerticalAlignment="Center"
                                         TextAlignment="Center"/>
                                <TextBlock Text="% or" VerticalAlignment="Center"/>
                                <TextBox x:Name="LowDiskThresholdGbTextBox"
                                         Width="50"
                                         Margin="8,0,4,0"
                                         VerticalAlignment="Center"
                                         TextAlignment="Center"/>
                                <TextBlock Text="GB free (Azure SQL DB excluded)" VerticalAlignment="Center"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                                <CheckBox x:Name="NotifyOnLongRunningJobsCheckBox"
                                          Content="Agent jobs running over"
                                          VerticalAlignment="Center"
                                          Checked="NotifyOnLongRunningJobsCheckBox_Changed"
                                          Unchecked="NotifyOnLongRunningJobsCheckBox_Changed"/>
                                <TextBox x:Name="LongRunningJobMultiplierTextBox"
                                         Width="50"
                                         Margin="8,0,8,0"
                                         VerticalAlignment="Center"
                                         TextAlignment="Center"/>
                                <TextBlock Text="x historical average" VerticalAlignment="Center"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                                <CheckBox x:Name="NotifyOnFailedJobsCheckBox"
                                          Content="Agent job failed in the last"
                                          VerticalAlignment="Center"
                                          Checked="NotifyOnFailedJobsCheckBox_Changed"
                                          Unchecked="NotifyOnFailedJobsCheckBox_Changed"/>
                                <TextBox x:Name="FailedJobLookbackTextBox"
                                         Width="50"
                                         Margin="8,0,8,0"
                                         VerticalAlignment="Center"
                                         TextAlignment="Center"/>
                                <TextBlock Text="minutes" VerticalAlignment="Center"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                                <CheckBox x:Name="NotifyOnCollectionStoppedCheckBox"
                                          Content="Data collection has stopped (collector jobs disabled, Agent down, or collectors failing)"
                                          VerticalAlignment="Center"
                                          Checked="NotifyOnCollectionStoppedCheckBox_Changed"
                                          Unchecked="NotifyOnCollectionStoppedCheckBox_Changed"
                                          ToolTip="Warn when the PerformanceMonitor SQL Agent jobs are disabled, or nothing has collected recently (Agent service stopped or collectors erroring). The app checks this itself, so it still fires when collection is off."/>
                            </StackPanel>
                            <TextBlock x:Name="AlertPreviewText" FontSize="11" FontStyle="Italic" Foreground="Gray"
                                       TextWrapping="Wrap" Margin="0,12,0,0"/>
                            <StackPanel Orientation="Horizontal" Margin="0,12,0,0">
                                <TextBlock Text="Tray notification cooldown:" VerticalAlignment="Center" Margin="0,0,8,0"/>
                                <TextBox x:Name="AlertCooldownTextBox" Width="50" TextAlignment="Center" VerticalAlignment="Center"/>
                                <TextBlock Text="minutes" VerticalAlignment="Center" Margin="4,0,0,0"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                                <TextBlock Text="Email alert cooldown:" VerticalAlignment="Center" Margin="0,0,8,0"/>
                                <TextBox x:Name="EmailCooldownTextBox" Width="50" TextAlignment="Center" VerticalAlignment="Center"/>
                                <TextBlock Text="minutes" VerticalAlignment="Center" Margin="4,0,0,0"/>
                            </StackPanel>
                            <!-- #1141: deadlock/blocking notification delivery mode + per-event cap -->
                            <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                                <TextBlock Text="Deadlock/blocking delivery:" VerticalAlignment="Center" Margin="0,0,8,0"/>
                                <ComboBox x:Name="AlertDeliveryModeCombo" Width="260" VerticalAlignment="Center">
                                    <ComboBoxItem Content="Summary — one card per cycle" Tag="Summary"/>
                                    <ComboBoxItem Content="Per-event — one notification per incident" Tag="PerEvent"/>
                                </ComboBox>
                                <TextBlock Text="max" VerticalAlignment="Center" Margin="8,0,4,0"/>
                                <TextBox x:Name="AlertPerEventMaxTextBox" Width="50" TextAlignment="Center" VerticalAlignment="Center"/>
                                <TextBlock Text="/cycle" VerticalAlignment="Center" Margin="4,0,0,0"/>
                            </StackPanel>
                            <!-- Global Alert Filters -->
                            <TextBlock Text="Global Alert Filters" FontWeight="Bold" FontSize="12" Margin="0,16,0,6"/>
                            <TextBlock Text="Exclude databases from alerts (comma-separated):" Margin="0,0,0,4"/>
                            <TextBox x:Name="AlertExcludedDatabasesTextBox" Margin="0,0,0,4"/>
                            <TextBlock Text="e.g. DBAtools, msdb, tempdb — applies to blocking, deadlock, and long-running query alerts"
                                       FontSize="11" FontStyle="Italic" Foreground="Gray" TextWrapping="Wrap"/>

                            <!-- Mute Rules -->
                            <TextBlock Text="Alert Muting" FontWeight="Bold" FontSize="12" Margin="0,16,0,6"/>
                            <StackPanel Orientation="Horizontal" Margin="0,0,0,4">
                                <TextBlock Text="Default expiration for new mute rules:" VerticalAlignment="Center" Margin="0,0,8,0"/>
                                <ComboBox x:Name="MuteRuleDefaultExpirationCombo" Width="130" VerticalAlignment="Center">
                                    <ComboBoxItem Content="1 hour"/>
                                    <ComboBoxItem Content="24 hours"/>
                                    <ComboBoxItem Content="7 days"/>
                                    <ComboBoxItem Content="Never"/>
                                </ComboBox>
                            </StackPanel>
                            <Button x:Name="ManageMuteRulesButton" Content="Manage Mute Rules..."
                                    Click="ManageMuteRulesButton_Click" HorizontalAlignment="Left"
                                    Padding="12,4" Margin="0,0,0,4"/>
                            <TextBlock Text="Create rules to suppress specific recurring alerts while still logging them."
                                       FontSize="11" FontStyle="Italic" Foreground="Gray" TextWrapping="Wrap"/>
                            <CheckBox x:Name="LogAlertDismissalsCheckBox" Content="Log alert dismiss and mute actions"
                                      Margin="0,8,0,0"/>
                        </StackPanel>
                    </StackPanel>
                </ScrollViewer>
            </TabItem>

            <!-- Diagnostics Tab -->
            <TabItem Header="Diagnostics">
                <ScrollViewer VerticalScrollBarVisibility="Auto" Padding="15">
                    <StackPanel>
                        <!-- Query Logging Section -->
                        <TextBlock Text="Query Logging" FontWeight="Bold" FontSize="13" Margin="0,0,0,10"/>
                        <StackPanel Margin="0,0,0,20">
                            <StackPanel Orientation="Horizontal" Margin="0,0,0,8">
                                <CheckBox x:Name="LogSlowQueriesCheckBox"
                                          Content="Log slow queries (> 2 seconds)"
                                          VerticalAlignment="Center"
                                          Checked="LogSlowQueriesCheckBox_Changed"
                                          Unchecked="LogSlowQueriesCheckBox_Changed"/>
                                <Button Content="Open Log" Margin="15,0,0,0" Padding="10,3" Click="OpenQueryLogButton_Click"/>
                            </StackPanel>
                        </StackPanel>

                        <!-- Method Profiling Section -->
                        <TextBlock Text="Method Profiling" FontWeight="Bold" FontSize="13" Margin="0,0,0,10"/>
                        <StackPanel Margin="0,0,0,20">
                            <StackPanel Orientation="Horizontal">
                                <CheckBox x:Name="LogSlowMethodsCheckBox"
                                          Content="Log slow methods (> 500ms)"
                                          VerticalAlignment="Center"
                                          Checked="LogSlowMethodsCheckBox_Changed"
                                          Unchecked="LogSlowMethodsCheckBox_Changed"/>
                                <Button Content="Open Log" Margin="15,0,0,0" Padding="10,3" Click="OpenMethodProfileLogButton_Click"/>
                            </StackPanel>
                        </StackPanel>

                        <!-- MCP Server Section -->
                        <TextBlock Text="MCP Server (LLM Tool Access)" FontWeight="Bold" FontSize="13" Margin="0,0,0,10"/>
                        <StackPanel>
                            <CheckBox x:Name="McpEnabledCheckBox"
                                      Content="Enable MCP server"
                                      Margin="0,0,0,8"
                                      Checked="McpEnabledCheckBox_Changed"
                                      Unchecked="McpEnabledCheckBox_Changed"/>
                            <StackPanel Orientation="Horizontal" Margin="20,0,0,0">
                                <TextBlock Text="Port:" VerticalAlignment="Center" Margin="0,0,10,0"/>
                                <TextBox x:Name="McpPortTextBox"
                                         Width="70"
                                         VerticalAlignment="Center"
                                         TextAlignment="Center"/>
                                <Button Content="Auto" ToolTip="Find an available port"
                                        Margin="6,0,0,0" Padding="8,2"
                                        VerticalAlignment="Center"
                                        Click="AutoPortButton_Click"/>
                            </StackPanel>
                            <TextBlock x:Name="McpStatusText"
                                       FontStyle="Italic" Foreground="Gray" Margin="20,8,0,0" FontSize="11"/>
                            <StackPanel Orientation="Horizontal" Margin="20,8,0,0">
                                <Button x:Name="CopyMcpCommandButton" Content="Copy Setup Command" Padding="10,3"
                                        Click="CopyMcpCommandButton_Click"/>
                                <TextBlock Text="Works with Claude Code. Other LLMs may apply, but why would you?"
                                           FontStyle="Italic" Foreground="Gray" VerticalAlignment="Center" Margin="10,0,0,0" FontSize="11"/>
                            </StackPanel>
                        </StackPanel>

                        <!-- Automated Analysis -->
                        <TextBlock Text="Automated Analysis" FontWeight="Bold" FontSize="13" Margin="0,20,0,5"/>
                        <TextBlock Text="Runs the diagnostic inference engine on a schedule and records findings. Notifications are optional — analysis runs and persists findings even with notifications off. When notifications are on, high-severity findings are also routed to your configured channels (email, Teams, Slack)."
                                   TextWrapping="Wrap" Margin="0,0,0,10" FontStyle="Italic" Foreground="Gray"/>
                        <StackPanel>
                            <CheckBox x:Name="AnalysisEnabledCheckBox"
                                      Content="Enable automated analysis"
                                      Margin="0,0,0,8"/>
                            <CheckBox x:Name="AnalysisNotificationsEnabledCheckBox"
                                      Content="Send automated analysis notifications"
                                      Margin="0,0,0,8"/>
                            <Grid Margin="20,0,0,0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="160"/>
                                    <ColumnDefinition Width="90"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>

                                <TextBlock Grid.Row="0" Grid.Column="0" Text="Interval (minutes):" VerticalAlignment="Center" Margin="0,0,8,6"/>
                                <TextBox Grid.Row="0" Grid.Column="1" x:Name="AnalysisIntervalMinutesTextBox"
                                         VerticalAlignment="Center" TextAlignment="Center" Margin="0,0,0,6"
                                         ToolTip="How often to run analysis (5–360 minutes)."/>

                                <TextBlock Grid.Row="1" Grid.Column="0" Text="Notify at severity ≥:" VerticalAlignment="Center" Margin="0,0,8,0"/>
                                <TextBox Grid.Row="1" Grid.Column="1" x:Name="AnalysisNotifySeverityTextBox"
                                         VerticalAlignment="Center" TextAlignment="Center"
                                         ToolTip="Minimum finding severity to notify on (0.0–2.0). Default 1.5."/>
                                <TextBlock Grid.Row="1" Grid.Column="2" Text="0.0 = everything, 1.0 = concerning, 2.0 = critical only"
                                           FontStyle="Italic" Foreground="Gray" VerticalAlignment="Center" Margin="10,0,0,0" FontSize="11"/>
                            </Grid>
                            <TextBlock Text="Cooldown and timeout are configurable in preferences.json (defaults: 6h cooldown per finding, 120s timeout per server)."
                                       FontStyle="Italic" Foreground="Gray" Margin="0,10,0,0" FontSize="11" TextWrapping="Wrap"/>
                        </StackPanel>
                    </StackPanel>
                </ScrollViewer>
            </TabItem>

            <!-- Webhooks Tab -->
            <TabItem Header="Webhooks">
                <ScrollViewer VerticalScrollBarVisibility="Auto" Padding="15">
                    <StackPanel>
                        <!-- Microsoft Teams Section -->
                        <TextBlock Text="Microsoft Teams" FontWeight="Bold" FontSize="13" Margin="0,0,0,5"/>
                        <TextBlock Text="Send alert notifications to a Microsoft Teams channel via incoming webhook. Color-coded accent bars indicate alert severity."
                                   TextWrapping="Wrap" Margin="0,0,0,10" FontStyle="Italic" Foreground="Gray"/>

                        <CheckBox x:Name="TeamsWebhookEnabledCheckBox"
                                  Content="Enable Teams notifications"
                                  Margin="0,0,0,10"
                                  Checked="TeamsWebhookEnabledCheckBox_Changed"
                                  Unchecked="TeamsWebhookEnabledCheckBox_Changed"/>

                        <Grid Margin="20,0,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="110"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>

                            <TextBlock Grid.Row="0" Grid.Column="0" Text="Webhook URL:" VerticalAlignment="Center" Margin="0,0,8,6"/>
                            <TextBox Grid.Row="0" Grid.Column="1" x:Name="TeamsWebhookUrlTextBox" Margin="0,0,0,6"
                                     ToolTip="Paste your Teams Incoming Webhook or Power Automate Workflows URL here"/>

                            <TextBlock Grid.Row="1" Grid.Column="0" Text="Proxy Address:" VerticalAlignment="Center" Margin="0,0,8,6"/>
                            <TextBox Grid.Row="1" Grid.Column="1" x:Name="TeamsProxyAddressTextBox" Margin="0,0,0,6"
                                     ToolTip="Optional HTTP proxy (e.g. http://proxy.corp:8080). Leave blank for direct connection."/>

                            <StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal" Margin="0,4,0,0">
                                <Button x:Name="TestTeamsButton" Content="Send Test Notification" Padding="10,3"
                                        Click="TestTeamsButton_Click"/>
                                <TextBlock x:Name="TestTeamsStatusText" FontStyle="Italic" Foreground="Gray"
                                           VerticalAlignment="Center" Margin="10,0,0,0" FontSize="11"/>
                            </StackPanel>
                        </Grid>

                        <TextBlock Margin="20,10,0,0" TextWrapping="Wrap" FontSize="11" Foreground="Gray">
                            <Run Text="Setup: Create an Incoming Webhook connector or a Power Automate Workflows webhook for your Teams channel. "/>
                            <Hyperlink NavigateUri="https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook"
                                       RequestNavigate="Hyperlink_RequestNavigate">
                                Teams Webhooks Documentation
                            </Hyperlink>
                        </TextBlock>

                        <!-- Slack Section -->
                        <TextBlock Text="Slack" FontWeight="Bold" FontSize="13" Margin="0,25,0,5"/>
                        <TextBlock Text="Send alert notifications to a Slack channel via incoming webhook. Color-coded sidebar indicates alert severity."
                                   TextWrapping="Wrap" Margin="0,0,0,10" FontStyle="Italic" Foreground="Gray"/>

                        <CheckBox x:Name="SlackWebhookEnabledCheckBox"
                                  Content="Enable Slack notifications"
                                  Margin="0,0,0,10"
                                  Checked="SlackWebhookEnabledCheckBox_Changed"
                                  Unchecked="SlackWebhookEnabledCheckBox_Changed"/>

                        <Grid Margin="20,0,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="110"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>

                            <TextBlock Grid.Row="0" Grid.Column="0" Text="Webhook URL:" VerticalAlignment="Center" Margin="0,0,8,6"/>
                            <TextBox Grid.Row="0" Grid.Column="1" x:Name="SlackWebhookUrlTextBox" Margin="0,0,0,6"
                                     ToolTip="Paste your Slack Incoming Webhook URL here"/>

                            <TextBlock Grid.Row="1" Grid.Column="0" Text="Proxy Address:" VerticalAlignment="Center" Margin="0,0,8,6"/>
                            <TextBox Grid.Row="1" Grid.Column="1" x:Name="SlackProxyAddressTextBox" Margin="0,0,0,6"
                                     ToolTip="Optional HTTP proxy (e.g. http://proxy.corp:8080). Leave blank for direct connection."/>

                            <StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal" Margin="0,4,0,0">
                                <Button x:Name="TestSlackButton" Content="Send Test Notification" Padding="10,3"
                                        Click="TestSlackButton_Click"/>
                                <TextBlock x:Name="TestSlackStatusText" FontStyle="Italic" Foreground="Gray"
                                           VerticalAlignment="Center" Margin="10,0,0,0" FontSize="11"/>
                            </StackPanel>
                        </Grid>

                        <TextBlock Margin="20,10,0,0" TextWrapping="Wrap" FontSize="11" Foreground="Gray">
                            <Run Text="Setup: Create an Incoming Webhook app in your Slack workspace settings. "/>
                            <Hyperlink NavigateUri="https://api.slack.com/messaging/webhooks"
                                       RequestNavigate="Hyperlink_RequestNavigate">
                                Slack Webhooks Documentation
                            </Hyperlink>
                        </TextBlock>

                        <!-- Severity Color Reference -->
                        <TextBlock Text="Alert Severity Colors" FontWeight="Bold" FontSize="12" Margin="0,25,0,6"/>
                        <TextBlock TextWrapping="Wrap" FontSize="11" Foreground="Gray" Margin="0,0,0,2">
                            <Run Foreground="#DC2626" FontWeight="Bold" Text="&#x25A0; Red" /> — Critical (Deadlocks, Poison Waits, Server Unreachable)
                        </TextBlock>
                        <TextBlock TextWrapping="Wrap" FontSize="11" Foreground="Gray" Margin="0,0,0,2">
                            <Run Foreground="#D97706" FontWeight="Bold" Text="&#x25A0; Orange" /> — Alert / Warning (Blocking, Long-Running Queries, TempDB, Jobs)
                        </TextBlock>
                        <TextBlock TextWrapping="Wrap" FontSize="11" Foreground="Gray" Margin="0,0,0,2">
                            <Run Foreground="#F59E0B" FontWeight="Bold" Text="&#x25A0; Yellow" /> — Warning (High CPU)
                        </TextBlock>
                        <TextBlock TextWrapping="Wrap" FontSize="11" Foreground="Gray" Margin="0,0,0,2">
                            <Run Foreground="#16A34A" FontWeight="Bold" Text="&#x25A0; Green" /> — Resolved (Server Restored)
                        </TextBlock>
                        <TextBlock TextWrapping="Wrap" FontSize="11" Foreground="Gray">
                            <Run Foreground="#2eaef1" FontWeight="Bold" Text="&#x25A0; Blue" /> — Info
                        </TextBlock>
                    </StackPanel>
                </ScrollViewer>
            </TabItem>

            <!-- Email Alerts Tab -->
            <TabItem Header="Email Alerts">
                <ScrollViewer VerticalScrollBarVisibility="Auto" Padding="15">
                    <StackPanel>
                        <TextBlock Text="Email Alerts (Optional)" FontWeight="Bold" FontSize="13" Margin="0,0,0,5"/>
                        <TextBlock Text="Send email notifications alongside system tray alerts when performance thresholds are exceeded. Requires an SMTP server."
                                   TextWrapping="Wrap" Margin="0,0,0,15" FontStyle="Italic" Foreground="Gray"/>

                        <CheckBox x:Name="SmtpEnabledCheckBox"
                                  Content="Enable email alerts"
                                  Margin="0,0,0,10"
                                  Checked="SmtpEnabledCheckBox_Changed"
                                  Unchecked="SmtpEnabledCheckBox_Changed"/>

                        <Grid Margin="20,0,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="100"/>
                                <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="SMTP Server:" VerticalAlignment="Center" Margin="0,0,8,6"/>
                            <TextBox Grid.Row="0" Grid.Column="1" x:Name="SmtpServerTextBox" Margin="0,0,0,6"/>

                            <TextBlock Grid.Row="1" Grid.Column="0" Text="Port:" VerticalAlignment="Center" Margin="0,0,8,6"/>
                            <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal" Margin="0,0,0,6">
                                <TextBox x:Name="SmtpPortTextBox" Width="70" Text="587"/>
                                <CheckBox x:Name="SmtpSslCheckBox" Content="Use SSL/TLS" VerticalAlignment="Center" Margin="16,0,0,0" IsChecked="True"/>
                            </StackPanel>

                            <TextBlock Grid.Row="2" Grid.Column="0" Text="Username:" VerticalAlignment="Center" Margin="0,0,8,6"/>
                            <TextBox Grid.Row="2" Grid.Column="1" x:Name="SmtpUsernameTextBox" Margin="0,0,0,6"/>

                            <TextBlock Grid.Row="3" Grid.Column="0" Text="Password:" VerticalAlignment="Center" Margin="0,0,8,6"/>
                            <PasswordBox Grid.Row="3" Grid.Column="1" x:Name="SmtpPasswordBox" Margin="0,0,0,6"/>

                            <TextBlock Grid.Row="4" Grid.Column="0" Text="From Address:" VerticalAlignment="Center" Margin="0,0,8,6"/>
                            <TextBox Grid.Row="4" Grid.Column="1" x:Name="SmtpFromTextBox" Margin="0,0,0,6"/>

                            <TextBlock Grid.Row="5" Grid.Column="0" Text="Recipients:" VerticalAlignment="Center" Margin="0,0,8,6"/>
                            <TextBox Grid.Row="5" Grid.Column="1" x:Name="SmtpRecipientsTextBox" Margin="0,0,0,6"
                                     ToolTip="Comma-separated email addresses"/>

                            <StackPanel Grid.Row="6" Grid.Column="1" Orientation="Horizontal" Margin="0,4,0,0">
                                <Button x:Name="TestEmailButton" Content="Send Test Email" Padding="10,3"
                                        Click="TestEmailButton_Click"/>
                                <Button x:Name="ValidateSmtpButton" Content="Validate Settings" Padding="10,3" Margin="8,0,0,0"
                                        Click="ValidateSmtpButton_Click"/>
                                <TextBlock x:Name="TestEmailStatusText" FontStyle="Italic" Foreground="Gray"
                                           VerticalAlignment="Center" Margin="10,0,0,0" FontSize="11"/>
                            </StackPanel>
                        </Grid>
                    </StackPanel>
                </ScrollViewer>
            </TabItem>

        </TabControl>

        <!-- Buttons -->
        <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,15,0,0">
            <Button Content="OK" Width="75" Margin="0,0,10,0" Click="OkButton_Click" IsDefault="True"/>
            <Button Content="Cancel" Width="75" Click="CancelButton_Click" IsCancel="True"/>
        </StackPanel>
    </Grid>
</Window>
