<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:ClaudeDo.Ui.ViewModels.Modals"
        x:Class="ClaudeDo.Ui.Views.Modals.SettingsModalView"
        x:DataType="vm:SettingsModalViewModel"
        Title="Settings"
        Width="580" Height="760"
        SystemDecorations="None"
        ExtendClientAreaToDecorationsHint="True"
        WindowStartupLocation="CenterOwner"
        Background="{DynamicResource SurfaceBrush}">

  <Window.KeyBindings>
    <KeyBinding Gesture="Escape" Command="{Binding CancelCommand}"/>
  </Window.KeyBindings>

  <Window.Styles>
    <Style Selector="TextBlock.section-label">
      <Setter Property="FontFamily" Value="{DynamicResource MonoFont}"/>
      <Setter Property="FontSize" Value="10"/>
      <Setter Property="LetterSpacing" Value="1.4"/>
      <Setter Property="Foreground" Value="{DynamicResource TextFaintBrush}"/>
      <Setter Property="Margin" Value="4,0,0,6"/>
    </Style>
    <Style Selector="TextBlock.field-label">
      <Setter Property="FontSize" Value="11"/>
      <Setter Property="Foreground" Value="{DynamicResource TextDimBrush}"/>
      <Setter Property="Margin" Value="0,0,0,4"/>
    </Style>
    <Style Selector="TextBlock.path-mono">
      <Setter Property="FontFamily" Value="{DynamicResource MonoFont}"/>
      <Setter Property="FontSize" Value="11"/>
      <Setter Property="Foreground" Value="{DynamicResource TextDimBrush}"/>
      <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
    </Style>
    <Style Selector="Border.section">
      <Setter Property="BorderBrush" Value="{DynamicResource LineBrush}"/>
      <Setter Property="BorderThickness" Value="1"/>
      <Setter Property="CornerRadius" Value="6"/>
      <Setter Property="Padding" Value="14"/>
      <Setter Property="Background" Value="{DynamicResource DeepBrush}"/>
    </Style>
    <Style Selector="Button.danger">
      <Setter Property="Background" Value="{DynamicResource BloodBrush}"/>
      <Setter Property="Foreground" Value="White"/>
    </Style>
    <Style Selector="Button.primary">
      <Setter Property="Background" Value="{DynamicResource AccentBrush}"/>
      <Setter Property="Foreground" Value="{DynamicResource DeepBrush}"/>
      <Setter Property="FontWeight" Value="SemiBold"/>
    </Style>
  </Window.Styles>

  <Border Background="{DynamicResource SurfaceBrush}"
          BorderBrush="{DynamicResource LineBrush}"
          BorderThickness="1">
    <Grid RowDefinitions="36,*,52">

      <!-- Title bar -->
      <Border Grid.Row="0"
              x:Name="TitleBar"
              Background="{DynamicResource DeepBrush}"
              BorderBrush="{DynamicResource LineBrush}"
              BorderThickness="0,0,0,1"
              PointerPressed="TitleBar_PointerPressed">
        <Grid ColumnDefinitions="*,Auto" Margin="14,0">
          <TextBlock Text="SETTINGS"
                     FontFamily="{DynamicResource MonoFont}"
                     FontSize="11"
                     LetterSpacing="1.4"
                     Foreground="{DynamicResource TextBrush}"
                     VerticalAlignment="Center"/>
          <Button Grid.Column="1"
                  Classes="icon-btn"
                  Content="✕"
                  FontSize="12"
                  Command="{Binding CancelCommand}"
                  VerticalAlignment="Center"/>
        </Grid>
      </Border>

      <!-- Scrollable body -->
      <ScrollViewer Grid.Row="1" Padding="20,16">
        <StackPanel Spacing="18">

          <!-- CLAUDE DEFAULTS -->
          <StackPanel Spacing="0">
            <TextBlock Classes="section-label" Text="CLAUDE DEFAULTS"/>
            <Border Classes="section">
              <StackPanel Spacing="12">
                <StackPanel Spacing="4">
                  <TextBlock Classes="field-label" Text="Default instructions"/>
                  <TextBox AcceptsReturn="True"
                           TextWrapping="Wrap"
                           Height="110"
                           Watermark="Baseline instructions applied to every task (e.g. 'speak German', 'never touch .env')"
                           Text="{Binding DefaultClaudeInstructions, Mode=TwoWay}"/>
                </StackPanel>

                <Grid ColumnDefinitions="*,12,*,12,*">
                  <StackPanel Grid.Column="0" Spacing="4">
                    <TextBlock Classes="field-label" Text="Model"/>
                    <ComboBox ItemsSource="{Binding Models}"
                              SelectedItem="{Binding DefaultModel, Mode=TwoWay}"
                              HorizontalAlignment="Stretch"/>
                  </StackPanel>
                  <StackPanel Grid.Column="2" Spacing="4">
                    <TextBlock Classes="field-label" Text="Max turns"/>
                    <NumericUpDown Value="{Binding DefaultMaxTurns, Mode=TwoWay}"
                                   Minimum="1" Maximum="200" Increment="1" FormatString="0"/>
                  </StackPanel>
                  <StackPanel Grid.Column="4" Spacing="4">
                    <TextBlock Classes="field-label" Text="Permission"/>
                    <ComboBox ItemsSource="{Binding PermissionModes}"
                              SelectedItem="{Binding DefaultPermissionMode, Mode=TwoWay}"
                              HorizontalAlignment="Stretch"/>
                  </StackPanel>
                </Grid>
              </StackPanel>
            </Border>
          </StackPanel>

          <!-- WORKTREES -->
          <StackPanel Spacing="0">
            <TextBlock Classes="section-label" Text="WORKTREES"/>
            <Border Classes="section">
              <StackPanel Spacing="12">
                <Grid ColumnDefinitions="*,12,2*">
                  <StackPanel Grid.Column="0" Spacing="4">
                    <TextBlock Classes="field-label" Text="Strategy"/>
                    <ComboBox ItemsSource="{Binding WorktreeStrategies}"
                              SelectedItem="{Binding WorktreeStrategy, Mode=TwoWay}"
                              HorizontalAlignment="Stretch"/>
                  </StackPanel>
                  <StackPanel Grid.Column="2" Spacing="4">
                    <TextBlock Classes="field-label" Text="Central worktree root"/>
                    <TextBox Text="{Binding CentralWorktreeRoot, Mode=TwoWay}"
                             Watermark="e.g. C:\worktrees"/>
                  </StackPanel>
                </Grid>

                <StackPanel Orientation="Horizontal" Spacing="8">
                  <CheckBox IsChecked="{Binding WorktreeAutoCleanupEnabled, Mode=TwoWay}"
                            Content="Auto-cleanup finished worktrees after"
                            VerticalAlignment="Center"/>
                  <NumericUpDown Value="{Binding WorktreeAutoCleanupDays, Mode=TwoWay}"
                                 Width="130" Minimum="1" Maximum="365" Increment="1" FormatString="0"
                                 IsEnabled="{Binding WorktreeAutoCleanupEnabled}"/>
                  <TextBlock Text="days" VerticalAlignment="Center"
                             Foreground="{DynamicResource TextDimBrush}"/>
                </StackPanel>

                <Border BorderBrush="{DynamicResource LineBrush}" BorderThickness="0,1,0,0" Margin="0,4,0,0"/>

                <StackPanel Spacing="8">
                  <Button Content="Cleanup finished worktrees"
                          Command="{Binding CleanupWorktreesCommand}"
                          HorizontalAlignment="Left"/>

                  <!-- Force-remove: button vs. confirm bar -->
                  <StackPanel>
                    <Button Content="Force-remove all worktrees"
                            Classes="danger"
                            Command="{Binding RequestResetConfirmCommand}"
                            HorizontalAlignment="Left"
                            IsVisible="{Binding !ShowResetConfirm}"/>

                    <Border BorderBrush="{DynamicResource BloodBrush}" BorderThickness="1"
                            CornerRadius="6" Padding="12,10"
                            IsVisible="{Binding ShowResetConfirm}">
                      <StackPanel Spacing="8">
                        <TextBlock Text="Remove ALL worktrees? Uncommitted work will be lost."
                                   Foreground="{DynamicResource TextBrush}" TextWrapping="Wrap"/>
                        <StackPanel Orientation="Horizontal" Spacing="8">
                          <Button Content="Cancel" Command="{Binding CancelResetConfirmCommand}"/>
                          <Button Content="Remove All" Classes="danger"
                                  Command="{Binding ConfirmResetAllCommand}"/>
                        </StackPanel>
                      </StackPanel>
                    </Border>
                  </StackPanel>
                </StackPanel>
              </StackPanel>
            </Border>
          </StackPanel>

          <!-- AGENTS -->
          <StackPanel Spacing="0">
            <TextBlock Classes="section-label" Text="AGENTS"/>
            <Border Classes="section">
              <StackPanel Spacing="8">
                <TextBlock Text="Restore bundled default agents (code-reviewer, test-writer, debugger, security-reviewer, explorer, researcher). Existing files are not overwritten."
                           FontSize="11"
                           TextWrapping="Wrap"
                           Foreground="{DynamicResource TextDimBrush}"/>
                <Button Content="Restore default agents"
                        Command="{Binding RestoreDefaultAgentsCommand}"
                        IsEnabled="{Binding !IsBusy}"
                        HorizontalAlignment="Left"/>
              </StackPanel>
            </Border>
          </StackPanel>

          <!-- ABOUT -->
          <StackPanel Spacing="0">
            <TextBlock Classes="section-label" Text="ABOUT"/>
            <Border Classes="section">
              <Grid RowDefinitions="Auto,Auto,Auto,Auto" ColumnDefinitions="90,*,Auto" RowSpacing="8">
                <TextBlock Grid.Row="0" Grid.Column="0" Classes="field-label" Text="Version"
                           VerticalAlignment="Center"/>
                <TextBlock Grid.Row="0" Grid.Column="1" Classes="path-mono"
                           Text="{Binding AppVersion}" VerticalAlignment="Center"/>

                <TextBlock Grid.Row="1" Grid.Column="0" Classes="field-label" Text="Data"
                           VerticalAlignment="Center"/>
                <TextBlock Grid.Row="1" Grid.Column="1" Classes="path-mono"
                           Text="{Binding DataFolderPath}" VerticalAlignment="Center"/>
                <Button Grid.Row="1" Grid.Column="2" Content="Open"
                        Command="{Binding OpenPathCommand}"
                        CommandParameter="{Binding DataFolderPath}"/>

                <TextBlock Grid.Row="2" Grid.Column="0" Classes="field-label" Text="Logs"
                           VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Grid.Column="1" Classes="path-mono"
                           Text="{Binding LogsFolderPath}" VerticalAlignment="Center"/>
                <Button Grid.Row="2" Grid.Column="2" Content="Open"
                        Command="{Binding OpenPathCommand}"
                        CommandParameter="{Binding LogsFolderPath}"/>

                <TextBlock Grid.Row="3" Grid.Column="0" Classes="field-label" Text="Config"
                           VerticalAlignment="Center"/>
                <TextBlock Grid.Row="3" Grid.Column="1" Classes="path-mono"
                           Text="{Binding WorkerConfigPath}" VerticalAlignment="Center"/>
                <Button Grid.Row="3" Grid.Column="2" Content="Open"
                        Command="{Binding OpenPathCommand}"
                        CommandParameter="{Binding WorkerConfigPath}"/>
              </Grid>
            </Border>
          </StackPanel>

          <!-- Inline status / error -->
          <TextBlock Text="{Binding ValidationError}"
                     Foreground="{DynamicResource BloodBrush}"
                     FontSize="11"
                     IsVisible="{Binding ValidationError, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"/>
          <TextBlock Text="{Binding StatusMessage}"
                     Foreground="{DynamicResource TextDimBrush}"
                     FontSize="11"
                     IsVisible="{Binding StatusMessage, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"/>
        </StackPanel>
      </ScrollViewer>

      <!-- Footer -->
      <Border Grid.Row="2"
              Background="{DynamicResource DeepBrush}"
              BorderBrush="{DynamicResource LineBrush}"
              BorderThickness="0,1,0,0">
        <StackPanel Orientation="Horizontal" Spacing="8"
                    HorizontalAlignment="Right" VerticalAlignment="Center"
                    Margin="16,0">
          <Button Content="Cancel" Command="{Binding CancelCommand}" MinWidth="90"/>
          <Button Content="Save" Classes="primary"
                  Command="{Binding SaveCommand}"
                  IsEnabled="{Binding !IsBusy}" MinWidth="90"/>
        </StackPanel>
      </Border>

    </Grid>
  </Border>
</Window>
