<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="using:ClaudeDo.Ui.ViewModels.Islands"
             xmlns:islands="using:ClaudeDo.Ui.Views.Islands"
             x:Class="ClaudeDo.Ui.Views.Islands.DetailsIslandView"
             x:DataType="vm:DetailsIslandViewModel">
  <DockPanel>

    <!-- ── Metadata footer (sticky bottom) ── -->
    <Border DockPanel.Dock="Bottom"
            BorderBrush="{DynamicResource LineBrush}"
            BorderThickness="0,1,0,0"
            Padding="14,8">
      <Grid ColumnDefinitions="Auto,*,Auto">
        <Button Grid.Column="0" Classes="icon-btn"
                Command="{Binding DeleteTaskCommand}"
                ToolTip.Tip="Delete task"
                VerticalAlignment="Center">
          <PathIcon Data="{StaticResource Icon.Trash}" Width="14" Height="14"
                    Foreground="{DynamicResource BloodBrush}"/>
        </Button>
        <TextBlock Grid.Column="1"
                   Text="{Binding Task.CreatedAtFormatted}"
                   FontFamily="{DynamicResource MonoFont}" FontSize="10"
                   Foreground="{DynamicResource TextFaintBrush}"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"/>
        <Button Grid.Column="2" Classes="icon-btn"
                Command="{Binding CloseDetailsCommand}"
                ToolTip.Tip="Close"
                VerticalAlignment="Center">
          <PathIcon Data="{StaticResource Icon.X}" Width="14" Height="14"/>
        </Button>
      </Grid>
    </Border>

    <!-- ── Header (sticky top): eyebrow · title · gear (agent-settings flyout) ── -->
    <Border DockPanel.Dock="Top" Classes="island-header">
      <Grid ColumnDefinitions="*,Auto">
        <StackPanel Grid.Column="0" Spacing="0">
          <StackPanel Orientation="Horizontal" Spacing="6" Margin="0,0,0,4">
            <Ellipse Width="5" Height="5" Fill="{DynamicResource AccentBrush}"
                     VerticalAlignment="Center"/>
            <TextBlock Classes="eyebrow" Text="LOGBOOK" VerticalAlignment="Center"/>
            <TextBlock Text="{Binding TaskIdBadge}"
                       FontFamily="{DynamicResource MonoFont}" FontSize="10"
                       Foreground="{DynamicResource TextFaintBrush}"
                       VerticalAlignment="Center"
                       Margin="8,0,0,0"/>
          </StackPanel>
          <TextBox Text="{Binding EditableTitle, Mode=TwoWay}"
                   FontSize="14" FontWeight="Medium"
                   BorderThickness="0" Background="Transparent"
                   Foreground="{DynamicResource TextBrush}"
                   Padding="0"/>
        </StackPanel>

        <Button Grid.Column="1" Classes="icon-btn"
                ToolTip.Tip="Agent settings"
                IsEnabled="{Binding IsAgentSectionEnabled}"
                VerticalAlignment="Top"
                Margin="6,0,0,0">
          <TextBlock Text="⚙" FontSize="14"/>
          <Button.Flyout>
            <Flyout Placement="BottomEdgeAlignedRight" ShowMode="Standard">
              <StackPanel Width="340" Spacing="10" Margin="4">
                <TextBlock Text="Agent settings (overrides)" FontWeight="SemiBold"/>

                <StackPanel Spacing="2">
                  <TextBlock Text="Model"/>
                  <ComboBox ItemsSource="{Binding TaskModelOptions}"
                            SelectedItem="{Binding TaskModelSelection, Mode=TwoWay}"
                            HorizontalAlignment="Stretch"/>
                  <TextBlock Text="{Binding EffectiveModelHint, StringFormat='Effective if inherited: {0}'}"
                             Opacity="0.6" FontSize="11"/>
                </StackPanel>

                <StackPanel Spacing="2">
                  <TextBlock Text="System prompt (appended)"/>
                  <TextBox Text="{Binding TaskSystemPrompt, Mode=TwoWay}"
                           AcceptsReturn="True" TextWrapping="Wrap" MinHeight="70"
                           PlaceholderText="{Binding EffectiveSystemPromptHint}"/>
                </StackPanel>

                <StackPanel Spacing="2">
                  <TextBlock Text="Agent file"/>
                  <ComboBox ItemsSource="{Binding TaskAgentOptions}"
                            SelectedItem="{Binding TaskSelectedAgent, Mode=TwoWay}"
                            HorizontalAlignment="Stretch">
                    <ComboBox.ItemTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding Name}"/>
                      </DataTemplate>
                    </ComboBox.ItemTemplate>
                  </ComboBox>
                  <TextBlock Text="{Binding EffectiveAgentHint, StringFormat='Effective if inherited: {0}'}"
                             Opacity="0.6" FontSize="11"/>
                </StackPanel>
              </StackPanel>
            </Flyout>
          </Button.Flyout>
        </Button>
      </Grid>
    </Border>

    <!-- ── Task strip row (sticky top): check + title + star ── -->
    <Border DockPanel.Dock="Top"
            Padding="18,10,18,10"
            BorderBrush="{DynamicResource LineBrush}"
            BorderThickness="0,0,0,1">
      <Grid ColumnDefinitions="Auto,*,Auto">
        <Ellipse Grid.Column="0"
                 Classes="task-check"
                 Classes.done="{Binding Task.Done}"
                 Width="18" Height="18"
                 VerticalAlignment="Center"
                 Cursor="Hand"/>
        <TextBlock Grid.Column="1"
                   Text="{Binding EditableTitle}"
                   FontSize="14" FontWeight="Medium"
                   Foreground="{DynamicResource TextBrush}"
                   TextTrimming="CharacterEllipsis"
                   VerticalAlignment="Center"
                   Margin="10,0"/>
        <Button Grid.Column="2"
                Classes="icon-btn star-btn"
                Classes.on="{Binding Task.IsStarred}"
                VerticalAlignment="Center">
          <PathIcon Data="{StaticResource Icon.Star}" Width="14" Height="14"/>
        </Button>
      </Grid>
    </Border>

    <!-- ── Agent status strip (sticky, above metadata footer) ── -->
    <islands:AgentStripView DockPanel.Dock="Bottom"/>

    <!-- ── Scrollable body: steps + terminal ── -->
    <ScrollViewer VerticalScrollBarVisibility="Auto">
      <StackPanel Spacing="0">

        <!-- Steps section -->
        <Border Padding="18,12,18,12"
                BorderBrush="{DynamicResource LineBrush}"
                BorderThickness="0,0,0,1">
          <StackPanel Spacing="6">
            <TextBlock Classes="section-label" Text="STEPS" Margin="0,0,0,2"/>
            <TextBox Text="{Binding NewSubtaskTitle, Mode=TwoWay}"
                     PlaceholderText="Add a step..."
                     Padding="8"
                     Background="{DynamicResource Surface2Brush}"
                     BorderBrush="{DynamicResource LineBrush}"
                     BorderThickness="1"
                     CornerRadius="6">
              <TextBox.KeyBindings>
                <KeyBinding Gesture="Enter" Command="{Binding AddSubtaskCommand}"/>
              </TextBox.KeyBindings>
            </TextBox>
            <ItemsControl ItemsSource="{Binding Subtasks}"
                          IsVisible="{Binding Subtasks.Count}">
              <ItemsControl.ItemTemplate>
                <DataTemplate DataType="vm:SubtaskRowViewModel">
                  <Border Classes="subtask-row"
                          Classes.done="{Binding Done}">
                    <Grid ColumnDefinitions="Auto,*">
                      <Ellipse Grid.Column="0"
                               Classes="task-check"
                               Classes.done="{Binding Done}"
                               Width="16" Height="16"
                               VerticalAlignment="Center"
                               Cursor="Hand"
                               Margin="0,0,8,0"/>
                      <TextBlock Grid.Column="1"
                                 Classes="subtask-title"
                                 Text="{Binding Title}"
                                 FontSize="13"
                                 Foreground="{DynamicResource TextDimBrush}"
                                 VerticalAlignment="Center"
                                 TextWrapping="Wrap"/>
                    </Grid>
                  </Border>
                </DataTemplate>
              </ItemsControl.ItemTemplate>
            </ItemsControl>
          </StackPanel>
        </Border>

        <!-- Session terminal — auto-sizes to output, scrolls internally after MaxHeight -->
        <islands:SessionTerminalView MaxHeight="420"/>

      </StackPanel>
    </ScrollViewer>

  </DockPanel>
</UserControl>
