<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:ClaudeDo.Ui.ViewModels.Modals"
        xmlns:converters="using:ClaudeDo.Ui.Converters"
        x:Class="ClaudeDo.Ui.Views.Modals.WorktreesOverviewModalView"
        x:DataType="vm:WorktreesOverviewModalViewModel"
        Title="{Binding Title}"
        Width="900" Height="560" MinWidth="640" MinHeight="360"
        CanResize="True"
        WindowStartupLocation="CenterOwner"
        Background="{DynamicResource SurfaceBrush}"
        WindowDecorations="BorderOnly"
        ExtendClientAreaToDecorationsHint="True"
        ExtendClientAreaTitleBarHeightHint="-1">

  <Window.Resources>
    <converters:WorktreeStateColorConverter x:Key="WorktreeStateColor"/>
    <DataTemplate x:Key="WorktreeRowTemplate" x:DataType="vm:WorktreeOverviewRowViewModel">
      <Border Classes="wt-row"
              Classes.selected="{Binding IsSelected}"
              Tapped="OnRowTapped">
        <Border.ContextMenu>
          <ContextMenu>
            <MenuItem Header="Show diff"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).ShowDiffCommand}"
                      CommandParameter="{Binding}"/>
            <MenuItem Header="Open in Explorer"
                      IsEnabled="{Binding PathExistsOnDisk}"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).OpenInExplorerCommand}"
                      CommandParameter="{Binding}"/>
            <MenuItem Header="Jump to task"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).JumpToTaskCommand}"
                      CommandParameter="{Binding}"/>
            <Separator/>
            <MenuItem Header="Merge…"
                      IsEnabled="{Binding IsActive}"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).MergeCommand}"
                      CommandParameter="{Binding}"/>
            <MenuItem Header="Discard"
                      IsEnabled="{Binding IsActive}"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).DiscardCommand}"
                      CommandParameter="{Binding}"/>
            <MenuItem Header="Keep"
                      IsEnabled="{Binding IsActive}"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).KeepCommand}"
                      CommandParameter="{Binding}"/>
            <Separator/>
            <MenuItem Header="Copy branch"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).CopyBranchCommand}"
                      CommandParameter="{Binding}"/>
            <MenuItem Header="Copy path"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).CopyPathCommand}"
                      CommandParameter="{Binding}"/>
            <Separator/>
            <MenuItem Header="Force remove"
                      Foreground="#EF5350"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).ForceRemoveCommand}"
                      CommandParameter="{Binding}"/>
          </ContextMenu>
        </Border.ContextMenu>
        <Grid ColumnDefinitions="*,90,80,80">
          <StackPanel Grid.Column="0" Orientation="Vertical" Spacing="2">
            <TextBlock Text="{Binding TaskTitle}" FontWeight="SemiBold"/>
            <StackPanel Orientation="Horizontal" Spacing="4">
              <TextBlock Text="{Binding TaskStatus}" FontSize="10"
                         Foreground="{DynamicResource TextFaintBrush}"/>
              <TextBlock Text="•" FontSize="10" Foreground="{DynamicResource TextFaintBrush}"
                         IsVisible="{Binding !PathExistsOnDisk}"/>
              <TextBlock Text="phantom" FontSize="10" Foreground="#EF5350"
                         IsVisible="{Binding !PathExistsOnDisk}"
                         ToolTip.Tip="Directory missing on disk"/>
            </StackPanel>
          </StackPanel>
          <Border Grid.Column="1" CornerRadius="3" Padding="6,2" VerticalAlignment="Center"
                  Background="{Binding State, Converter={StaticResource WorktreeStateColor}}">
            <TextBlock Text="{Binding State}" FontSize="10" Foreground="White"
                       HorizontalAlignment="Center"/>
          </Border>
          <TextBlock Grid.Column="2" Text="{Binding DiffStat}" VerticalAlignment="Center"
                     FontFamily="{DynamicResource MonoFont}" FontSize="11"
                     Foreground="{DynamicResource TextDimBrush}"/>
          <TextBlock Grid.Column="3" Text="{Binding AgeText}" VerticalAlignment="Center"
                     FontSize="11" Foreground="{DynamicResource TextDimBrush}"/>
        </Grid>
      </Border>
    </DataTemplate>
  </Window.Resources>

  <Window.Styles>
    <Style Selector="Border.wt-row">
      <Setter Property="Padding"         Value="12,10"/>
      <Setter Property="CornerRadius"    Value="8"/>
      <Setter Property="Background"      Value="Transparent"/>
      <Setter Property="BorderBrush"     Value="{DynamicResource LineBrush}"/>
      <Setter Property="BorderThickness" Value="1"/>
      <Setter Property="Margin"          Value="0,0,0,6"/>
      <Setter Property="Cursor"          Value="Hand"/>
      <Setter Property="Transitions">
        <Transitions>
          <BrushTransition Property="BorderBrush" Duration="0:0:0.10"/>
        </Transitions>
      </Setter>
    </Style>
    <Style Selector="Border.wt-row:pointerover">
      <Setter Property="BorderBrush" Value="{DynamicResource LineBrightBrush}"/>
    </Style>
    <Style Selector="Border.wt-row.selected">
      <Setter Property="BorderBrush" Value="{DynamicResource LineBrightBrush}"/>
    </Style>
  </Window.Styles>

  <Grid RowDefinitions="36,Auto,*,52">

      <!-- Title bar -->
      <Border Grid.Row="0"
              Background="{DynamicResource DeepBrush}"
              BorderBrush="{DynamicResource LineBrush}"
              BorderThickness="0,0,0,1"
              PointerPressed="OnTitleBarPressed">
        <Grid ColumnDefinitions="*,Auto">
          <TextBlock Grid.Column="0"
                     Text="{Binding Title}"
                     VerticalAlignment="Center"
                     Margin="14,0,0,0"
                     FontSize="12"
                     FontWeight="SemiBold"
                     Foreground="{DynamicResource TextBrush}"/>
          <Button Grid.Column="1"
                  Content="✕"
                  Command="{Binding CloseCommand}"
                  Margin="0,0,8,0"
                  Width="28" Height="28"
                  FontSize="11"
                  HorizontalContentAlignment="Center"
                  VerticalContentAlignment="Center"/>
        </Grid>
      </Border>

      <!-- Toolbar -->
      <Border Grid.Row="1"
              Background="{DynamicResource DeepBrush}"
              BorderBrush="{DynamicResource LineBrush}"
              BorderThickness="0,0,0,1"
              Padding="12,8">
        <StackPanel Orientation="Horizontal" Spacing="8">
          <Button Content="Refresh" Command="{Binding RefreshCommand}" IsEnabled="{Binding !IsBusy}"/>
          <Button Content="Cleanup finished" Command="{Binding CleanupFinishedCommand}" IsEnabled="{Binding !IsBusy}"/>
          <TextBlock Text="{Binding StatusMessage}" VerticalAlignment="Center" Margin="12,0,0,0"
                     Foreground="{DynamicResource TextDimBrush}"/>
        </StackPanel>
      </Border>

      <!-- Content -->
      <ScrollViewer Grid.Row="2" Padding="12,8">
        <StackPanel>
          <!-- Column headers -->
          <Grid ColumnDefinitions="*,90,80,80" Margin="12,0,12,4">
            <TextBlock Grid.Column="0" Text="TASK"
                       FontFamily="{DynamicResource MonoFont}" FontSize="10" LetterSpacing="1.4"
                       Foreground="{DynamicResource TextFaintBrush}"/>
            <TextBlock Grid.Column="1" Text="STATE"
                       FontFamily="{DynamicResource MonoFont}" FontSize="10" LetterSpacing="1.4"
                       Foreground="{DynamicResource TextFaintBrush}"/>
            <TextBlock Grid.Column="2" Text="DIFF"
                       FontFamily="{DynamicResource MonoFont}" FontSize="10" LetterSpacing="1.4"
                       Foreground="{DynamicResource TextFaintBrush}"/>
            <TextBlock Grid.Column="3" Text="AGE"
                       FontFamily="{DynamicResource MonoFont}" FontSize="10" LetterSpacing="1.4"
                       Foreground="{DynamicResource TextFaintBrush}"/>
          </Grid>
          <Border Height="1" Background="{DynamicResource LineBrush}" Margin="0,0,0,8"/>

          <!-- Rows (per-list) -->
          <ItemsControl ItemsSource="{Binding Rows}" IsVisible="{Binding !IsGlobal}">
            <ItemsControl.ItemTemplate>
              <DataTemplate DataType="vm:WorktreeOverviewRowViewModel">
                <ContentControl ContentTemplate="{StaticResource WorktreeRowTemplate}" Content="{Binding}"/>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>

          <!-- Rows (global, grouped) -->
          <ItemsControl ItemsSource="{Binding Groups}" IsVisible="{Binding IsGlobal}">
            <ItemsControl.ItemTemplate>
              <DataTemplate DataType="vm:WorktreesGroupViewModel">
                <Expander Header="{Binding ListName}" IsExpanded="True" Margin="0,0,0,6">
                  <ItemsControl ItemsSource="{Binding Rows}">
                    <ItemsControl.ItemTemplate>
                      <DataTemplate DataType="vm:WorktreeOverviewRowViewModel">
                        <ContentControl ContentTemplate="{StaticResource WorktreeRowTemplate}" Content="{Binding}"/>
                      </DataTemplate>
                    </ItemsControl.ItemTemplate>
                  </ItemsControl>
                </Expander>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>
        </StackPanel>
      </ScrollViewer>

      <!-- Footer -->
      <Border Grid.Row="3"
              Background="{DynamicResource DeepBrush}"
              BorderBrush="{DynamicResource LineBrush}"
              BorderThickness="0,1,0,0"
              Padding="12,10">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
          <Button Content="Close" Command="{Binding CloseCommand}"/>
        </StackPanel>
      </Border>

    </Grid>
</Window>
