<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="using:ClaudeDo.Ui.ViewModels.Islands"
             x:Class="ClaudeDo.Ui.Views.Islands.TaskRowView"
             x:DataType="vm:TaskRowViewModel">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="6"/>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <!-- Above-row indicator: lives in the 6px gap between cards -->
    <Border Grid.Row="0" Height="2" VerticalAlignment="Center" Margin="4,0"
            Background="{DynamicResource MossBrush}" CornerRadius="1"
            IsVisible="{Binding DropHintAbove}"/>

    <Border Grid.Row="1" Classes="task-row"
            Margin="0"
            Classes.selected="{Binding IsSelected}"
            Classes.done="{Binding Done}">
      <Border.ContextMenu>
        <ContextMenu>
          <MenuItem Header="Send to queue"
                    IsVisible="{Binding !IsQueued}"
                    Click="OnSendToQueueClick"/>
          <MenuItem Header="Remove from queue"
                    IsVisible="{Binding IsQueued}"
                    Click="OnRemoveFromQueueClick"/>
          <Separator/>
          <MenuItem Header="Schedule for..." Click="OnScheduleForClick"/>
          <MenuItem Header="Clear schedule"
                    IsVisible="{Binding HasSchedule}"
                    Click="OnClearScheduleClick"/>
        </ContextMenu>
      </Border.ContextMenu>
      <Grid ColumnDefinitions="0,32,*,32" Margin="6,8,10,8">

      <!-- Done toggle -->
      <Button Grid.Column="1" Classes="flat" VerticalAlignment="Top"
              Margin="0,2,0,0"
              Command="{Binding $parent[ItemsControl].((vm:TasksIslandViewModel)DataContext).ToggleDoneCommand}"
              CommandParameter="{Binding}">
        <Ellipse Width="18" Height="18" Classes="task-check"
                 Classes.done="{Binding Done}"/>
      </Button>

      <!-- Title + chip row + live tail -->
      <StackPanel Grid.Column="2" Spacing="6" VerticalAlignment="Center">
        <TextBlock Classes="task-title"
                   Text="{Binding Title}" FontSize="14"
                   Foreground="{DynamicResource TextBrush}"
                   TextDecorations="{Binding Done, Converter={StaticResource StrikeIfTrue}}"/>

        <!-- Chip row -->
        <StackPanel Orientation="Horizontal" Spacing="6">

          <!-- Status chip -->
          <Border Classes="chip"
                  Classes.running="{Binding Status, Converter={StaticResource EqStatus}, ConverterParameter=Running}"
                  Classes.review="{Binding Status, Converter={StaticResource EqStatus}, ConverterParameter=Done}"
                  Classes.error="{Binding Status, Converter={StaticResource EqStatus}, ConverterParameter=Failed}"
                  Classes.queued="{Binding Status, Converter={StaticResource EqStatus}, ConverterParameter=Queued}">
            <TextBlock Text="{Binding Status}"/>
          </Border>

          <!-- Dequeue button (only when Queued) -->
          <Button Classes="icon-btn dequeue-btn"
                  IsVisible="{Binding IsQueued}"
                  ToolTip.Tip="Remove from queue"
                  Command="{Binding $parent[ItemsControl].((vm:TasksIslandViewModel)DataContext).RemoveFromQueueCommand}"
                  CommandParameter="{Binding}">
            <PathIcon Width="10" Height="10" Data="{StaticResource Icon.X}"/>
          </Button>

          <!-- List chip with dot -->
          <Border Classes="chip chip-list">
            <StackPanel Orientation="Horizontal" Spacing="5" VerticalAlignment="Center">
              <Ellipse Width="6" Height="6"
                       Fill="{DynamicResource MossBrush}"
                       VerticalAlignment="Center"/>
              <TextBlock Text="{Binding ListName}"/>
            </StackPanel>
          </Border>

          <!-- Branch chip -->
          <Border Classes="chip chip-branch" IsVisible="{Binding HasBranch}">
            <StackPanel Orientation="Horizontal" Spacing="4" VerticalAlignment="Center">
              <PathIcon Width="10" Height="10"
                        Data="{StaticResource Icon.GitBranch}"
                        Foreground="{DynamicResource TextDimBrush}"/>
              <TextBlock Text="{Binding Branch}"/>
            </StackPanel>
          </Border>

          <!-- Diff chip -->
          <Border Classes="chip chip-diff" IsVisible="{Binding HasDiff}">
            <StackPanel Orientation="Horizontal" Spacing="4" VerticalAlignment="Center">
              <TextBlock Classes="diff-add" Text="{Binding DiffAdditionsText}"/>
              <TextBlock Classes="diff-del" Text="{Binding DiffDeletionsText}"/>
            </StackPanel>
          </Border>

          <!-- Tag chips -->
          <ItemsControl ItemsSource="{Binding Tags}" IsVisible="{Binding HasTags}">
            <ItemsControl.ItemsPanel>
              <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" Spacing="6"/>
              </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
              <DataTemplate>
                <Border Classes="chip chip-tag">
                  <TextBlock Text="{Binding}"/>
                </Border>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>
        </StackPanel>

        <!-- Live-tail row (visible when running + has tail) -->
        <Border Classes="task-live-tail" IsVisible="{Binding HasLiveTail}">
          <StackPanel Spacing="3">
            <TextBlock Text="{Binding LiveTail}"
                       TextTrimming="CharacterEllipsis" MaxLines="1"/>
            <Grid Height="3" HorizontalAlignment="Stretch">
              <Rectangle Fill="{DynamicResource Surface3Brush}"
                         HorizontalAlignment="Stretch" RadiusX="1.5" RadiusY="1.5"/>
              <Rectangle Fill="{DynamicResource MossBrush}"
                         HorizontalAlignment="Left" Width="60" RadiusX="1.5" RadiusY="1.5"/>
            </Grid>
          </StackPanel>
        </Border>
      </StackPanel>

      <!-- Star toggle -->
      <Button Grid.Column="3" Classes="icon-btn star-btn"
              Classes.on="{Binding IsStarred}"
              VerticalAlignment="Top" Margin="0,2,0,0"
              Command="{Binding $parent[ItemsControl].((vm:TasksIslandViewModel)DataContext).ToggleStarCommand}"
              CommandParameter="{Binding}">
        <PathIcon Width="14" Height="14" Data="{StaticResource Icon.Star}"/>
      </Button>
      </Grid>
    </Border>

    <!-- Below-row indicator: only expands when visible (used for the last row of a section) -->
    <Grid Grid.Row="2" Height="6" IsVisible="{Binding DropHintBelow}">
      <Border Height="2" VerticalAlignment="Center" Margin="4,0"
              Background="{DynamicResource MossBrush}" CornerRadius="1"/>
    </Grid>

    <!-- Hidden schedule anchor (its Flyout is shown from the context menu) -->
    <Button Grid.Row="1" x:Name="ScheduleAnchor"
            Width="1" Height="1" Opacity="0"
            HorizontalAlignment="Left" VerticalAlignment="Top"
            IsHitTestVisible="False" Focusable="False">
      <Button.Flyout>
        <Flyout Placement="Bottom" ShowMode="Standard">
          <Border Background="{DynamicResource Surface2Brush}"
                  BorderBrush="{DynamicResource BorderBrush}"
                  BorderThickness="1" CornerRadius="10"
                  Padding="16" Width="300">
            <StackPanel Spacing="12">
              <TextBlock Text="Schedule task"
                         FontWeight="SemiBold" FontSize="13"
                         Foreground="{DynamicResource TextBrush}"/>

              <StackPanel Spacing="6">
                <TextBlock Text="DATE" FontSize="10" Opacity="0.6"
                           Foreground="{DynamicResource TextDimBrush}"/>
                <DatePicker x:Name="ScheduleDate" HorizontalAlignment="Stretch"/>
              </StackPanel>

              <StackPanel Spacing="6">
                <TextBlock Text="TIME" FontSize="10" Opacity="0.6"
                           Foreground="{DynamicResource TextDimBrush}"/>
                <TimePicker x:Name="ScheduleTime" ClockIdentifier="24HourClock"
                            HorizontalAlignment="Stretch"/>
              </StackPanel>

              <StackPanel Orientation="Horizontal" Spacing="8"
                          HorizontalAlignment="Right" Margin="0,4,0,0">
                <Button Content="Cancel" Click="OnScheduleCancelClick" MinWidth="76"/>
                <Button Content="Schedule" Classes="accent" Click="OnScheduleSetClick" MinWidth="76"/>
              </StackPanel>
            </StackPanel>
          </Border>
        </Flyout>
      </Button.Flyout>
    </Button>
  </Grid>
</UserControl>
