<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="using:ClaudeDo.Ui.ViewModels.Agent"
             xmlns:ctl="using:ClaudeDo.Ui.Views.Controls"
             xmlns:loc="using:ClaudeDo.Ui.Localization"
             x:Class="ClaudeDo.Ui.Views.Controls.AgentConfigEditor"
             x:DataType="vm:AgentConfigEditorViewModel"
             x:Name="Root">
  <StackPanel Spacing="12">

    <!-- Model -->
    <StackPanel Spacing="4">
      <Grid ColumnDefinitions="Auto,Auto,*,Auto" ColumnSpacing="6">
        <TextBlock Grid.Column="0" Classes="field-label" Text="{loc:Tr settings.agentEditor.model}" VerticalAlignment="Center"/>
        <ctl:InheritedBadge Grid.Column="1" BadgeText="{Binding ModelBadge}"/>
        <Button Grid.Column="3" Classes="icon-btn" Content="↺" ToolTip.Tip="{loc:Tr settings.inherit.resetToInherited}"
                Command="{Binding ResetModelCommand}"/>
      </Grid>
      <ComboBox ItemsSource="{Binding ModelOptions}"
                SelectedItem="{Binding Model, Mode=TwoWay}"
                PlaceholderText="{Binding ModelInheritedHint}"
                HorizontalAlignment="Stretch"/>
    </StackPanel>

    <!-- Max turns -->
    <StackPanel Spacing="4">
      <Grid ColumnDefinitions="Auto,Auto,*,Auto" ColumnSpacing="6">
        <TextBlock Grid.Column="0" Classes="field-label" Text="{loc:Tr settings.agentEditor.maxTurns}" VerticalAlignment="Center"/>
        <ctl:InheritedBadge Grid.Column="1" BadgeText="{Binding TurnsBadge}"/>
        <Button Grid.Column="3" Classes="icon-btn" Content="↺" ToolTip.Tip="{loc:Tr settings.inherit.resetToInherited}"
                Command="{Binding ResetTurnsCommand}"/>
      </Grid>
      <NumericUpDown Value="{Binding MaxTurns, Mode=TwoWay}"
                     PlaceholderText="{Binding TurnsInheritedHint}"
                     Minimum="1" Maximum="200" Increment="1" FormatString="0"
                     HorizontalAlignment="Stretch"/>
    </StackPanel>

    <!-- System prompt -->
    <StackPanel Spacing="4">
      <TextBlock Classes="field-label" Text="{loc:Tr settings.agentEditor.systemPrompt}"/>
      <TextBox Text="{Binding SystemPrompt, Mode=TwoWay}"
               AcceptsReturn="True" TextWrapping="Wrap" MinHeight="80"/>
      <TextBlock Classes="meta" Opacity="0.6"
                 Text="{loc:Tr settings.agentEditor.promptPrepended}"
                 IsVisible="{Binding EffectiveSystemPromptHint, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"/>
      <TextBlock Classes="meta" Opacity="0.6" TextWrapping="Wrap"
                 Text="{Binding EffectiveSystemPromptHint}"
                 IsVisible="{Binding EffectiveSystemPromptHint, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"/>
    </StackPanel>

    <!-- Agent file -->
    <StackPanel Spacing="4">
      <Grid ColumnDefinitions="Auto,Auto,*,Auto" ColumnSpacing="6">
        <TextBlock Grid.Column="0" Classes="field-label" Text="{loc:Tr settings.agentEditor.agentFile}" VerticalAlignment="Center"/>
        <ctl:InheritedBadge Grid.Column="1" BadgeText="{Binding AgentBadge}"/>
        <Button Grid.Column="3" Classes="icon-btn" Content="↺" ToolTip.Tip="{loc:Tr settings.inherit.resetToInherited}"
                Command="{Binding ResetAgentCommand}"/>
      </Grid>
      <Grid ColumnDefinitions="*,Auto">
        <ComboBox Grid.Column="0"
                  ItemsSource="{Binding Agents}"
                  SelectedItem="{Binding SelectedAgent, Mode=TwoWay}"
                  HorizontalAlignment="Stretch">
          <ComboBox.ItemTemplate>
            <DataTemplate>
              <StackPanel>
                <TextBlock Classes="title" Text="{Binding Name}"/>
                <TextBlock Classes="meta" Text="{Binding Description}"
                           IsVisible="{Binding Description, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"/>
              </StackPanel>
            </DataTemplate>
          </ComboBox.ItemTemplate>
        </ComboBox>
        <Button Classes="btn" Grid.Column="1" Content="{loc:Tr settings.agentEditor.browse}"
                Margin="8,0,0,0" Click="BrowseAgentClicked"
                IsVisible="{Binding #Root.ShowAgentBrowse}"/>
      </Grid>
      <TextBlock Classes="path-mono" Text="{Binding SelectedAgent.Path}"
                 TextTrimming="PrefixCharacterEllipsis"
                 IsVisible="{Binding SelectedAgent.Path, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"/>
    </StackPanel>

    <!-- Session skills (additive union with list/global; no inheritance badge) -->
    <StackPanel Spacing="4">
      <TextBlock Classes="field-label" Text="{loc:Tr settings.agentEditor.sessionSkills}"/>
      <TextBlock Classes="meta" Opacity="0.6" TextWrapping="Wrap"
                 Text="{loc:Tr settings.agentEditor.sessionSkillsHint}"/>
      <ItemsControl ItemsSource="{Binding SessionSkills}">
        <ItemsControl.ItemTemplate>
          <DataTemplate x:DataType="vm:SelectableSkillViewModel">
            <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" Content="{Binding Name}"
                      ToolTip.Tip="{Binding Description}"/>
          </DataTemplate>
        </ItemsControl.ItemTemplate>
      </ItemsControl>
    </StackPanel>

  </StackPanel>
</UserControl>
