<Window x:Class="PerformanceMonitorDashboard.ManageServersWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Manage Servers"
        Height="450" Width="960"
        WindowStartupLocation="CenterOwner"
        ResizeMode="CanResizeWithGrip"
        Background="{DynamicResource BackgroundBrush}">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
        <ContextMenu x:Key="DataGridContextMenu">
            <MenuItem Header="Edit..." Click="EditServer_Click"/>
            <MenuItem Header="Toggle Favorite" Click="ToggleFavorite_Click"/>
            <MenuItem Header="Check Server Version" Click="CheckForUpdates_Click"/>
            <MenuItem Header="Excluded Databases" Click="ExcludedDatabases_Click"/>
            <MenuItem Header="Purge Now" Click="PurgeNow_Click"/>
            <MenuItem Header="Remove" Click="RemoveServer_Click"
                      Foreground="{DynamicResource ErrorBrush}"/>
            <Separator/>
            <MenuItem Header="Copy Cell" Click="CopyCell_Click">
                <MenuItem.Icon><TextBlock Text="&#x1F4CB;"/></MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Copy Row" Click="CopyRow_Click">
                <MenuItem.Icon><TextBlock Text="&#x1F4C4;"/></MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Copy All Rows" Click="CopyAllRows_Click">
                <MenuItem.Icon><TextBlock Text="&#x1F4D1;"/></MenuItem.Icon>
            </MenuItem>
            <Separator/>
            <MenuItem Header="Export to CSV..." Click="ExportToCsv_Click">
                <MenuItem.Icon><TextBlock Text="&#x1F4CA;"/></MenuItem.Icon>
            </MenuItem>
        </ContextMenu>
    </Window.Resources>
    <Grid Margin="16">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- Header -->
        <TextBlock Grid.Row="0" Text="SQL Server Connections" FontWeight="Bold" FontSize="16"
                   Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,0,12"/>

        <!-- Server List -->
        <Border Grid.Row="1" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" CornerRadius="4">
            <DataGrid x:Name="ServersDataGrid"
                      AutoGenerateColumns="False"
                      IsReadOnly="True"
                      SelectionMode="Single"
                      CanUserAddRows="False"
                      CanUserDeleteRows="False"
                      CanUserReorderColumns="False"
                      CanUserSortColumns="True"
                      HeadersVisibility="Column"
                      GridLinesVisibility="Horizontal"
                      Background="{DynamicResource BackgroundBrush}"
                      RowBackground="{DynamicResource BackgroundBrush}"
                      AlternatingRowBackground="{DynamicResource BackgroundLightBrush}"
                      BorderThickness="0"
                      MouseDoubleClick="ServersDataGrid_MouseDoubleClick"
                  ContextMenu="{StaticResource DataGridContextMenu}">
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="" Width="30" CanUserSort="False">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="★" FontSize="14" Foreground="#FFD700"
                                           HorizontalAlignment="Center" VerticalAlignment="Center"
                                           Visibility="{Binding IsFavorite, Converter={StaticResource BoolToVisibilityConverter}}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Header="Display Name" Binding="{Binding DisplayName}" Width="150"/>
                    <DataGridTextColumn Header="Server" Binding="{Binding ServerName}" Width="180"/>
                    <DataGridTextColumn Header="Authentication" Binding="{Binding AuthenticationDisplay}" Width="120"/>
                    <DataGridTextColumn Header="Installed Version" Binding="{Binding InstalledVersion}" Width="130"/>
                    <DataGridTextColumn Header="Monthly Cost ($)" Binding="{Binding MonthlyCostUsd, StringFormat='{}{0:N0}'}" Width="120">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="HorizontalAlignment" Value="Right"/>
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="Last Connected" Binding="{Binding LastConnected, StringFormat='{}{0:g}'}" Width="130"/>
                </DataGrid.Columns>
            </DataGrid>
        </Border>

        <!-- Buttons -->
        <Grid Grid.Row="2" Margin="0,16,0,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>

            <!-- Action Buttons -->
            <StackPanel Grid.Column="0" Orientation="Horizontal">
                <Button Content="Add Server..." Width="100" Height="30" Margin="0,0,8,0" Click="AddServer_Click"/>
                <Button Content="Edit..." Width="80" Height="30" Margin="0,0,8,0" Click="EditServer_Click"/>
                <Button x:Name="ToggleFavoriteButton" Content="Toggle Favorite" Width="110" Height="30" Margin="0,0,8,0" Click="ToggleFavorite_Click"/>
                <Button x:Name="CheckUpdatesButton" Content="Check Server Version" Width="140" Height="30" Margin="0,0,8,0" Click="CheckForUpdates_Click"/>
                <Button Content="Excluded Databases" Width="140" Height="30" Margin="0,0,8,0" Click="ExcludedDatabases_Click"/>
                <Button Content="Purge Now" Width="100" Height="30" Margin="0,0,8,0" Click="PurgeNow_Click"/>
                <Button Content="Remove" Width="80" Height="30" Margin="0,0,8,0" Click="RemoveServer_Click"
                        Foreground="{DynamicResource ErrorBrush}"/>
            </StackPanel>

            <!-- Close Button -->
            <Button Grid.Column="1" Content="Close" Width="80" Height="30" Click="Close_Click" IsCancel="True"/>
        </Grid>
    </Grid>
</Window>
