<Window x:Class="PerformanceMonitorLite.Windows.AddServerDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Add SQL Server"
        SizeToContent="Height" Width="450"
        WindowStartupLocation="CenterOwner"
        ResizeMode="NoResize"
        Icon="/EDD.ico"
        Background="{DynamicResource BackgroundBrush}">
    <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="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- Header -->
        <TextBlock Grid.Row="0" Text="Add SQL Server Connection" FontSize="18" FontWeight="Bold" Margin="0,0,0,15"
                   Foreground="{DynamicResource ForegroundBrush}"/>

        <!-- Server Name -->
        <TextBlock Grid.Row="1" Text="Server Name / Address" FontWeight="Bold" Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
        <TextBox Grid.Row="2" x:Name="ServerNameBox" Margin="0,0,0,12"/>

        <!-- Display Name -->
        <TextBlock Grid.Row="3" Text="Display Name (optional)" FontWeight="Bold" Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
        <TextBox Grid.Row="4" x:Name="DisplayNameBox" Margin="0,0,0,12"/>

        <!-- Authentication -->
        <TextBlock Grid.Row="5" Text="Authentication" FontWeight="Bold" Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
        <StackPanel Grid.Row="6" Margin="0,0,0,12">
            <!-- Credential source: inline (per-server) vs a shared credential profile -->
            <RadioButton x:Name="ConfigureOnServerRadio" Content="Configure credentials on this server"
                         Foreground="{DynamicResource ForegroundBrush}" IsChecked="True"
                         Checked="CredentialSource_Changed" Margin="0,0,0,4"/>
            <RadioButton x:Name="UseProfileRadio" Content="Use a credential profile"
                         Foreground="{DynamicResource ForegroundBrush}"
                         Checked="CredentialSource_Changed" Margin="0,0,0,4"
                         ToolTip="Use a named, shared credential (set up under Manage Servers → Credential Profiles). The profile fully overrides this server's auth type and credentials."/>

            <!-- Profile picker (shown when 'Use a credential profile' is selected) -->
            <StackPanel x:Name="ProfilePickerPanel" Visibility="Collapsed" Margin="0,4,0,0">
                <TextBlock Text="Credential Profile" Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
                <ComboBox x:Name="ProfileComboBox" DisplayMemberPath="PickerDisplay"/>
                <TextBlock x:Name="NoProfilesNote" TextWrapping="Wrap" FontSize="11" Visibility="Collapsed"
                           Foreground="{DynamicResource ForegroundMutedBrush}" Margin="0,4,0,0"
                           Text="No credential profiles exist yet. Create one under Manage Servers → Credential Profiles."/>
            </StackPanel>

            <!-- Inline per-server auth radios (hidden when a profile is chosen) -->
            <StackPanel x:Name="InlineAuthRadios" Margin="0,4,0,0">
                <RadioButton x:Name="WindowsAuthRadio" Content="Windows Authentication" Foreground="{DynamicResource ForegroundBrush}"
                             IsChecked="True" Checked="AuthMode_Changed" Margin="0,0,0,4"/>
                <RadioButton x:Name="SqlAuthRadio" Content="SQL Server Authentication" Foreground="{DynamicResource ForegroundBrush}"
                             Checked="AuthMode_Changed" Margin="0,0,0,4"/>
                <RadioButton x:Name="EntraMfaAuthRadio" Content="Microsoft Entra MFA" Foreground="{DynamicResource ForegroundBrush}"
                             Checked="AuthMode_Changed"
                             ToolTip="Interactive authentication with MFA for Azure SQL Database."/>
                <RadioButton x:Name="ServicePrincipalAuthRadio" Content="Azure — Service Principal" Foreground="{DynamicResource ForegroundBrush}"
                             Checked="AuthMode_Changed" Margin="0,4,0,0"
                             ToolTip="Non-interactive Azure SQL auth using an Entra app registration (client id + secret)."/>
                <RadioButton x:Name="ManagedIdentityAuthRadio" Content="Azure — Managed Identity" Foreground="{DynamicResource ForegroundBrush}"
                             Checked="AuthMode_Changed" Margin="0,4,0,0"
                             ToolTip="Non-interactive Azure SQL auth using the managed identity of the Azure resource running Lite."/>
            </StackPanel>
        </StackPanel>

        <!-- SQL Auth Credentials -->
        <StackPanel Grid.Row="7" x:Name="SqlCredentialsPanel" Visibility="Collapsed" Margin="0,0,0,12">
            <TextBlock Text="Username" Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
            <TextBox x:Name="UsernameBox" Margin="0,0,0,8"/>
            <TextBlock Text="Password" Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
            <PasswordBox x:Name="PasswordBox"/>
        </StackPanel>

        <!-- Entra MFA Credentials -->
        <StackPanel Grid.Row="7" x:Name="EntraMfaPanel" Visibility="Collapsed" Margin="0,0,0,12">
            <TextBlock Text="Username (optional)" Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
            <TextBox x:Name="EntraMfaUsernameBox" Margin="0,0,0,8"
                     ToolTip="Optional: Pre-populate the authentication dialog with this email"/>
        </StackPanel>

        <!-- Service Principal Credentials -->
        <StackPanel Grid.Row="7" x:Name="ServicePrincipalPanel" Visibility="Collapsed" Margin="0,0,0,12">
            <TextBlock Text="Client (Application) ID" Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
            <TextBox x:Name="AzureClientIdBox" Margin="0,0,0,8"
                     ToolTip="The Application (client) ID of the Entra app registration / service principal."/>
            <TextBlock Text="Client Secret" Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
            <PasswordBox x:Name="AzureClientSecretBox" Margin="0,0,0,8"/>
            <TextBlock Text="Tenant ID (optional)" Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
            <TextBox x:Name="AzureTenantIdBox" Margin="0,0,0,4"
                     ToolTip="Optional. Stored for reference; the tenant is resolved automatically from the target server."/>
        </StackPanel>

        <!-- Managed Identity Credentials -->
        <StackPanel Grid.Row="7" x:Name="ManagedIdentityPanel" Visibility="Collapsed" Margin="0,0,0,12">
            <TextBlock Text="User-Assigned Identity Client ID (optional)" Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
            <TextBox x:Name="ManagedIdentityClientIdBox" Margin="0,0,0,8"
                     ToolTip="Leave blank to use the system-assigned managed identity. Set to a client id to use a specific user-assigned identity."/>
            <TextBlock TextWrapping="Wrap" Foreground="{DynamicResource ForegroundMutedBrush}" FontSize="11"
                       Text="Managed Identity only works when Lite is running on an Azure VM / resource that has a managed identity assigned — not on a typical workstation. Use Service Principal for non-interactive auth from a workstation."/>
        </StackPanel>

        <!-- Connection Options -->
        <TextBlock Grid.Row="8" Text="Connection Options" FontWeight="Bold" Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
        <StackPanel Grid.Row="9" Margin="0,0,0,12">
            <StackPanel Orientation="Horizontal" Margin="0,0,0,6">
                <TextBlock Text="Encryption:" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center" Width="80"/>
                <ComboBox x:Name="EncryptModeComboBox" Width="150" SelectedIndex="1">
                    <ComboBoxItem Content="Optional"/>
                    <ComboBoxItem Content="Mandatory"/>
                    <ComboBoxItem Content="Strict"/>
                </ComboBox>
            </StackPanel>
            <CheckBox x:Name="TrustCertCheckBox" Content="Trust server certificate (skip certificate validation)"
                      Foreground="{DynamicResource ForegroundBrush}" IsChecked="False"/>
            <CheckBox x:Name="EnabledCheckBox" Content="Enable data collection for this server"
                      Foreground="{DynamicResource ForegroundBrush}" IsChecked="True" Margin="0,6,0,0"/>
            <CheckBox x:Name="FavoriteCheckBox" Content="Mark as favorite (appears at top of list)"
                      Foreground="{DynamicResource ForegroundBrush}" Margin="0,6,0,0"/>
            <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                <TextBlock Text="Description:" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center" Width="80"/>
                <TextBox x:Name="DescriptionTextBox" Width="250"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                <TextBlock Text="Database:" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center" Width="80"/>
                <TextBox x:Name="DatabaseNameBox" Width="250"
                         ToolTip="Required for Azure SQL Database. Leave empty for on-premises SQL Server."/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                <TextBlock Text="Utility DB:" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center" Width="80"/>
                <TextBox x:Name="UtilityDatabaseBox" Width="250"
                         ToolTip="Database where community stored procedures (sp_IndexCleanup) are installed. Leave empty to use the connection database."/>
            </StackPanel>
            <CheckBox x:Name="ReadOnlyIntentCheckBox" Content="Read-only intent (for AG listeners and readable replicas)"
                      Foreground="{DynamicResource ForegroundBrush}" Margin="0,6,0,0"
                      ToolTip="Sets ApplicationIntent=ReadOnly. Required when connecting via an AG listener or Azure failover group endpoint to route to a readable secondary."/>
            <CheckBox x:Name="MultiSubnetFailoverCheckBox" Content="Multi-subnet failover (for AG listeners and FCIs)"
                      Foreground="{DynamicResource ForegroundBrush}" Margin="0,6,0,0"
                      ToolTip="Sets MultiSubnetFailover=true. Connects to all IPs in parallel during failover. Recommended for AG listeners and failover cluster instances spanning multiple subnets."/>
            <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                <TextBlock Text="Monthly Cost ($):" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center" Width="110"/>
                <TextBox x:Name="MonthlyCostBox" Width="100" TextAlignment="Right" Text="0"
                         ToolTip="What this server costs per month (license, compute, storage combined). Used for FinOps cost attribution. Leave 0 to hide cost columns."/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                <TextBlock Text="Alert delivery:" Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center" Width="110"/>
                <ComboBox x:Name="AlertDeliveryOverrideBox" Width="240" SelectedIndex="0"
                          ToolTip="How deadlock/blocking alerts for THIS server are delivered. 'Use global setting' follows the app-wide Alerts setting; override to force Summary or Per-event for just this server.">
                    <ComboBoxItem Content="Use global setting"/>
                    <ComboBoxItem Content="Summary — one card per cycle"/>
                    <ComboBoxItem Content="Per-event — one notification per incident"/>
                </ComboBox>
            </StackPanel>
        </StackPanel>

        <!-- Status -->
        <TextBlock Grid.Row="10" x:Name="StatusText" Foreground="{DynamicResource ForegroundMutedBrush}"
                   TextWrapping="Wrap" VerticalAlignment="Bottom" Margin="0,0,0,8"/>

        <!-- Buttons -->
        <StackPanel Grid.Row="11" Orientation="Horizontal" HorizontalAlignment="Right">
            <Button x:Name="TestButton" Content="Test Connection" Click="TestButton_Click" Margin="0,0,8,0"/>
            <Button x:Name="SaveButton" Content="Save" Width="80" Height="30" Click="SaveButton_Click" Style="{StaticResource AccentButton}" Margin="0,0,8,0"/>
            <Button Content="Cancel" Width="80" Height="30" Click="CancelButton_Click"/>
        </StackPanel>
    </Grid>
</Window>
