<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:vm="using:ClawdDotNet.Desktop.ViewModels"
             xmlns:ctrl="using:ClawdDotNet.Desktop.Controls"
             xmlns:conv="using:ClawdDotNet.Desktop.Converters"
             mc:Ignorable="d" d:DesignWidth="1100" d:DesignHeight="700"
             x:Class="ClawdDotNet.Desktop.Views.ChatPageView"
             x:DataType="vm:ChatPageViewModel">

  <DockPanel MaxWidth="960" HorizontalAlignment="Left">

    <!-- Agentenauswahl und Verlaufsbefehle -->
    <StackPanel DockPanel.Dock="Top" Classes="toolbar" Margin="0,0,0,16">
      <TextBlock Text="Agent:" FontWeight="SemiBold" VerticalAlignment="Center" />
      <ComboBox ItemsSource="{Binding AvailableAgents}" SelectedItem="{Binding SelectedAgent}"
                Width="220">
        <ComboBox.ItemTemplate>
          <DataTemplate>
            <TextBlock Text="{Binding DisplayName}" />
          </DataTemplate>
        </ComboBox.ItemTemplate>
      </ComboBox>

      <Border Classes="sep" />

      <Button Classes="toolbar" Command="{Binding ClearHistoryCommand}">
        <StackPanel Orientation="Horizontal" Spacing="6">
          <ctrl:StrokeIcon Data="{StaticResource IconTrash}" Width="14" Height="14" />
          <TextBlock Text="Verlauf leeren" />
        </StackPanel>
      </Button>

      <Button Classes="toolbar" Command="{Binding AbortCommand}" IsEnabled="{Binding IsSending}">
        <StackPanel Orientation="Horizontal" Spacing="6">
          <ctrl:StrokeIcon Data="{StaticResource IconStop}" Width="12" Height="12" IsFilled="True" />
          <TextBlock Text="Abbrechen" />
        </StackPanel>
      </Button>
    </StackPanel>

    <!-- Statuszeile des Bereichs -->
    <TextBlock DockPanel.Dock="Bottom" Classes="caption" Margin="0,8,0,0"
               Text="{Binding StatusText}" />

    <!-- Eingabe -->
    <Grid DockPanel.Dock="Bottom" ColumnDefinitions="*,Auto" Margin="0,12,0,0">
      <TextBox Grid.Column="0" Text="{Binding InputPrompt}"
               PlaceholderText="Nachricht eingeben… (Strg+Eingabe zum Senden)"
               AcceptsReturn="True" Height="60" TextWrapping="Wrap"
               KeyDown="OnInputKeyDown" />

      <Button Grid.Column="1" Classes="primary" Content="Senden" Width="100"
              Command="{Binding SendMessageCommand}" IsEnabled="{Binding !IsSending}"
              VerticalAlignment="Stretch" Margin="8,0,0,0" />
    </Grid>

    <!-- Verlauf -->
    <ScrollViewer x:Name="ChatScroller" VerticalScrollBarVisibility="Auto">
      <ItemsControl ItemsSource="{Binding ChatEntries}">
        <ItemsControl.ItemsPanel>
          <ItemsPanelTemplate>
            <StackPanel Spacing="12" />
          </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>
          <DataTemplate DataType="vm:ChatEntryItemViewModel">
            <!-- Eigene Nachrichten rechts und in Akzent hinterlegt, Antworten links
                 als schlichter Kasten — wie im Entwurf. -->
            <Border Classes="card" MaxWidth="660"
                    Classes.accent="{Binding IsUser}"
                    HorizontalAlignment="{Binding IsUser,
                        Converter={x:Static conv:ChatAlignmentConverter.Instance}}">
              <StackPanel Spacing="4">
                <DockPanel>
                  <TextBlock Text="{Binding Sender}" FontWeight="SemiBold" FontSize="12" />
                  <TextBlock Classes="caption" DockPanel.Dock="Right" HorizontalAlignment="Right"
                             Margin="8,0,0,0"
                             Text="{Binding Timestamp, StringFormat='{}{0:HH:mm}'}" />
                </DockPanel>

                <SelectableTextBlock Text="{Binding Text}" TextWrapping="Wrap" FontSize="13.5" />
              </StackPanel>
            </Border>
          </DataTemplate>
        </ItemsControl.ItemTemplate>
      </ItemsControl>
    </ScrollViewer>

  </DockPanel>

</UserControl>
