<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="1100" d:DesignHeight="740"
             x:Class="ClawdDotNet.Desktop.Views.BackupPageView"
             x:DataType="vm:BackupPageViewModel">

  <DockPanel>

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

    <ScrollViewer VerticalScrollBarVisibility="Auto">
      <StackPanel Spacing="24" MaxWidth="820" HorizontalAlignment="Left">

        <!-- ══════════════════ Manuell ══════════════════ -->
        <StackPanel Spacing="12">
          <TextBlock Classes="kicker" Text="MANUELL" />
          <TextBlock Classes="h2" Text="Sicherung erstellen" />

          <Grid ColumnDefinitions="*,Auto" VerticalAlignment="Bottom">
            <StackPanel Grid.Column="0">
              <TextBlock Classes="label" Text="Zielordner" />
              <TextBox Text="{Binding TargetFolder}"
                       PlaceholderText="Zielordner für Sicherungen…" />
            </StackPanel>
            <Button Grid.Column="1" Content="Durchsuchen…" Click="OnBrowseTargetFolder"
                    Margin="8,0,0,0" VerticalAlignment="Bottom" />
          </Grid>

          <StackPanel Spacing="6">
            <TextBlock Classes="label" Text="Zugangsdaten" />
            <StackPanel Orientation="Horizontal" Spacing="18">
              <RadioButton GroupName="secrets" Content="nicht mitsichern"
                           IsChecked="{Binding SecretsExclude}" />
              <RadioButton GroupName="secrets" Content="mit Passphrase schützen"
                           IsChecked="{Binding SecretsPassphrase}" />
            </StackPanel>
          </StackPanel>

          <StackPanel Spacing="8" Margin="24,0,0,0" IsVisible="{Binding SecretsPassphrase}">
            <TextBox Text="{Binding Passphrase}" PasswordChar="•" Width="320"
                     HorizontalAlignment="Left" PlaceholderText="Passphrase eingeben" />
            <TextBox Text="{Binding PassphraseRepeat}" PasswordChar="•" Width="320"
                     HorizontalAlignment="Left" PlaceholderText="Passphrase wiederholen" />
          </StackPanel>

          <StackPanel Spacing="6">
            <TextBlock Classes="label" Text="Umfang" />
            <StackPanel Orientation="Horizontal" Spacing="18">
              <CheckBox Content="Chatverläufe" IsChecked="{Binding IncludeChatHistory}" />
              <CheckBox Content="Protokolle" IsChecked="{Binding IncludeLogs}" />
            </StackPanel>
          </StackPanel>

          <Button Classes="primary" HorizontalAlignment="Left" Margin="0,4,0,0"
                  Command="{Binding CreateBackupCommand}" IsEnabled="{Binding !IsBusy}">
            <StackPanel Orientation="Horizontal" Spacing="6">
              <ctrl:StrokeIcon Data="{StaticResource IconPlay}" Width="12" Height="12" IsFilled="True" />
              <TextBlock Text="Sicherung jetzt erstellen" />
            </StackPanel>
          </Button>
        </StackPanel>

        <Border Classes="hr" />

        <!-- ══════════════════ Automatik ══════════════════ -->
        <StackPanel Spacing="10">
          <TextBlock Classes="kicker" Text="AUTOMATIK" />
          <TextBlock Classes="h2" Text="Automatische Sicherung" />

          <CheckBox Content="täglich sichern um" IsChecked="{Binding AutoBackupEnabled}" />

          <StackPanel Orientation="Horizontal" Spacing="24" Margin="24,0,0,0"
                      IsEnabled="{Binding AutoBackupEnabled}">
            <StackPanel Width="100">
              <TextBlock Classes="label" Text="Uhrzeit" />
              <TextBox Text="{Binding AutoBackupTime}" PlaceholderText="03:00" />
            </StackPanel>

            <StackPanel Width="160">
              <TextBlock Classes="label" Text="Aufbewahren" />
              <TextBox Text="{Binding KeepCount}" />
            </StackPanel>
          </StackPanel>

          <TextBlock Classes="caption" Margin="24,0,0,0" TextWrapping="Wrap"
                     Text="Die Automatik sichert ohne Zugangsdaten — eine gespeicherte Passphrase wäre wirkungslos." />
        </StackPanel>

        <Border Classes="hr" />

        <!-- ══════════════════ Bestand ══════════════════ -->
        <StackPanel Spacing="10">
          <TextBlock Classes="kicker" Text="BESTAND" />

          <DockPanel>
            <TextBlock Classes="h2" Text="Vorhandene Sicherungen" VerticalAlignment="Center" />

            <WrapPanel DockPanel.Dock="Right" HorizontalAlignment="Right" Classes="toolbar"
                       Margin="0">
              <Button Classes="toolbar" Content="Aktualisieren"
                      Command="{Binding RefreshBackupListCommand}" />
              <Button Classes="toolbar" Content="Ordner öffnen"
                      Command="{Binding ShowSelectedInFolderCommand}"
                      IsEnabled="{Binding SelectedBackup, Converter={x:Static ObjectConverters.IsNotNull}}" />
              <Button Classes="toolbar" Content="Löschen"
                      Command="{Binding DeleteSelectedCommand}"
                      IsEnabled="{Binding SelectedBackup, Converter={x:Static ObjectConverters.IsNotNull}}" />
              <Button Classes="toolbar primary" Content="Wiederherstellen…"
                      Command="{Binding RestoreSelectedCommand}"
                      IsEnabled="{Binding SelectedBackup, Converter={x:Static ObjectConverters.IsNotNull}}" />
            </WrapPanel>
          </DockPanel>

          <Border Classes="thead">
            <Grid ColumnDefinitions="2.6*,1.5*,1.3*,0.9*,1.3*">
              <TextBlock Grid.Column="0" Classes="th" Text="DATEINAME" />
              <TextBlock Grid.Column="1" Classes="th" Text="ERSTELLT AM" />
              <TextBlock Grid.Column="2" Classes="th" Text="INSTANZ" />
              <TextBlock Grid.Column="3" Classes="th" Text="GRÖSSE" />
              <TextBlock Grid.Column="4" Classes="th" Text="ZUGANGSDATEN" />
            </Grid>
          </Border>

          <ListBox Classes="table" MaxHeight="300" Margin="0,-10,0,0"
                   ItemsSource="{Binding Backups}" SelectedItem="{Binding SelectedBackup}">
            <ListBox.ItemTemplate>
              <DataTemplate DataType="vm:BackupItemViewModel">
                <Border Classes="tr" Padding="7">
                  <Grid ColumnDefinitions="2.6*,1.5*,1.3*,0.9*,1.3*">
                    <TextBlock Grid.Column="0" Classes="td" Text="{Binding FileName}" />
                    <TextBlock Grid.Column="1" Classes="td"
                               Text="{Binding LastWriteTime, StringFormat='{}{0:dd.MM.yyyy HH:mm}'}" />
                    <TextBlock Grid.Column="2" Classes="td" Text="{Binding InstanceName}" />
                    <TextBlock Grid.Column="3" Classes="td" Text="{Binding SizeFormatted}" />
                    <TextBlock Grid.Column="4" Classes="td dim" Text="{Binding SecretsInfo}" />
                  </Grid>
                </Border>
              </DataTemplate>
            </ListBox.ItemTemplate>
          </ListBox>
        </StackPanel>

      </StackPanel>
    </ScrollViewer>

  </DockPanel>
</UserControl>
