<Window x:Class="MarcusLaw.OutlookAddin.UI.Dialogs.FileToCaseDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:conv="clr-namespace:MarcusLaw.OutlookAddin.UI.Converters"
        Title="תיוק מייל ל-Klear"
        Height="560" Width="680"
        MinHeight="420" MinWidth="540"
        FlowDirection="RightToLeft"
        WindowStartupLocation="CenterOwner"
        ShowInTaskbar="False"
        ResizeMode="CanResize"
        Background="#F8FAFC"
        FontFamily="Segoe UI, David, Frank Ruehl, Arial">
    <Window.Resources>
        <conv:EntityTypeToHebrewConverter x:Key="HebrewType" />
        <conv:EntityTypeToBrushConverter  x:Key="TypeBrush" />
        <conv:EntityTypeToIconConverter   x:Key="TypeIcon" />

        <SolidColorBrush x:Key="Primary"      Color="#2563EB" />
        <SolidColorBrush x:Key="PrimaryHover" Color="#1D4ED8" />
        <SolidColorBrush x:Key="Border"       Color="#E2E8F0" />
        <SolidColorBrush x:Key="Muted"        Color="#64748B" />
        <SolidColorBrush x:Key="CardBg"       Color="White" />
        <SolidColorBrush x:Key="HoverBg"      Color="#F1F5F9" />
        <SolidColorBrush x:Key="SelectedBg"   Color="#DBEAFE" />

        <Style x:Key="Card" TargetType="Border">
            <Setter Property="Background" Value="{StaticResource CardBg}" />
            <Setter Property="BorderBrush" Value="{StaticResource Border}" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="CornerRadius" Value="6" />
        </Style>

        <Style x:Key="PrimaryButton" TargetType="Button">
            <Setter Property="Background" Value="{StaticResource Primary}" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Padding" Value="22,8" />
            <Setter Property="FontWeight" Value="SemiBold" />
            <Setter Property="MinWidth" Value="110" />
            <Setter Property="Cursor" Value="Hand" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="Bd"
                                Background="{TemplateBinding Background}"
                                CornerRadius="5"
                                Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Bd" Property="Background" Value="{StaticResource PrimaryHover}" />
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="Bd" Property="Background" Value="#94A3B8" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="SecondaryButton" TargetType="Button">
            <Setter Property="Background" Value="White" />
            <Setter Property="Foreground" Value="#0F172A" />
            <Setter Property="BorderBrush" Value="{StaticResource Border}" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="Padding" Value="20,8" />
            <Setter Property="MinWidth" Value="110" />
            <Setter Property="Cursor" Value="Hand" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="Bd"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="5"
                                Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Bd" Property="Background" Value="{StaticResource HoverBg}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="LinkButton" TargetType="Button">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Foreground" Value="{StaticResource Primary}" />
            <Setter Property="Padding" Value="0" />
            <Setter Property="Cursor" Value="Hand" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <ContentPresenter HorizontalAlignment="Right" VerticalAlignment="Center" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="ResultItem" TargetType="ListBoxItem">
            <Setter Property="Padding" Value="0" />
            <Setter Property="Margin" Value="0" />
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border x:Name="Bd"
                                Background="{TemplateBinding Background}"
                                CornerRadius="4"
                                Margin="2,1"
                                Padding="8,6">
                            <ContentPresenter />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Bd" Property="Background" Value="{StaticResource HoverBg}" />
                            </Trigger>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter TargetName="Bd" Property="Background" Value="{StaticResource SelectedBg}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <Grid Margin="16">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <!-- Search row -->
        <Border Grid.Row="0" Style="{StaticResource Card}" Padding="10,8" Margin="0,0,0,12">
            <DockPanel LastChildFill="True">
                <TextBlock DockPanel.Dock="Left"
                           Text="🔍"
                           VerticalAlignment="Center"
                           FontSize="16"
                           Margin="0,0,8,0" />
                <TextBox x:Name="SearchBox"
                         Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}"
                         BorderThickness="0"
                         Background="Transparent"
                         FontSize="14"
                         VerticalAlignment="Center" />
            </DockPanel>
        </Border>

        <!-- Two-column body: results + recents -->
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="200" />
            </Grid.ColumnDefinitions>

            <!-- Results -->
            <Border Grid.Column="0" Style="{StaticResource Card}">
                <ListBox ItemsSource="{Binding ResultsView}"
                         SelectedItem="{Binding SelectedTarget}"
                         BorderThickness="0"
                         Background="Transparent"
                         ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                         ItemContainerStyle="{StaticResource ResultItem}"
                         Padding="4">
                    <ListBox.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <Border Background="#F8FAFC" Padding="8,6" Margin="0,4,0,2" CornerRadius="3"
                                            BorderBrush="{Binding Name, Converter={StaticResource TypeBrush}}"
                                            BorderThickness="0,0,3,0">
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="{Binding Name, Converter={StaticResource TypeIcon}}"
                                                       FontSize="14"
                                                       Margin="0,0,6,0" />
                                            <TextBlock Text="{Binding Name, Converter={StaticResource HebrewType}}"
                                                       FontWeight="SemiBold"
                                                       Foreground="{Binding Name, Converter={StaticResource TypeBrush}}" />
                                            <TextBlock Text=" · " Foreground="{StaticResource Muted}" />
                                            <TextBlock Text="{Binding ItemCount, StringFormat='{}{0} תוצאות'}"
                                                       Foreground="{StaticResource Muted}"
                                                       FontSize="11" />
                                        </StackPanel>
                                    </Border>
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                        </GroupStyle>
                    </ListBox.GroupStyle>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}" FontSize="13" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Border>

            <!-- Recents -->
            <Border Grid.Column="1" Style="{StaticResource Card}" Margin="12,0,0,0">
                <DockPanel LastChildFill="True">
                    <Border DockPanel.Dock="Top" Background="#F8FAFC" Padding="10,8" CornerRadius="6,6,0,0">
                        <TextBlock Text="אחרונים" FontWeight="SemiBold" Foreground="#334155" />
                    </Border>
                    <ListBox ItemsSource="{Binding RecentEntities}"
                             SelectedItem="{Binding SelectedTarget}"
                             BorderThickness="0"
                             Background="Transparent"
                             ItemContainerStyle="{StaticResource ResultItem}"
                             Padding="4">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding Name}" FontSize="13" />
                                    <StackPanel Orientation="Horizontal" Margin="0,2,0,0">
                                        <Border Background="{Binding EntityType, Converter={StaticResource TypeBrush}}"
                                                CornerRadius="9"
                                                Padding="6,1">
                                            <TextBlock Text="{Binding EntityType, Converter={StaticResource HebrewType}}"
                                                       Foreground="White" FontSize="10" />
                                        </Border>
                                    </StackPanel>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </DockPanel>
            </Border>
        </Grid>

        <!-- Status row -->
        <DockPanel Grid.Row="2" Margin="0,12,0,0" LastChildFill="True">
            <Button DockPanel.Dock="Left"
                    Command="{Binding CreateContactFromSenderCommand}"
                    Content="+ צור איש קשר חדש מהשולח"
                    Style="{StaticResource LinkButton}"
                    FontSize="12" />
            <TextBlock Text="{Binding StatusMessage}"
                       VerticalAlignment="Center"
                       Margin="0,0,8,0"
                       Foreground="{StaticResource Muted}"
                       FontSize="12"
                       TextTrimming="CharacterEllipsis" />
        </DockPanel>

        <!-- Action row -->
        <StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,12,0,0">
            <Button Command="{Binding SubmitCommand}"
                    IsDefault="True"
                    Content="תייק"
                    Style="{StaticResource PrimaryButton}"
                    Margin="0,0,8,0" />
            <Button Command="{Binding CancelCommand}"
                    IsCancel="True"
                    Content="ביטול"
                    Style="{StaticResource SecondaryButton}" />
        </StackPanel>
    </Grid>
</Window>
