<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">
            <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."/>
        </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>

        <!-- 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>

        <!-- 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>
