<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:ClaudeDo.Ui.ViewModels.Modals"
        xmlns:ctl="using:ClaudeDo.Ui.Views.Controls"
        xmlns:loc="using:ClaudeDo.Ui.Localization"
        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>
    <DataTemplate x:Key="WorktreeRowTemplate" x:DataType="vm:WorktreeOverviewRowViewModel">
      <Border Classes="task-row"
              Classes.selected="{Binding IsSelected}"
              Tapped="OnRowTapped">
        <Border.ContextMenu>
          <ContextMenu>
            <MenuItem Header="{loc:Tr modals.worktreesOverview.ctxShowDiff}"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).ShowDiffCommand}"
                      CommandParameter="{Binding}"/>
            <MenuItem Header="{loc:Tr modals.worktreesOverview.ctxOpenInExplorer}"
                      IsEnabled="{Binding PathExistsOnDisk}"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).OpenInExplorerCommand}"
                      CommandParameter="{Binding}"/>
            <MenuItem Header="{loc:Tr modals.worktreesOverview.ctxJumpToTask}"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).JumpToTaskCommand}"
                      CommandParameter="{Binding}"/>
            <Separator/>
            <MenuItem Header="{loc:Tr modals.worktreesOverview.ctxMerge}"
                      IsEnabled="{Binding IsActive}"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).MergeCommand}"
                      CommandParameter="{Binding}"/>
            <MenuItem Header="{loc:Tr modals.worktreesOverview.ctxDiscard}"
                      IsEnabled="{Binding IsActive}"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).DiscardCommand}"
                      CommandParameter="{Binding}"/>
            <MenuItem Header="{loc:Tr modals.worktreesOverview.ctxKeep}"
                      IsEnabled="{Binding IsActive}"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).KeepCommand}"
                      CommandParameter="{Binding}"/>
            <Separator/>
            <MenuItem Header="{loc:Tr modals.worktreesOverview.ctxCopyBranch}"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).CopyBranchCommand}"
                      CommandParameter="{Binding}"/>
            <MenuItem Header="{loc:Tr modals.worktreesOverview.ctxCopyPath}"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).CopyPathCommand}"
                      CommandParameter="{Binding}"/>
            <Separator/>
            <MenuItem Header="{loc:Tr modals.worktreesOverview.ctxForceRemove}"
                      Foreground="{DynamicResource StatusErrorBrush}"
                      Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).ForceRemoveCommand}"
                      CommandParameter="{Binding}"/>
          </ContextMenu>
        </Border.ContextMenu>
        <Grid ColumnDefinitions="Auto,*,90,90,80,80">
          <CheckBox Grid.Column="0" VerticalAlignment="Center" Margin="0,0,8,0"
                    IsChecked="{Binding IsChecked, Mode=TwoWay}"
                    IsEnabled="{Binding IsActive}"
                    IsVisible="{Binding IsActive}"/>
          <StackPanel Grid.Column="1" Orientation="Vertical" Spacing="2">
            <TextBlock Classes="title" Text="{Binding TaskTitle}"/>
            <StackPanel Orientation="Horizontal" Spacing="4">
              <TextBlock Classes="meta" Text="{Binding TaskStatus}"/>
              <TextBlock Classes="meta" Text="•"
                         IsVisible="{Binding !PathExistsOnDisk}"/>
              <TextBlock Classes="meta" Text="{loc:Tr modals.worktreesOverview.phantom}" Foreground="{DynamicResource StatusErrorBrush}"
                         IsVisible="{Binding !PathExistsOnDisk}"
                         ToolTip.Tip="{loc:Tr modals.worktreesOverview.phantomTooltip}"/>
            </StackPanel>
          </StackPanel>
          <TextBlock Grid.Column="2" Classes="meta" VerticalAlignment="Center"
                     Text="{Binding MergeOutcome}"
                     IsVisible="{Binding HasOutcome}"/>
          <Border Grid.Column="3" Classes="chip"
                  Classes.wt-active="{Binding IsActive}"
                  Classes.wt-merged="{Binding IsMerged}"
                  Classes.wt-discarded="{Binding IsDiscarded}"
                  Classes.wt-kept="{Binding IsKept}"
                  HorizontalAlignment="Left" VerticalAlignment="Center">
            <TextBlock Text="{Binding State}"/>
          </Border>
          <TextBlock Grid.Column="4" Classes="meta" Text="{Binding DiffStat}" VerticalAlignment="Center"/>
          <TextBlock Grid.Column="5" Classes="meta" Text="{Binding AgeText}" VerticalAlignment="Center"/>
        </Grid>
      </Border>
    </DataTemplate>
  </Window.Resources>

  <ctl:ModalShell Title="{Binding Title}" CloseCommand="{Binding CloseCommand}">

    <!-- Body: toolbar + content -->
    <DockPanel>

      <!-- Toolbar -->
      <Border DockPanel.Dock="Top"
              Background="{DynamicResource DeepBrush}"
              BorderBrush="{DynamicResource LineBrush}"
              BorderThickness="0,0,0,1"
              Padding="12,8">
        <StackPanel Orientation="Horizontal" Spacing="8">
          <Button Classes="btn" Content="{loc:Tr modals.worktreesOverview.refresh}" Command="{Binding RefreshCommand}" IsEnabled="{Binding !IsBusy}"/>
          <Button Classes="btn" Content="{loc:Tr modals.worktreesOverview.cleanupFinished}" Command="{Binding CleanupFinishedCommand}" IsEnabled="{Binding !IsBusy}"/>
          <!-- Batch merge is per-list only: a single target branch is meaningless across repos. -->
          <StackPanel Orientation="Horizontal" Spacing="8" IsVisible="{Binding !IsGlobal}">
            <Button Classes="btn" Content="{loc:Tr modals.worktreesOverview.selectAll}" Command="{Binding ToggleSelectAllCommand}"/>
            <Border Width="1" Background="{DynamicResource LineBrush}" Margin="4,2"/>
            <TextBlock Text="{loc:Tr modals.worktreesOverview.targetLabel}" VerticalAlignment="Center" Foreground="{DynamicResource TextDimBrush}"/>
            <ComboBox MinWidth="160"
                      ItemsSource="{Binding MergeTargets}"
                      SelectedItem="{Binding SelectedTarget, Mode=TwoWay}"/>
            <Button Classes="btn accent"
                    Content="{loc:Tr modals.worktreesOverview.mergeAll}"
                    Command="{Binding MergeAllCommand}"/>
            <TextBlock Text="{Binding SelectedCount, StringFormat='{}{0} selected'}"
                       VerticalAlignment="Center" Foreground="{DynamicResource TextDimBrush}"/>
            <TextBlock Text="{Binding BatchProgress}" VerticalAlignment="Center" Margin="8,0,0,0"
                       Foreground="{DynamicResource TextDimBrush}"/>
          </StackPanel>
          <TextBlock Text="{Binding StatusMessage}" VerticalAlignment="Center" Margin="8,0,0,0"
                     Foreground="{DynamicResource TextDimBrush}"/>
        </StackPanel>
      </Border>

      <!-- Content -->
      <ScrollViewer Padding="20,16">
        <StackPanel>
          <Border IsVisible="{Binding ConflictRows.Count}"
                  Background="{DynamicResource ErrorTintBrush}"
                  BorderBrush="{DynamicResource StatusErrorBrush}"
                  BorderThickness="1" CornerRadius="6" Padding="12,8" Margin="0,0,0,12">
            <StackPanel Spacing="6">
              <TextBlock Classes="eyebrow" Text="{loc:Tr modals.worktreesOverview.needsResolution}"/>
              <ItemsControl ItemsSource="{Binding ConflictRows}">
                <ItemsControl.ItemTemplate>
                  <DataTemplate x:DataType="vm:WorktreeOverviewRowViewModel">
                    <Grid ColumnDefinitions="*,Auto" Margin="0,2">
                      <TextBlock Grid.Column="0" Classes="meta" VerticalAlignment="Center"
                                 Text="{Binding TaskTitle}"/>
                      <Button Grid.Column="1" Classes="btn"
                              Content="{loc:Tr modals.worktreesOverview.resolve}"
                              Command="{Binding $parent[Window].((vm:WorktreesOverviewModalViewModel)DataContext).ResolveConflictCommand}"
                              CommandParameter="{Binding}"/>
                    </Grid>
                  </DataTemplate>
                </ItemsControl.ItemTemplate>
              </ItemsControl>
            </StackPanel>
          </Border>
          <!-- Column headers -->
          <Grid ColumnDefinitions="Auto,*,90,90,80,80" Margin="12,0,12,4">
            <TextBlock Grid.Column="1" Classes="eyebrow" Text="{loc:Tr modals.worktreesOverview.columnTask}"/>
            <TextBlock Grid.Column="2" Classes="eyebrow" Text="{loc:Tr modals.worktreesOverview.columnOutcome}"/>
            <TextBlock Grid.Column="3" Classes="eyebrow" Text="{loc:Tr modals.worktreesOverview.columnState}"/>
            <TextBlock Grid.Column="4" Classes="eyebrow" Text="{loc:Tr modals.worktreesOverview.columnDiff}"/>
            <TextBlock Grid.Column="5" Classes="eyebrow" Text="{loc:Tr modals.worktreesOverview.columnAge}"/>
          </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"
                          HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
                  <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>

    </DockPanel>

  </ctl:ModalShell>
</Window>
