<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"
             mc:Ignorable="d" d:DesignWidth="1280" d:DesignHeight="820"
             x:Class="ClawdDotNet.Desktop.Views.AgentsPageView"
             x:DataType="vm:AgentsPageViewModel">

  <!--
    Vier Felder wie in der WinForms-Vorlage: oben Agentenliste und Agenteneinstellungen,
    unten Werkzeugliste und Werkzeugeinstellungen — aber mit ziehbaren Teilern statt
    fester SplitterDistance und mit flachen Feldern statt PropertyGrid.

    Die Teilerstellungen werden im Code-Behind gelesen und gespeichert: ColumnDefinition
    liegt nicht im Sichtbarkeitsbaum und erbt deshalb keinen DataContext — eine Bindung
    darauf wuerde stillschweigend nichts tun.
  -->
  <DockPanel>

    <TextBlock DockPanel.Dock="Bottom" Classes="caption" Margin="0,10,0,0"
               Text="{Binding StatusText}" TextWrapping="Wrap" />

    <Grid x:Name="OuterGrid" RowDefinitions="*,Auto,*">

      <!-- ══════════════════ Oben: Agenten ══════════════════ -->
      <Grid x:Name="TopGrid" Grid.Row="0" ColumnDefinitions="0.44*,Auto,0.56*">

        <!-- Liste -->
        <DockPanel Grid.Column="0" Margin="0,0,14,0">

          <WrapPanel DockPanel.Dock="Top" Classes="toolbar">
            <Button Classes="toolbar" Command="{Binding AddAgentCommand}">
              <StackPanel Orientation="Horizontal" Spacing="6">
                <ctrl:StrokeIcon Data="{StaticResource IconPlus}" Width="14" Height="14" />
                <TextBlock Text="Agent" />
              </StackPanel>
            </Button>

            <Button Classes="toolbar" Command="{Binding RemoveAgentCommand}"
                    IsEnabled="{Binding SelectedAgent, Converter={x:Static ObjectConverters.IsNotNull}}">
              <StackPanel Orientation="Horizontal" Spacing="6">
                <ctrl:StrokeIcon Data="{StaticResource IconMinus}" Width="14" Height="14" />
                <TextBlock Text="Agent" />
              </StackPanel>
            </Button>

            <Border Classes="sep" />

            <Button Classes="toolbar" Command="{Binding RunAgentNowCommand}"
                    IsEnabled="{Binding !IsRunning}">
              <StackPanel Orientation="Horizontal" Spacing="6">
                <ctrl:StrokeIcon Data="{StaticResource IconPlay}" Width="12" Height="12" IsFilled="True" />
                <TextBlock Text="Jetzt ausf." />
              </StackPanel>
            </Button>

            <Border Classes="sep" />

            <Button Classes="toolbar" Command="{Binding EditIdentityCommand}"
                    IsEnabled="{Binding SelectedAgent, Converter={x:Static ObjectConverters.IsNotNull}}">
              <StackPanel Orientation="Horizontal" Spacing="6">
                <ctrl:StrokeIcon Data="{StaticResource IconPencil}" Width="14" Height="14" />
                <TextBlock Text="Identity" />
              </StackPanel>
            </Button>

            <Button Classes="toolbar" Command="{Binding EditSoulCommand}"
                    IsEnabled="{Binding SelectedAgent, Converter={x:Static ObjectConverters.IsNotNull}}">
              <StackPanel Orientation="Horizontal" Spacing="6">
                <ctrl:StrokeIcon Data="{StaticResource IconPencil}" Width="14" Height="14" />
                <TextBlock Text="Soul" />
              </StackPanel>
            </Button>

            <Border Classes="sep" />

            <Button Classes="toolbar" Command="{Binding OpenAgentFolderCommand}"
                    IsEnabled="{Binding SelectedAgent, Converter={x:Static ObjectConverters.IsNotNull}}">
              <StackPanel Orientation="Horizontal" Spacing="6">
                <ctrl:StrokeIcon Data="{StaticResource IconFolder}" Width="14" Height="14" />
                <TextBlock Text="Ordner" />
              </StackPanel>
            </Button>

            <Button Classes="toolbar" Command="{Binding SaveAgentsCommand}">
              <StackPanel Orientation="Horizontal" Spacing="6">
                <ctrl:StrokeIcon Data="{StaticResource IconSave}" Width="14" Height="14" />
                <TextBlock Text="Speichern" />
              </StackPanel>
            </Button>
          </WrapPanel>

          <Border DockPanel.Dock="Top" Classes="thead">
            <Grid ColumnDefinitions="1.4*,1.6*,2*">
              <TextBlock Grid.Column="0" Classes="th" Text="ANZEIGENAME" />
              <TextBlock Grid.Column="1" Classes="th" Text="MODELL" />
              <TextBlock Grid.Column="2" Classes="th" Text="KURZINFO" />
            </Grid>
          </Border>

          <ListBox Classes="table" ItemsSource="{Binding Agents}"
                   SelectedItem="{Binding SelectedAgent}">
            <ListBox.ItemTemplate>
              <DataTemplate DataType="vm:AgentItemViewModel">
                <Border Classes="tr" Padding="7">
                  <Grid ColumnDefinitions="1.4*,1.6*,2*">
                    <TextBlock Grid.Column="0" Classes="td" FontWeight="SemiBold"
                               Text="{Binding DisplayName}" />
                    <TextBlock Grid.Column="1" Classes="td dim" Text="{Binding Model}" />
                    <TextBlock Grid.Column="2" Classes="td dim" Text="{Binding Description}" />
                  </Grid>
                </Border>
              </DataTemplate>
            </ListBox.ItemTemplate>
          </ListBox>
        </DockPanel>

        <GridSplitter x:Name="TopSplitter" Grid.Column="1" Classes="vertical"
                      ResizeDirection="Columns" />

        <!-- Einstellungen des gewählten Agenten -->
        <ScrollViewer Grid.Column="2" VerticalScrollBarVisibility="Auto" Padding="4,0,0,0">
          <ContentControl Content="{Binding SelectedAgent}">
            <ContentControl.ContentTemplate>
              <DataTemplate DataType="vm:AgentItemViewModel">
                <StackPanel Spacing="20" MaxWidth="640" HorizontalAlignment="Left">

                  <StackPanel Spacing="12">
                    <TextBlock Classes="kicker" Text="ALLGEMEIN &amp; MODELL" />
                    <TextBlock Classes="h2" Text="{Binding DisplayName}" />

                    <Grid ColumnDefinitions="*,12,*">
                      <StackPanel Grid.Column="0">
                        <TextBlock Classes="label" Text="Agent-ID" />
                        <TextBox Text="{Binding AgentId}" IsReadOnly="True" />
                      </StackPanel>
                      <StackPanel Grid.Column="2">
                        <TextBlock Classes="label" Text="Anzeigename" />
                        <TextBox Text="{Binding DisplayName}" />
                      </StackPanel>
                    </Grid>

                    <Grid ColumnDefinitions="*,12,*">
                      <StackPanel Grid.Column="0">
                        <TextBlock Classes="label" Text="Modell" />
                        <ComboBox HorizontalAlignment="Stretch"
                                  ItemsSource="{Binding $parent[UserControl].((vm:AgentsPageViewModel)DataContext).KnownModels}"
                                  SelectedItem="{Binding Model}" />
                      </StackPanel>
                      <StackPanel Grid.Column="2">
                        <TextBlock Classes="label" Text="Prompt Caching" />
                        <ComboBox HorizontalAlignment="Stretch"
                                  ItemsSource="{Binding $parent[UserControl].((vm:AgentsPageViewModel)DataContext).CachingOptions}"
                                  SelectedItem="{Binding PromptCaching}" />
                      </StackPanel>
                    </Grid>

                    <StackPanel>
                      <TextBlock Classes="label" Text="Rolle / Beschreibung" />
                      <TextBox Text="{Binding Description}" AcceptsReturn="True" Height="54"
                               TextWrapping="Wrap" />
                    </StackPanel>

                    <StackPanel>
                      <TextBlock Classes="label" Text="System-Prompt (zusätzliche Anweisungen)" />
                      <TextBox Text="{Binding SystemPrompt}" AcceptsReturn="True" Height="70"
                               TextWrapping="Wrap" />
                    </StackPanel>
                  </StackPanel>

                  <Border Classes="hr" />

                  <StackPanel Spacing="12">
                    <TextBlock Classes="kicker" Text="GRENZEN" />
                    <TextBlock Classes="h2" Text="Schutzgrenzen (LoopGuard &amp; Budget)" />

                    <Grid ColumnDefinitions="*,12,*">
                      <StackPanel Grid.Column="0">
                        <TextBlock Classes="label" Text="Max. Schritte pro Run" />
                        <TextBox Text="{Binding MaxSteps}" />
                      </StackPanel>
                      <StackPanel Grid.Column="2">
                        <TextBlock Classes="label" Text="Timeout (Sekunden)" />
                        <TextBox Text="{Binding TimeoutSeconds}" />
                      </StackPanel>
                    </Grid>

                    <Grid ColumnDefinitions="*,12,*">
                      <StackPanel Grid.Column="0">
                        <TextBlock Classes="label" Text="Max. Kontext-Tokens" />
                        <TextBox Text="{Binding MaxContextTokens}" />
                      </StackPanel>
                      <StackPanel Grid.Column="2">
                        <TextBlock Classes="label" Text="Max. Tool-Ergebnis (Zeichen)" />
                        <TextBox Text="{Binding MaxToolResultChars}" />
                      </StackPanel>
                    </Grid>

                    <Grid ColumnDefinitions="*,12,*">
                      <StackPanel Grid.Column="0">
                        <TextBlock Classes="label" Text="Tagesbudget ($ USD)" />
                        <TextBox Text="{Binding DailyCostUsd}" />
                      </StackPanel>
                      <StackPanel Grid.Column="2">
                        <TextBlock Classes="label" Text="Tagesbudget (Tokens)" />
                        <TextBox Text="{Binding DailyTokens}" />
                      </StackPanel>
                    </Grid>
                  </StackPanel>

                </StackPanel>
              </DataTemplate>
            </ContentControl.ContentTemplate>
          </ContentControl>
        </ScrollViewer>
      </Grid>

      <GridSplitter x:Name="VerticalSplitter" Grid.Row="1" Classes="horizontal"
                    ResizeDirection="Rows" Margin="0,8" />

      <!-- ══════════════════ Unten: Werkzeuge ══════════════════ -->
      <Grid x:Name="BottomGrid" Grid.Row="2" ColumnDefinitions="0.44*,Auto,0.56*">

        <DockPanel Grid.Column="0" Margin="0,0,14,0">
          <TextBlock DockPanel.Dock="Top" Classes="kicker" Margin="0,4,0,8"
                     Text="{Binding SelectedAgent.DisplayName,
                            StringFormat='ZUGELASSENE WERKZEUGE FÜR {0}'}" />

          <Border DockPanel.Dock="Top" Classes="thead">
            <Grid ColumnDefinitions="36,1.4*,2.4*">
              <TextBlock Grid.Column="1" Classes="th" Text="WERKZEUG" />
              <TextBlock Grid.Column="2" Classes="th" Text="BESCHREIBUNG" />
            </Grid>
          </Border>

          <ListBox Classes="table" ItemsSource="{Binding SelectedAgent.AvailableTools}"
                   SelectedItem="{Binding SelectedTool}">
            <ListBox.ItemTemplate>
              <DataTemplate DataType="vm:ToolItemViewModel">
                <Border Classes="tr" Padding="7">
                  <Grid ColumnDefinitions="36,1.4*,2.4*">
                    <CheckBox Grid.Column="0" IsChecked="{Binding IsEnabled}"
                              VerticalAlignment="Center" />
                    <TextBlock Grid.Column="1" Classes="td" FontWeight="SemiBold"
                               Text="{Binding Name}" />
                    <TextBlock Grid.Column="2" Classes="td dim" Text="{Binding Description}" />
                  </Grid>
                </Border>
              </DataTemplate>
            </ListBox.ItemTemplate>
          </ListBox>
        </DockPanel>

        <GridSplitter x:Name="BottomSplitter" Grid.Column="1" Classes="vertical"
                      ResizeDirection="Columns" />

        <DockPanel Grid.Column="2" Margin="4,0,0,0">
          <StackPanel DockPanel.Dock="Top" Spacing="2" Margin="0,4,0,10">
            <TextBlock Classes="kicker" Text="WERKZEUG-EINSTELLUNGEN" />
            <DockPanel>
              <TextBlock Classes="h2" VerticalAlignment="Center"
                         Text="{Binding SelectedTool.Name}" />
              <Button DockPanel.Dock="Right" HorizontalAlignment="Right" Classes="toolbar primary"
                      Command="{Binding SaveToolSettingsCommand}"
                      IsEnabled="{Binding ToolSettings, Converter={x:Static ObjectConverters.IsNotNull}}">
                <StackPanel Orientation="Horizontal" Spacing="6">
                  <ctrl:StrokeIcon Data="{StaticResource IconSave}" Width="14" Height="14" />
                  <TextBlock Text="Übernehmen" />
                </StackPanel>
              </Button>
            </DockPanel>
          </StackPanel>

          <ScrollViewer VerticalScrollBarVisibility="Auto">
            <ContentControl Content="{Binding ToolSettings}" />
          </ScrollViewer>
        </DockPanel>
      </Grid>

    </Grid>
  </DockPanel>

</UserControl>
