<Window x:Class="PerformanceMonitorLite.Windows.SettingsWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Settings"
        Height="750" Width="750"
        MinHeight="500" MinWidth="600"
        ResizeMode="CanResizeWithGrip"
        WindowStartupLocation="CenterOwner"
        Icon="/EDD.ico"
        Background="{DynamicResource BackgroundBrush}">
    <Window.Resources>
        <ResourceDictionary>
            <ContextMenu x:Key="DataGridContextMenu">
                <MenuItem Header="Copy Cell" Click="CopyCell_Click"/>
                <MenuItem Header="Copy Row" Click="CopyRow_Click"/>
                <MenuItem Header="Copy All Rows" Click="CopyAllRows_Click"/>
                <Separator/>
                <MenuItem Header="Export to CSV..." Click="ExportToCsv_Click"/>
            </ContextMenu>
        </ResourceDictionary>
    </Window.Resources>
    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
    <Grid Margin="16">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- Header -->
        <StackPanel Grid.Row="0" Margin="0,0,0,16">
            <TextBlock Text="Settings" FontSize="18" FontWeight="Bold" Foreground="{DynamicResource ForegroundBrush}"/>
            <TextBlock Text="Configure data collection schedules and application settings"
                       FontSize="12" Foreground="{DynamicResource ForegroundMutedBrush}" Margin="0,4,0,0"/>
        </StackPanel>

        <!-- Collection Control -->
        <Border Grid.Row="1" Background="{DynamicResource BackgroundLightBrush}" CornerRadius="4" Padding="12" Margin="0,0,0,12">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <StackPanel Grid.Column="0">
                    <TextBlock Text="Data Collection" FontSize="14" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBlock x:Name="CollectionStatusText" Text="Status: Active" FontSize="12"
                               Foreground="{DynamicResource ForegroundMutedBrush}" Margin="0,4,0,0"/>
                </StackPanel>
                <Button Grid.Column="1" x:Name="PauseResumeButton" Content="Pause Collection"
                        Click="PauseResumeButton_Click" VerticalAlignment="Center"/>
            </Grid>
        </Border>

        <!-- MCP Server Settings -->
        <Border Grid.Row="2" Background="{DynamicResource BackgroundLightBrush}" CornerRadius="4" Padding="12" Margin="0,0,0,12">
            <StackPanel>
                <TextBlock Text="MCP Server (LLM Tool Access)" FontSize="14" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundBrush}"/>
                <TextBlock Text="Allows LLM clients (Claude Code, etc.) to query monitoring data via the Model Context Protocol."
                           FontSize="12" Foreground="{DynamicResource ForegroundMutedBrush}" Margin="0,4,0,0" TextWrapping="Wrap"/>
                <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                    <CheckBox x:Name="McpEnabledCheckBox" Content="Enable MCP server" VerticalAlignment="Center"
                              Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBlock Text="Port:" VerticalAlignment="Center" Margin="20,0,6,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="McpPortTextBox" Width="70" Text="5151" VerticalAlignment="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" FontSize="12" Foreground="{DynamicResource ForegroundMutedBrush}" Margin="0,6,0,0"/>
                <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                    <Button x:Name="CopyMcpCommandButton" Content="Copy Setup Command" Click="CopyMcpCommandButton_Click"/>
                    <TextBlock Text="Works with Claude Code. Other LLMs may apply, but why would you?"
                               FontSize="11" FontStyle="Italic" Foreground="{DynamicResource ForegroundMutedBrush}"
                               VerticalAlignment="Center" Margin="8,0,0,0"/>
                </StackPanel>
            </StackPanel>
        </Border>

        <!-- Dashboard Defaults -->
        <Border Grid.Row="3" Background="{DynamicResource BackgroundLightBrush}" CornerRadius="4" Padding="12" Margin="0,0,0,12">
            <StackPanel>
                <TextBlock Text="Dashboard Defaults" FontSize="14" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundBrush}"/>
                <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                    <TextBlock Text="Default time range:" VerticalAlignment="Center"
                               Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,0"/>
                    <ComboBox x:Name="DefaultTimeRangeCombo" Width="140" VerticalAlignment="Center">
                        <ComboBoxItem Content="Last 1 hour"/>
                        <ComboBoxItem Content="Last 4 hours"/>
                        <ComboBoxItem Content="Last 12 hours"/>
                        <ComboBoxItem Content="Last 24 hours"/>
                        <ComboBoxItem Content="Last 7 days"/>
                    </ComboBox>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                    <TextBlock Text="Connection timeout:" VerticalAlignment="Center"
                               Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,0"/>
                    <TextBox x:Name="ConnectionTimeoutBox" Width="50" Text="5" VerticalAlignment="Center"/>
                    <TextBlock Text="seconds (5–60, increase for VPN/remote connections)"
                               FontSize="11" FontStyle="Italic" Foreground="{DynamicResource ForegroundMutedBrush}"
                               VerticalAlignment="Center" Margin="6,0,0,0"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                    <TextBlock Text="CSV separator:" VerticalAlignment="Center"
                               Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,0"/>
                    <ComboBox x:Name="CsvSeparatorCombo" Width="140" VerticalAlignment="Center">
                        <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="{DynamicResource ForegroundMutedBrush}"
                               VerticalAlignment="Center" Margin="6,0,0,0" x:Name="CsvSeparatorHint"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                    <TextBlock Text="Show timestamps in:" VerticalAlignment="Center"
                               Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,0"/>
                    <ComboBox x:Name="TimeDisplayModeCombo" Width="140" VerticalAlignment="Center">
                        <ComboBoxItem Content="Server Time" Tag="ServerTime"/>
                        <ComboBoxItem Content="Local Time" Tag="LocalTime"/>
                        <ComboBoxItem Content="UTC" Tag="UTC"/>
                    </ComboBox>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                    <TextBlock Text="Color theme:" VerticalAlignment="Center"
                               Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,0"/>
                    <ComboBox x:Name="ColorThemeCombo" Width="140" VerticalAlignment="Center"
                              SelectionChanged="ColorThemeCombo_SelectionChanged">
                        <ComboBoxItem Content="Dark" Tag="Dark"/>
                        <ComboBoxItem Content="Light" Tag="Light"/>
                        <ComboBoxItem Content="Cool Breeze" Tag="CoolBreeze"/>
                    </ComboBox>
                </StackPanel>
            </StackPanel>
        </Border>

        <!-- Notifications -->
        <Border Grid.Row="4" Background="{DynamicResource BackgroundLightBrush}" CornerRadius="4" Padding="12" Margin="0,0,0,12">
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Notifications" FontSize="14" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"/>
                    <Button x:Name="RestoreAlertDefaultsButton" Content="Restore Defaults" Margin="12,0,0,0"
                            FontSize="11" VerticalAlignment="Center" Click="RestoreAlertDefaultsButton_Click"/>
                </StackPanel>
                <CheckBox x:Name="MinimizeToTrayCheckBox" Content="Minimize to system tray" Margin="0,8,0,0"
                          Foreground="{DynamicResource ForegroundBrush}"/>
                <CheckBox x:Name="AlertsEnabledCheckBox" Content="Enable notifications" Margin="0,8,0,0"
                          Foreground="{DynamicResource ForegroundBrush}" Checked="AlertsEnabledCheckBox_Changed" Unchecked="AlertsEnabledCheckBox_Changed"/>
                <CheckBox x:Name="NotifyConnectionCheckBox" Content="Connection lost / restored" Margin="20,6,0,0"
                          Foreground="{DynamicResource ForegroundBrush}"/>
                <StackPanel Orientation="Horizontal" Margin="20,6,0,0">
                    <CheckBox x:Name="AlertCpuCheckBox" Content="CPU above" VerticalAlignment="Center"
                              Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="AlertCpuThresholdBox" Width="45" Text="80" Margin="6,0,0,0" VerticalAlignment="Center"/>
                    <TextBlock Text="%" VerticalAlignment="Center" Margin="2,0,0,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBlock Text="measured as" VerticalAlignment="Center" Margin="8,0,4,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <ComboBox x:Name="AlertCpuModeBox" Width="220" 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="20,6,0,0">
                    <CheckBox x:Name="AlertBlockingCheckBox" Content="Blocking sessions &#x2265;" VerticalAlignment="Center"
                              Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="AlertBlockingThresholdBox" Width="45" Text="1" Margin="6,0,0,0" VerticalAlignment="Center"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="20,6,0,0">
                    <CheckBox x:Name="AlertDeadlockCheckBox" Content="Deadlocks &#x2265;" VerticalAlignment="Center"
                              Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="AlertDeadlockThresholdBox" Width="45" Text="1" Margin="6,0,0,0" VerticalAlignment="Center"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="20,6,0,0">
                    <CheckBox x:Name="AlertPoisonWaitCheckBox" Content="Poison waits (THREADPOOL, etc.) avg wait &#x2265;" VerticalAlignment="Center"
                              Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="AlertPoisonWaitThresholdBox" Width="45" Text="500" Margin="6,0,0,0" VerticalAlignment="Center"/>
                    <TextBlock Text="ms" VerticalAlignment="Center" Margin="2,0,0,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="20,6,0,0">
                    <CheckBox x:Name="AlertLongRunningQueryCheckBox" Content="Long-running queries over" VerticalAlignment="Center"
                              Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="AlertLongRunningQueryThresholdBox" Width="45" Text="30" Margin="6,0,0,0" VerticalAlignment="Center"/>
                    <TextBlock Text="minutes," VerticalAlignment="Center" Margin="2,0,0,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBlock Text="return up to" VerticalAlignment="Center" Margin="6,0,6,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="AlertLongRunningQueryMaxResultsBox" Width="45" Text="5" VerticalAlignment="Center"/>
                    <TextBlock Text="results" VerticalAlignment="Center" Margin="2,0,0,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                </StackPanel>
                <!-- Long Running Query Filters -->
                <TextBlock Text="Long Running Query Filters" FontWeight="SemiBold" FontSize="12"
                           Foreground="{DynamicResource ForegroundBrush}" Margin="20,10,0,4"/>
                <CheckBox x:Name="LrqExcludeSpServerDiagnosticsCheckBox"
                          Content="Exclude SP_SERVER_DIAGNOSTICS"
                          Foreground="{DynamicResource ForegroundBrush}" Margin="20,0,0,4"/>
                <CheckBox x:Name="LrqExcludeWaitForCheckBox"
                          Content="Exclude WAITFOR / BROKER_RECEIVE_WAITFOR"
                          Foreground="{DynamicResource ForegroundBrush}" Margin="20,0,0,4"/>
                <CheckBox x:Name="LrqExcludeBackupsCheckBox"
                          Content="Exclude backup waits (BACKUPTHREAD, BACKUPIO)"
                          Foreground="{DynamicResource ForegroundBrush}" Margin="20,0,0,4"/>
                <CheckBox x:Name="LrqExcludeMiscWaitsCheckBox"
                          Content="Exclude miscellaneous waits (XE_LIVE_TARGET_TVF)"
                          Foreground="{DynamicResource ForegroundBrush}" Margin="20,0,0,4"/>
                <CheckBox x:Name="LrqExcludeCdcCheckBox"
                          Content="Exclude CDC capture jobs (sp_MScdc_capture_job, sp_cdc_scan)"
                          Foreground="{DynamicResource ForegroundBrush}" Margin="20,0,0,0"/>
                <StackPanel Orientation="Horizontal" Margin="20,6,0,0">
                    <CheckBox x:Name="AlertTempDbSpaceCheckBox" Content="TempDB space usage over" VerticalAlignment="Center"
                              Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="AlertTempDbSpaceThresholdBox" Width="45" Text="80" Margin="6,0,0,0" VerticalAlignment="Center"/>
                    <TextBlock Text="%" VerticalAlignment="Center" Margin="2,0,0,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="20,6,0,0">
                    <CheckBox x:Name="AlertLowDiskCheckBox" Content="Volume free space under" VerticalAlignment="Center"
                              Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="AlertLowDiskThresholdPercentBox" Width="45" Text="10" Margin="6,0,0,0" VerticalAlignment="Center"/>
                    <TextBlock Text="% or" VerticalAlignment="Center" Margin="2,0,0,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="AlertLowDiskThresholdGbBox" Width="45" Text="5" Margin="6,0,0,0" VerticalAlignment="Center"/>
                    <TextBlock Text="GB free (Azure SQL DB excluded)" VerticalAlignment="Center" Margin="2,0,0,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="20,6,0,0">
                    <CheckBox x:Name="AlertLongRunningJobCheckBox" Content="Agent jobs running over" VerticalAlignment="Center"
                              Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="AlertLongRunningJobMultiplierBox" Width="35" Text="3" Margin="6,0,0,0" VerticalAlignment="Center"/>
                    <TextBlock Text="x historical average" VerticalAlignment="Center" Margin="2,0,0,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="20,6,0,0">
                    <CheckBox x:Name="AlertFailedJobCheckBox" Content="Agent job failed in the last" VerticalAlignment="Center"
                              Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="AlertFailedJobLookbackBox" Width="45" Text="60" Margin="6,0,0,0" VerticalAlignment="Center"/>
                    <TextBlock Text="minutes" VerticalAlignment="Center" Margin="2,0,0,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="20,10,0,0">
                    <TextBlock Text="Tray notification cooldown:" VerticalAlignment="Center" Margin="0,0,8,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="AlertCooldownBox" Width="40" TextAlignment="Center" VerticalAlignment="Center"/>
                    <TextBlock Text="minutes" VerticalAlignment="Center" Margin="4,0,0,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="20,6,0,0">
                    <TextBlock Text="Email alert cooldown:" VerticalAlignment="Center" Margin="0,0,8,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="EmailCooldownBox" Width="40" TextAlignment="Center" VerticalAlignment="Center"/>
                    <TextBlock Text="minutes" VerticalAlignment="Center" Margin="4,0,0,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                </StackPanel>
                <!-- #1141: deadlock/blocking notification delivery mode + per-event cap -->
                <StackPanel Orientation="Horizontal" Margin="20,6,0,0">
                    <TextBlock Text="Deadlock/blocking delivery:" VerticalAlignment="Center" Margin="0,0,8,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <ComboBox x:Name="AlertDeliveryModeBox" 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"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="AlertPerEventMaxBox" Width="40" TextAlignment="Center" VerticalAlignment="Center"/>
                    <TextBlock Text="/cycle" VerticalAlignment="Center" Margin="4,0,0,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                </StackPanel>
                <TextBlock x:Name="AlertPreviewText" FontSize="11" FontStyle="Italic"
                           Foreground="{DynamicResource ForegroundMutedBrush}" TextWrapping="Wrap" Margin="20,10,0,0"/>
                <!-- Global Alert Filters -->
                <TextBlock Text="Global Alert Filters" FontWeight="SemiBold" FontSize="12"
                           Foreground="{DynamicResource ForegroundBrush}" Margin="20,14,0,4"/>
                <TextBlock Text="Exclude databases from alerts (comma-separated):"
                           Foreground="{DynamicResource ForegroundBrush}" Margin="20,0,0,4" FontSize="12"/>
                <TextBox x:Name="AlertExcludedDatabasesBox" Margin="20,0,0,4"/>
                <TextBlock Text="e.g. DBAtools, msdb, tempdb — applies to blocking, deadlock, and long-running query alerts"
                           FontSize="11" FontStyle="Italic" Foreground="{DynamicResource ForegroundMutedBrush}"
                           Margin="20,0,0,0" TextWrapping="Wrap"/>

                <!-- Mute Rules -->
                <TextBlock Text="Alert Muting" FontWeight="SemiBold" FontSize="12"
                           Foreground="{DynamicResource ForegroundBrush}" Margin="20,14,0,4"/>
                <StackPanel Orientation="Horizontal" Margin="20,0,0,4">
                    <TextBlock Text="Default expiration for new mute rules:" VerticalAlignment="Center" Margin="0,0,8,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <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="20,4,0,4"/>
                <TextBlock Text="Create rules to suppress specific recurring alerts while still logging them."
                           FontSize="11" FontStyle="Italic" Foreground="{DynamicResource ForegroundMutedBrush}"
                           Margin="20,0,0,0" TextWrapping="Wrap"/>
                <CheckBox x:Name="LogAlertDismissalsCheckBox" Content="Log alert dismiss and mute actions"
                          Margin="20,8,0,0" Foreground="{DynamicResource ForegroundBrush}"/>

                <!-- Automated Analysis -->
                <TextBlock Text="Automated Analysis" FontWeight="SemiBold" FontSize="12"
                           Foreground="{DynamicResource ForegroundBrush}" Margin="20,14,0,4"/>
                <TextBlock Text="Periodically run the triage engine and record findings in the Alerts tab. Notifications below are optional — analysis runs and stores findings even with notifications off."
                           FontSize="11" FontStyle="Italic" Foreground="{DynamicResource ForegroundMutedBrush}"
                           Margin="20,0,0,4" TextWrapping="Wrap"/>
                <CheckBox x:Name="AnalysisEnabledCheckBox" Content="Enable automated analysis"
                          Margin="20,4,0,0" Foreground="{DynamicResource ForegroundBrush}"/>
                <CheckBox x:Name="AnalysisNotificationsCheckBox" Content="Send scheduled analysis notifications"
                          Margin="20,4,0,0" Foreground="{DynamicResource ForegroundBrush}"/>
                <StackPanel Orientation="Horizontal" Margin="20,6,0,0">
                    <TextBlock Text="Run analysis every" VerticalAlignment="Center" Margin="0,0,8,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="AnalysisIntervalBox" Width="45" TextAlignment="Center" VerticalAlignment="Center"/>
                    <TextBlock Text="minutes" VerticalAlignment="Center" Margin="4,0,0,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="20,6,0,0">
                    <TextBlock Text="Notify on finding severity at or above" VerticalAlignment="Center" Margin="0,0,8,0"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="AnalysisNotifySeverityBox" Width="45" TextAlignment="Center" VerticalAlignment="Center"/>
                    <TextBlock Text="(0.0 - 2.0)" VerticalAlignment="Center" Margin="4,0,0,0"
                               Foreground="{DynamicResource ForegroundMutedBrush}"/>
                </StackPanel>
                <TextBlock Text="Requires SMTP or a webhook configured below for delivery; findings are recorded in the Alerts tab either way."
                           FontSize="11" FontStyle="Italic" Foreground="{DynamicResource ForegroundMutedBrush}"
                           Margin="20,4,0,0" TextWrapping="Wrap"/>
            </StackPanel>
        </Border>

        <!-- Email Alerts (Optional) -->
        <Border Grid.Row="5" Background="{DynamicResource BackgroundLightBrush}" CornerRadius="4" Padding="12" Margin="0,0,0,12">
            <StackPanel>
                <TextBlock Text="Email Alerts (Optional)" FontSize="14" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundBrush}"/>
                <TextBlock Text="Send email notifications alongside system tray alerts. Requires an SMTP server."
                           FontSize="12" Foreground="{DynamicResource ForegroundMutedBrush}" Margin="0,4,0,0" TextWrapping="Wrap"/>
                <CheckBox x:Name="SmtpEnabledCheckBox" Content="Enable email alerts" Margin="0,8,0,0"
                          Foreground="{DynamicResource ForegroundBrush}" Checked="SmtpEnabledCheckBox_Changed" Unchecked="SmtpEnabledCheckBox_Changed"/>
                <Grid Margin="20,6,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"
                               Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,4"/>
                    <TextBox Grid.Row="0" Grid.Column="1" x:Name="SmtpServerBox" Margin="0,0,0,4"/>

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

                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Username:" VerticalAlignment="Center"
                               Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,4"/>
                    <TextBox Grid.Row="2" Grid.Column="1" x:Name="SmtpUsernameBox" Margin="0,0,0,4"/>

                    <TextBlock Grid.Row="3" Grid.Column="0" Text="Password:" VerticalAlignment="Center"
                               Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,4"/>
                    <PasswordBox Grid.Row="3" Grid.Column="1" x:Name="SmtpPasswordBox" Margin="0,0,0,4"/>

                    <TextBlock Grid.Row="4" Grid.Column="0" Text="From Address:" VerticalAlignment="Center"
                               Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,4"/>
                    <TextBox Grid.Row="4" Grid.Column="1" x:Name="SmtpFromBox" Margin="0,0,0,4"/>

                    <TextBlock Grid.Row="5" Grid.Column="0" Text="Recipients:" VerticalAlignment="Center"
                               Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,4"/>
                    <TextBox Grid.Row="5" Grid.Column="1" x:Name="SmtpRecipientsBox" Margin="0,0,0,4"
                             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" Click="TestEmailButton_Click"/>
                        <Button x:Name="ValidateSmtpButton" Content="Validate Settings" Margin="8,0,0,0"
                                Click="ValidateSmtpButton_Click"/>
                        <TextBlock x:Name="SmtpStatusText" FontSize="11" FontStyle="Italic"
                                   Foreground="{DynamicResource ForegroundMutedBrush}" VerticalAlignment="Center" Margin="8,0,0,0"/>
                    </StackPanel>
                </Grid>
            </StackPanel>
        </Border>

        <!-- Webhooks (Teams / Slack) -->
        <Border Grid.Row="6" Background="{DynamicResource BackgroundLightBrush}" CornerRadius="4" Padding="12" Margin="0,0,0,12">
            <StackPanel>
                <!-- Microsoft Teams -->
                <TextBlock Text="Microsoft Teams Notifications" FontSize="14" FontWeight="SemiBold" Foreground="{DynamicResource ForegroundBrush}"/>
                <TextBlock Text="Send alert notifications to a Teams channel via incoming webhook. Color-coded accent bars indicate alert severity."
                           FontSize="12" Foreground="{DynamicResource ForegroundMutedBrush}" Margin="0,4,0,0" TextWrapping="Wrap"/>
                <CheckBox x:Name="TeamsWebhookEnabledCheckBox" Content="Enable Teams notifications" Margin="0,8,0,0"
                          Foreground="{DynamicResource ForegroundBrush}" Checked="TeamsWebhookEnabledCheckBox_Changed" Unchecked="TeamsWebhookEnabledCheckBox_Changed"/>
                <Grid Margin="20,6,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"
                               Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,4"/>
                    <TextBox Grid.Row="0" Grid.Column="1" x:Name="TeamsWebhookUrlBox" Margin="0,0,0,4"
                             ToolTip="Paste your Teams Incoming Webhook or Power Automate Workflows URL here"/>

                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Proxy Address:" VerticalAlignment="Center"
                               Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,4"/>
                    <TextBox Grid.Row="1" Grid.Column="1" x:Name="TeamsProxyAddressBox" Margin="0,0,0,4"
                             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" Click="TestTeamsButton_Click"/>
                        <TextBlock x:Name="TeamsStatusText" FontSize="11" FontStyle="Italic"
                                   Foreground="{DynamicResource ForegroundMutedBrush}" VerticalAlignment="Center" Margin="8,0,0,0"/>
                    </StackPanel>
                </Grid>
                <TextBlock Margin="20,8,0,0" TextWrapping="Wrap" FontSize="11" Foreground="{DynamicResource ForegroundMutedBrush}">
                    <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 -->
                <TextBlock Text="Slack Notifications" FontSize="14" FontWeight="SemiBold"
                           Foreground="{DynamicResource ForegroundBrush}" Margin="0,20,0,0"/>
                <TextBlock Text="Send alert notifications to a Slack channel via incoming webhook. Color-coded sidebar indicates alert severity."
                           FontSize="12" Foreground="{DynamicResource ForegroundMutedBrush}" Margin="0,4,0,0" TextWrapping="Wrap"/>
                <CheckBox x:Name="SlackWebhookEnabledCheckBox" Content="Enable Slack notifications" Margin="0,8,0,0"
                          Foreground="{DynamicResource ForegroundBrush}" Checked="SlackWebhookEnabledCheckBox_Changed" Unchecked="SlackWebhookEnabledCheckBox_Changed"/>
                <Grid Margin="20,6,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"
                               Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,4"/>
                    <TextBox Grid.Row="0" Grid.Column="1" x:Name="SlackWebhookUrlBox" Margin="0,0,0,4"
                             ToolTip="Paste your Slack Incoming Webhook URL here"/>

                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Proxy Address:" VerticalAlignment="Center"
                               Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,4"/>
                    <TextBox Grid.Row="1" Grid.Column="1" x:Name="SlackProxyAddressBox" Margin="0,0,0,4"
                             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" Click="TestSlackButton_Click"/>
                        <TextBlock x:Name="SlackStatusText" FontSize="11" FontStyle="Italic"
                                   Foreground="{DynamicResource ForegroundMutedBrush}" VerticalAlignment="Center" Margin="8,0,0,0"/>
                    </StackPanel>
                </Grid>
                <TextBlock Margin="20,8,0,0" TextWrapping="Wrap" FontSize="11" Foreground="{DynamicResource ForegroundMutedBrush}">
                    <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="SemiBold" FontSize="12"
                           Foreground="{DynamicResource ForegroundBrush}" Margin="0,16,0,4"/>
                <TextBlock TextWrapping="Wrap" FontSize="11" Foreground="{DynamicResource ForegroundMutedBrush}" 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="{DynamicResource ForegroundMutedBrush}" 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="{DynamicResource ForegroundMutedBrush}" Margin="0,0,0,2">
                    <Run Foreground="#F59E0B" FontWeight="Bold" Text="&#x25A0; Yellow" /> — Warning (High CPU)
                </TextBlock>
                <TextBlock TextWrapping="Wrap" FontSize="11" Foreground="{DynamicResource ForegroundMutedBrush}" Margin="0,0,0,2">
                    <Run Foreground="#16A34A" FontWeight="Bold" Text="&#x25A0; Green" /> — Resolved (Server Restored)
                </TextBlock>
                <TextBlock TextWrapping="Wrap" FontSize="11" Foreground="{DynamicResource ForegroundMutedBrush}">
                    <Run Foreground="#2eaef1" FontWeight="Bold" Text="&#x25A0; Blue" /> — Info
                </TextBlock>
            </StackPanel>
        </Border>

        <!-- Collector Schedules (Per-Server) -->
        <Border Grid.Row="7" Background="{DynamicResource BackgroundLightBrush}" CornerRadius="4" Padding="12" Margin="0,0,0,12">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <TextBlock Grid.Row="0" Text="Collector Schedules" FontSize="14" FontWeight="SemiBold"
                           Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
                <TextBlock Grid.Row="1" Text="Each server can have its own collection schedule, or inherit the default."
                           FontSize="12" Foreground="{DynamicResource ForegroundMutedBrush}" Margin="0,0,0,8"/>

                <DataGrid Grid.Row="2" x:Name="ServerScheduleGrid"
                          AutoGenerateColumns="False" IsReadOnly="True"
                          CanUserAddRows="False" CanUserDeleteRows="False"
                          SelectionMode="Single"
                          Background="Transparent" BorderThickness="0"
                          HeadersVisibility="Column" GridLinesVisibility="Horizontal"
                          MinHeight="80" MaxHeight="200"
                          MouseDoubleClick="ServerScheduleGrid_DoubleClick">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Server" Binding="{Binding ServerName}" Width="*"/>
                        <DataGridTextColumn Header="Preset" Binding="{Binding Preset}" Width="110"/>
                        <DataGridTextColumn Header="Status" Binding="{Binding Status}" Width="100"/>
                    </DataGrid.Columns>
                </DataGrid>

                <StackPanel Grid.Row="3" Orientation="Horizontal" Margin="0,8,0,0">
                    <Button Content="Edit Selected..." x:Name="EditServerScheduleButton" Click="EditServerSchedule_Click" Margin="0,0,8,0"/>
                    <Button Content="Edit Default..." x:Name="EditDefaultScheduleButton" Click="EditDefaultSchedule_Click" Margin="0,0,16,0"/>
                    <TextBlock Text="Default preset:" Foreground="{DynamicResource ForegroundBrush}" FontSize="12"
                               VerticalAlignment="Center" Margin="0,0,8,0"/>
                    <TextBlock x:Name="DefaultPresetText" FontSize="12" FontWeight="SemiBold"
                               Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center" Margin="0,0,16,0"/>
                    <Button Content="Apply Default to All" Click="ApplyDefaultToAll_Click"/>
                </StackPanel>
            </Grid>
        </Border>

        <!-- Buttons -->
        <StackPanel Grid.Row="8" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,12,0,0">
            <Button Content="Save" Click="SaveButton_Click" Style="{StaticResource AccentButton}" Margin="0,0,8,0"/>
            <Button Content="Close" Click="CloseButton_Click"/>
        </StackPanel>
    </Grid>
    </ScrollViewer>
</Window>
