<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="using:ClaudeDo.Ui.ViewModels.Islands"
             xmlns:ctl="using:ClaudeDo.Ui.Views.Controls"
             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}"/>

    <!-- Indent wrapper: col 0 = 24px child indent track, col 1 = content -->
    <Grid Grid.Row="1" ColumnDefinitions="Auto,*">

      <!-- Indent track (only visible for child tasks) -->
      <Border Grid.Column="0" Width="24" IsVisible="{Binding IsChild}" VerticalAlignment="Stretch">
        <Rectangle Width="1" Fill="{DynamicResource LineBrush}"
                   HorizontalAlignment="Right" Margin="0,4"/>
      </Border>

      <!-- Main task card -->
      <Border Grid.Column="1" Classes="task-row"
              Margin="0"
              Classes.selected="{Binding IsSelected}"
              Classes.done="{Binding Done}">
        <Border.ContextMenu>
          <ContextMenu>
            <MenuItem Header="Send to queue"
                      IsVisible="{Binding CanSendToQueue}"
                      Click="OnSendToQueueClick"/>
            <MenuItem Header="Remove from queue"
                      IsVisible="{Binding CanRemoveFromQueue}"
                      Click="OnRemoveFromQueueClick"/>
            <MenuItem Header="Cancel execution"
                      IsVisible="{Binding IsRunning}"
                      Click="OnCancelExecutionClick"/>
            <Separator/>
            <MenuItem Header="Mark as">
              <MenuItem Header="Done"      Tag="Done"      Click="OnSetStatusClick"/>
              <MenuItem Header="Cancelled" Tag="Cancelled" Click="OnSetStatusClick"/>
            </MenuItem>
            <Separator/>
            <MenuItem Header="Run interactively"
                      Click="OnRunInteractivelyClick"/>
            <MenuItem Header="Open planning Session"
                      Click="OnOpenPlanningSessionClick"
                      IsVisible="{Binding CanOpenPlanningSession}"/>
            <MenuItem Header="Resume planning Session"
                      Click="OnResumePlanningSessionClick"
                      IsVisible="{Binding CanResumeOrDiscardPlanning}"/>
            <MenuItem Header="Discard planning session"
                      Click="OnDiscardPlanningSessionClick"
                      IsVisible="{Binding CanResumeOrDiscardPlanning}"/>
            <MenuItem Header="Queue subtasks sequentially"
                      Click="OnQueuePlanningSubtasksClick"
                      IsVisible="{Binding CanQueuePlan}"/>
            <Separator/>
            <MenuItem Header="Schedule for..." Click="OnScheduleForClick"/>
            <MenuItem Header="Clear schedule"
                      IsVisible="{Binding HasSchedule}"
                      Click="OnClearScheduleClick"/>
          </ContextMenu>
        </Border.ContextMenu>
        <Grid ColumnDefinitions="0,18,32,*,Auto,32" Margin="6,8,10,8">

        <!-- Chevron toggle (only for planning parent tasks) -->
        <Button Grid.Column="1"
                IsVisible="{Binding IsPlanningParent}"
                Command="{Binding $parent[ItemsControl].((vm:TasksIslandViewModel)DataContext).ToggleExpandCommand}"
                CommandParameter="{Binding}"
                Classes="icon-btn"
                Width="18" Height="18"
                VerticalAlignment="Center">
          <Panel>
            <TextBlock Text="▾" FontSize="10" IsVisible="{Binding IsExpanded}"
                       VerticalAlignment="Center" HorizontalAlignment="Center"/>
            <TextBlock Text="▸" FontSize="10" IsVisible="{Binding !IsExpanded}"
                       VerticalAlignment="Center" HorizontalAlignment="Center"/>
          </Panel>
        </Button>

        <!-- Done toggle -->
        <Button Grid.Column="2" 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="3" Spacing="6" VerticalAlignment="Center">
          <Grid ColumnDefinitions="*,Auto" VerticalAlignment="Center">
            <TextBlock Grid.Column="0"
                       Classes="task-title"
                       Text="{Binding Title}" FontSize="14"
                       Foreground="{DynamicResource TextBrush}"
                       TextWrapping="Wrap"
                       FontStyle="{Binding IsDraft, Converter={StaticResource BoolToItalic}}"
                       Opacity="{Binding IsDraft, Converter={StaticResource BoolToDraftOpacity}}"
                       TextDecorations="{Binding Done, Converter={StaticResource StrikeIfTrue}}"/>

            <!-- Badges: DRAFT and planning session -->
            <StackPanel Grid.Column="1" Orientation="Horizontal" Spacing="4"
                        VerticalAlignment="Center" Margin="4,0,0,0">
              <Border Classes="badge draft" IsVisible="{Binding IsDraft}">
                <TextBlock Text="DRAFT"/>
              </Border>
              <Border Classes="badge planning" IsVisible="{Binding IsPlanned}">
                <TextBlock Text="PLANNED"/>
              </Border>
              <Border Classes="badge planning" IsVisible="{Binding IsPlanningParent}">
                <TextBlock Text="{Binding PlanningBadge}"/>
              </Border>
            </StackPanel>
          </Grid>

          <!-- 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 (visible when row is Queued, or planning parent has queued subtasks) -->
            <Button Classes="icon-btn dequeue-btn"
                    IsVisible="{Binding CanRemoveFromQueue}"
                    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" IsVisible="{Binding ShowListChip}">
              <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>

          </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="5" 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>

    </Grid>

    <!-- 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="WHEN" FontSize="10" Opacity="0.6"
                           Foreground="{DynamicResource TextDimBrush}"/>
                <ctl:ThemedDatePicker x:Name="ScheduleDate" ShowTime="True"
                                      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>
