<Window x:Class="PerformanceMonitorDashboard.AddServerDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Add SQL Server"
        Width="500"
        WindowStartupLocation="Manual"
        ResizeMode="CanResizeWithGrip"
        Background="{DynamicResource BackgroundBrush}"
        Foreground="{DynamicResource ForegroundBrush}">
    <Grid Margin="20">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <ScrollViewer Grid.Row="0" VerticalScrollBarVisibility="Auto">
        <Grid>
            <Grid.RowDefinitions>
                <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}"/>

            <!-- Form Fields -->
            <StackPanel Grid.Row="1">
                <!-- Server Name/Address -->
                <TextBlock Text="Server Name/Address:" FontWeight="Bold" Margin="0,0,0,4"
                           Foreground="{DynamicResource ForegroundBrush}"/>
                <TextBox x:Name="ServerNameTextBox" Margin="0,0,0,12"
                         ToolTip="e.g., localhost, server.domain.com, 192.168.1.100, server\instance"/>

                <!-- Display Name -->
                <TextBlock Text="Display Name (optional):" FontWeight="Bold" Margin="0,0,0,4"
                           Foreground="{DynamicResource ForegroundBrush}"/>
                <TextBox x:Name="DisplayNameTextBox" Margin="0,0,0,12"
                         ToolTip="Friendly name to display in the server list"/>

                <!-- Authentication Type -->
                <TextBlock Text="Authentication:" FontWeight="Bold" Margin="0,0,0,4"
                           Foreground="{DynamicResource ForegroundBrush}"/>
                <StackPanel Margin="0,0,0,12">
                    <RadioButton x:Name="WindowsAuthRadio" Content="Windows Authentication"
                                 GroupName="Auth" IsChecked="True"
                                 Foreground="{DynamicResource ForegroundBrush}"
                                 Checked="AuthType_Changed" Margin="0,0,0,4"/>
                    <RadioButton x:Name="SqlAuthRadio" Content="SQL Server Authentication"
                                 GroupName="Auth"
                                 Foreground="{DynamicResource ForegroundBrush}"
                                 Checked="AuthType_Changed" Margin="0,0,0,4"/>
                    <RadioButton x:Name="EntraMfaAuthRadio" Content="Microsoft Entra MFA"
                                 GroupName="Auth"
                                 Foreground="{DynamicResource ForegroundBrush}"
                                 Checked="AuthType_Changed"
                                 ToolTip="Interactive authentication with MFA for Azure SQL Database."/>
                </StackPanel>

                <!-- SQL Authentication Fields -->
                <StackPanel x:Name="SqlAuthPanel" Visibility="Collapsed" Margin="0,0,0,12">
                    <TextBlock Text="Username:" Margin="0,0,0,4"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="UsernameTextBox" Margin="0,0,0,8"/>

                    <TextBlock Text="Password:" Margin="0,0,0,4"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <PasswordBox x:Name="PasswordBox"/>
                </StackPanel>

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

                <!-- Connection Options -->
                <TextBlock Text="Connection Options:" FontWeight="Bold" Margin="0,0,0,4"
                           Foreground="{DynamicResource ForegroundBrush}"/>
                <StackPanel Margin="0,0,0,12">
                    <!-- Encryption Mode -->
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,6">
                        <TextBlock Text="Encryption:" Width="80" VerticalAlignment="Center"
                                   Foreground="{DynamicResource ForegroundBrush}"/>
                        <ComboBox x:Name="EncryptModeComboBox" Width="150"
                                  SelectedIndex="1"
                                  ToolTip="Optional: Encrypt if server supports it&#x0a;Mandatory: Always encrypt (fails if not supported)&#x0a;Strict: Full validation required">
                            <ComboBoxItem Content="Optional"/>
                            <ComboBoxItem Content="Mandatory"/>
                            <ComboBoxItem Content="Strict"/>
                        </ComboBox>
                    </StackPanel>

                    <!-- Trust Server Certificate -->
                    <CheckBox x:Name="TrustServerCertificateCheckBox"
                              Content="Trust server certificate (skip certificate validation)"
                              IsChecked="False"
                              Foreground="{DynamicResource ForegroundBrush}"
                              ToolTip="When checked, accepts any server certificate without validation.&#x0a;Uncheck for stricter security (requires valid certificate)."
                              Margin="0,0,0,6"/>

                    <!-- Read-Only Intent -->
                    <CheckBox x:Name="ReadOnlyIntentCheckBox"
                              Content="Read-only intent (for AG listeners and readable replicas)"
                              Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,6"
                              ToolTip="Sets ApplicationIntent=ReadOnly. Required when connecting via an AG listener or Azure failover group endpoint to route to a readable secondary."/>

                    <!-- Multi-Subnet Failover -->
                    <CheckBox x:Name="MultiSubnetFailoverCheckBox"
                              Content="Multi-subnet failover (for AG listeners and FCIs)"
                              Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,6"
                              ToolTip="Sets MultiSubnetFailover=true. Connects to all IPs in parallel during failover. Recommended for AG listeners and failover cluster instances spanning multiple subnets."/>

                    <!-- Is Favorite -->
                    <CheckBox x:Name="IsFavoriteCheckBox" Content="Mark as favorite (appears at top of list)"
                              Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,6"/>

                    <!-- Monthly Cost -->
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,6">
                        <TextBlock Text="Monthly Cost ($):" Width="110" VerticalAlignment="Center"
                                   Foreground="{DynamicResource ForegroundBrush}"/>
                        <TextBox x:Name="MonthlyCostTextBox" 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>

                    <!-- Description -->
                    <TextBlock Text="Description (optional):" Margin="0,4,0,4"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="DescriptionTextBox" Height="60" TextWrapping="Wrap"
                             AcceptsReturn="True" VerticalScrollBarVisibility="Auto"/>
                </StackPanel>
            </StackPanel>

            <!-- Database Status Panel -->
            <Border x:Name="DatabaseStatusPanel" Grid.Row="2" Visibility="Collapsed"
                    Background="{DynamicResource BackgroundLightBrush}"
                    CornerRadius="4" Padding="12" Margin="0,0,0,10">
                <StackPanel>
                    <TextBlock x:Name="ConnectionInfoText" TextWrapping="Wrap" Margin="0,0,0,4"
                               FontWeight="SemiBold"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBlock x:Name="DatabaseStatusText" TextWrapping="Wrap" Margin="0,0,0,8"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,4">
                        <Button x:Name="InstallUpgradeButton" Content="Install Now"
                                Style="{StaticResource AccentButton}"
                                Click="InstallOrUpgrade_Click" Margin="0,0,12,0"/>
                        <TextBlock x:Name="SkipInstallText" Text="Skip, just add server"
                                   VerticalAlignment="Center" Cursor="Hand"
                                   Foreground="{DynamicResource ForegroundMutedBrush}"
                                   MouseLeftButtonUp="SkipInstall_Click">
                            <TextBlock.TextDecorations>
                                <TextDecoration Location="Underline"/>
                            </TextBlock.TextDecorations>
                        </TextBlock>
                    </StackPanel>
                </StackPanel>
            </Border>

            <!-- Installation Progress Panel -->
            <StackPanel x:Name="InstallationPanel" Grid.Row="3" Visibility="Collapsed" Margin="0,0,0,10">
                <!-- Advanced Options Expander (shown before install starts) -->
                <Expander x:Name="AdvancedOptionsExpander" Header="Advanced Options" IsExpanded="False"
                          Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,8">
                    <StackPanel Margin="16,8,0,0">
                        <CheckBox x:Name="CleanInstallCheckBox"
                                  Content="Perform clean install (drops existing database)"
                                  Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
                        <CheckBox x:Name="ResetScheduleCheckBox"
                                  Content="Reset collection schedule to defaults"
                                  Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
                        <CheckBox x:Name="ValidationCheckBox"
                                  Content="Run validation after install" IsChecked="True"
                                  Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
                        <CheckBox x:Name="InstallDepsCheckBox"
                                  Content="Install community dependencies" IsChecked="True"
                                  Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,4"/>
                    </StackPanel>
                </Expander>

                <ProgressBar x:Name="InstallProgressBar" Height="6" Margin="0,0,0,6"
                             Minimum="0" Maximum="100" Value="0"
                             Foreground="{DynamicResource AccentBrush}"
                             Background="{DynamicResource BackgroundLighterBrush}"/>
                <TextBlock x:Name="InstallStatusText" TextWrapping="Wrap" Margin="0,0,0,6"
                           Foreground="{DynamicResource ForegroundBrush}"/>
                <TextBox x:Name="InstallLogTextBox" FontFamily="Consolas" FontSize="11"
                         MaxHeight="180" IsReadOnly="True" TextWrapping="Wrap"
                         VerticalScrollBarVisibility="Auto"
                         Background="{DynamicResource BackgroundDarkBrush}"
                         Foreground="{DynamicResource ForegroundBrush}"
                         BorderBrush="{DynamicResource BorderBrush}"
                         Margin="0,0,0,6"/>
                <Button x:Name="CancelInstallButton" Content="Cancel" Click="CancelInstall_Click"
                        HorizontalAlignment="Left" Margin="0,0,0,4"/>
            </StackPanel>

            <!-- Monitoring Credentials Panel -->
            <Border x:Name="MonitoringCredsPanel" Grid.Row="4" Visibility="Collapsed"
                    Background="{DynamicResource BackgroundLightBrush}"
                    CornerRadius="4" Padding="12" Margin="0,0,0,10">
                <StackPanel>
                    <TextBlock Text="The installer used your credentials to set up the database. You can use different credentials for ongoing Dashboard monitoring if needed."
                               TextWrapping="Wrap" Margin="0,0,0,8"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <CheckBox x:Name="UseSameCredsCheckBox"
                              Content="Use same credentials for monitoring" IsChecked="True"
                              Foreground="{DynamicResource ForegroundBrush}"
                              Checked="UseSameCredsCheckBox_Changed"
                              Unchecked="UseSameCredsCheckBox_Changed"
                              Margin="0,0,0,8"/>
                    <StackPanel x:Name="MonitorCredsFieldsPanel" Visibility="Collapsed">
                        <TextBlock Text="Monitoring Username:" Margin="0,0,0,4"
                                   Foreground="{DynamicResource ForegroundBrush}"/>
                        <TextBox x:Name="MonitorUsernameTextBox" Margin="0,0,0,8"/>
                        <TextBlock Text="Monitoring Password:" Margin="0,0,0,4"
                                   Foreground="{DynamicResource ForegroundBrush}"/>
                        <PasswordBox x:Name="MonitorPasswordBox"/>
                    </StackPanel>
                </StackPanel>
            </Border>

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

        </Grid>
        </ScrollViewer>

        <!-- Buttons pinned to bottom -->
        <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,15,0,0">
            <Button x:Name="ViewReportButton" Content="View Report" Margin="0,0,8,0"
                    Click="ViewReport_Click" Visibility="Collapsed"/>
            <Button x:Name="CheckForUpdatesButton" Content="Check for Updates" Margin="0,0,8,0" Click="CheckForUpdates_Click"/>
            <Button x:Name="TestConnectionButton" Content="Test Connection" Margin="0,0,8,0" Click="TestConnection_Click"/>
            <Button x:Name="SaveButton" Content="Save" MinWidth="80" Height="30" Padding="12,0" Margin="0,0,10,0" Click="Save_Click" IsDefault="True" Style="{StaticResource AccentButton}"/>
            <Button Content="Cancel" Width="80" Height="30" Click="Cancel_Click" IsCancel="True"/>
        </StackPanel>
    </Grid>
</Window>
