<Window x:Class="PerformanceMonitorDashboard.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:PerformanceMonitorDashboard.Controls"
        Title="Performance Monitor Dashboard"
        Height="700" Width="1400"
        MinHeight="600" MinWidth="900"
        WindowStartupLocation="CenterScreen"
        WindowState="Maximized"
        Background="{DynamicResource BackgroundBrush}">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
        <!-- Sidebar Toggle Button Style -->
        <Style x:Key="SidebarToggleButtonStyle" TargetType="Button">
            <Setter Property="Background" Value="{DynamicResource BackgroundBrush}"/>
            <Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}"/>
            <Setter Property="Width" Value="28"/>
            <Setter Property="Height" Value="28"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="3">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="{DynamicResource BackgroundLighterBrush}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition x:Name="SidebarColumn" Width="250"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <!-- Left Sidebar - Server List -->
        <Border Grid.Column="0"
                Background="{DynamicResource BackgroundDarkBrush}"
                BorderBrush="{DynamicResource BorderBrush}"
                BorderThickness="0,0,1,0">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <!-- Sidebar Header with Toggle Button -->
                <Border Grid.Row="0" Background="{DynamicResource BackgroundLightBrush}" Padding="12">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <StackPanel Grid.Column="0" x:Name="SidebarHeaderText">
                            <TextBlock Text="SQL Servers" FontSize="16" FontWeight="Bold" Foreground="{DynamicResource ForegroundBrush}"/>
                            <TextBlock Text="Double-click to connect" FontSize="11" Foreground="{DynamicResource ForegroundMutedBrush}" Margin="0,2,0,0"/>
                        </StackPanel>
                        <Button Grid.Column="1" x:Name="SidebarToggleButton"
                                Click="SidebarToggle_Click"
                                Style="{StaticResource SidebarToggleButtonStyle}"
                                VerticalAlignment="Center"
                                Margin="8,0,0,0">
                            <TextBlock x:Name="SidebarToggleIcon" Text="&#171;" FontSize="14" FontWeight="Bold"
                                       Foreground="{DynamicResource ForegroundBrush}"
                                       HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Button>
                    </Grid>
                </Border>

                <!-- Server List -->
                <ListView Grid.Row="1" x:Name="ServerListView"
                          SelectionMode="Single"
                          MouseDoubleClick="ServerListView_MouseDoubleClick"
                          Margin="8"
                          Background="Transparent"
                          BorderThickness="0">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Border BorderBrush="{DynamicResource BorderBrush}"
                                    BorderThickness="1"
                                    CornerRadius="4"
                                    Padding="8,6"
                                    Margin="2">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>

                                    <!-- Status Indicator (back-ported from Lite: Ellipse + DataTriggers) -->
                                    <Ellipse Grid.Column="0"
                                             Width="10" Height="10"
                                             Margin="0,6,8,0"
                                             VerticalAlignment="Top"
                                             ToolTip="{Binding Status.ErrorMessage}">
                                        <Ellipse.Style>
                                            <Style TargetType="Ellipse">
                                                <Setter Property="Fill" Value="{DynamicResource ForegroundMutedBrush}"/>
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding DotStatus}" Value="Online">
                                                        <Setter Property="Fill" Value="{DynamicResource SuccessBrush}"/>
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding DotStatus}" Value="Offline">
                                                        <Setter Property="Fill" Value="{DynamicResource ErrorBrush}"/>
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding DotStatus}" Value="Warning">
                                                        <Setter Property="Fill" Value="{DynamicResource WarningBrush}"/>
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Ellipse.Style>
                                    </Ellipse>

                                    <!-- Server detail stack -->
                                    <StackPanel Grid.Column="1">
                                        <TextBlock Text="{Binding DisplayName}" FontWeight="SemiBold" FontSize="13" Foreground="{DynamicResource ForegroundBrush}"/>
                                        <TextBlock Text="{Binding ServerName}" FontSize="13" Foreground="{DynamicResource AccentBrush}" Margin="0,-2,0,0" />
                                        <TextBlock Text="{Binding Description}" FontSize="10" Foreground="{DynamicResource ForegroundDimBrush}"
                                                   TextWrapping="Wrap" Margin="0,-1,0,0"/>
                                        <!-- Status Duration (Online for X / Offline for X) -->
                                        <TextBlock Text="{Binding StatusDurationDisplay, Mode=OneWay}"
                                                   FontSize="13"
                                                   FontWeight="SemiBold"
                                                   Foreground="{Binding StatusColor}"
                                                   Margin="0,-1,0,0"
                                                   Visibility="{Binding HasBeenChecked, Converter={StaticResource BoolToVisibilityConverter}}"/>
                                        <!-- Monitor Version -->
                                        <TextBlock Text="{Binding MonitorVersionDisplay, Mode=OneWay}"
                                                   FontSize="11"
                                                   Foreground="{DynamicResource ForegroundMutedBrush}"
                                                   Margin="0,-1,0,0"
                                                   Visibility="{Binding HasBeenChecked, Converter={StaticResource BoolToVisibilityConverter}}"/>
                                        <!-- Last Checked Timestamp -->
                                        <TextBlock FontSize="12" Foreground="{DynamicResource ForegroundDimBrush}" Margin="0,-1,0,0">
                                            <Run Text="{Binding LastCheckedDisplay, Mode=OneWay}"/>
                                        </TextBlock>
                                    </StackPanel>

                                    <!-- Favorite Star (right-aligned, back-ported from Lite) -->
                                    <TextBlock Grid.Column="2"
                                               Text="&#x2605;" FontSize="16"
                                               VerticalAlignment="Top"
                                               Foreground="#FFD700"
                                               Margin="4,2,0,0"
                                               Visibility="{Binding IsFavorite, Converter={StaticResource BoolToVisibilityConverter}}"/>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                    <ListView.ContextMenu>
                        <ContextMenu Opened="ServerContextMenu_Opened">
                            <MenuItem Header="Open in New Tab" Click="OpenServerTab_Click">
                                <MenuItem.Icon><TextBlock Text="&#x1F5C2;"/></MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Header="Check Connection" Click="CheckConnection_Click">
                                <MenuItem.Icon><TextBlock Text="&#x2705;"/></MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Header="Edit Server..." Click="EditServer_Click">
                                <MenuItem.Icon><TextBlock Text="&#x270F;"/></MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Header="Check Server Version" Click="CheckServerVersion_Click">
                                <MenuItem.Icon><TextBlock Text="&#x1F504;"/></MenuItem.Icon>
                            </MenuItem>
                            <Separator/>
                            <MenuItem x:Name="ToggleFavoriteMenuItem" Header="Set as Favorite" Click="ToggleFavorite_Click">
                                <MenuItem.Icon><TextBlock Text="&#x2B50;"/></MenuItem.Icon>
                            </MenuItem>
                            <Separator/>
                            <MenuItem Header="Remove Server" Click="RemoveServer_Click">
                                <MenuItem.Icon><TextBlock Text="&#x1F5D1;"/></MenuItem.Icon>
                            </MenuItem>
                        </ContextMenu>
                    </ListView.ContextMenu>
                </ListView>

                <!-- Sidebar Footer -->
                <StackPanel Grid.Row="2" x:Name="SidebarFooter" Margin="8">
                    <Button x:Name="NocOverviewButton"
                            Content="Overview"
                            Click="NocOverview_Click"
                            Style="{StaticResource AccentButton}"
                            Height="32"
                            Margin="0,0,0,8"/>
                    <Button x:Name="AlertsHistoryButton"
                            Click="AlertsHistory_Click"
                            Style="{StaticResource AccentButton}"
                            Height="32"
                            Margin="0,0,0,8">
                        <Grid>
                            <TextBlock Text="Alerts" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            <Border x:Name="AlertBadge"
                                    Background="#FF4444"
                                    CornerRadius="8"
                                    MinWidth="16" Height="16"
                                    Padding="4,0"
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Top"
                                    Margin="0,-4,-8,0"
                                    Visibility="Collapsed">
                                <TextBlock x:Name="AlertBadgeText"
                                           Text="0"
                                           FontSize="10"
                                           FontWeight="Bold"
                                           Foreground="White"
                                           HorizontalAlignment="Center"
                                           VerticalAlignment="Center"/>
                            </Border>
                        </Grid>
                    </Button>
                    <Button x:Name="OpenPlanViewerButton"
                            Click="OpenPlanViewer_Click"
                            Height="32"
                            Margin="0,0,0,8"
                            ToolTip="Open or paste an execution plan (independent of any server)">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="&#x1F50D;" FontSize="12" VerticalAlignment="Center" Margin="0,0,6,0"/>
                            <TextBlock Text="Open Plan Viewer" VerticalAlignment="Center"/>
                        </StackPanel>
                    </Button>
                    <Button x:Name="FinOpsButton"
                            Content="FinOps"
                            Click="FinOps_Click"
                            Style="{StaticResource AccentButton}"
                            Height="32"
                            Margin="0,0,0,8"/>
                    <Separator Margin="0,0,0,8"/>
                    <Button x:Name="RefreshAllButton"
                            Content="&#x21BB; Refresh All Status"
                            Click="RefreshAllStatus_Click"
                            Height="28"
                            Margin="0,0,0,8"/>
                    <Button x:Name="AddServerButton"
                            Content="+ Add Server"
                            Click="AddServer_Click"
                            Style="{StaticResource AccentButton}"
                            Height="32"
                            Margin="0,0,0,8"/>
                    <Button x:Name="ManageServersButton"
                            Content="Manage Servers..."
                            Click="ManageServers_Click"
                            Height="28"
                            Margin="0,0,0,12"/>
                    <Separator/>
                    <Button Click="ViewLogButton_Click"
                            Height="28"
                            Margin="0,8,0,6"
                            ToolTip="Open the current application log file">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="&#xE7BA;" FontFamily="Segoe MDL2 Assets" FontSize="12" VerticalAlignment="Center" Margin="0,0,6,0"/>
                            <TextBlock Text="View Log" VerticalAlignment="Center"/>
                        </StackPanel>
                    </Button>
                    <Button x:Name="SettingsButton"
                            Click="Settings_Click"
                            Height="28"
                            Margin="0,8,0,6">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="&#xE713;" FontFamily="Segoe MDL2 Assets" FontSize="12" VerticalAlignment="Center" Margin="0,0,6,0"/>
                            <TextBlock Text="Settings" VerticalAlignment="Center"/>
                        </StackPanel>
                    </Button>
                    <Button x:Name="HelpButton"
                            Click="Help_Click"
                            Height="28">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="&#xE897;" FontFamily="Segoe MDL2 Assets" FontSize="12" VerticalAlignment="Center" Margin="0,0,6,0"/>
                            <TextBlock Text="Help / About" VerticalAlignment="Center"/>
                        </StackPanel>
                    </Button>
                </StackPanel>
            </Grid>
        </Border>

        <!-- Right Panel - Server Tabs -->
        <Grid Grid.Column="1" Background="{DynamicResource BackgroundBrush}">
            <!-- Server Tabs (including Overview tab) -->
            <TabControl x:Name="ServerTabControl" Margin="0" Background="{DynamicResource BackgroundBrush}">
            </TabControl>
        </Grid>
    </Grid>
</Window>
