<?xml version="1.0"
      encoding="utf-8"?>

<Page x:Class="Marechai.App.Presentation.Views.CompaniesPage"
      x:Name="PageRoot"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:utu="using:Uno.Toolkit.UI"
      NavigationCacheMode="Required"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid utu:SafeArea.Insets="VisibleBounds">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" /> <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <!-- Header -->
        <utu:NavigationBar Grid.Row="0"
                           Content="{Binding Path=Title}">
            <utu:NavigationBar.MainCommand>
                <AppBarButton Icon="Back"
                              Label="Back"
                              Command="{Binding GoBackCommand}"
                              AutomationProperties.Name="Go back" />
            </utu:NavigationBar.MainCommand>
        </utu:NavigationBar>

        <!-- Content -->
        <ScrollViewer Grid.Row="1">
            <StackPanel Padding="16"
                        Spacing="16">

                <!-- Company Count Display -->
                <StackPanel HorizontalAlignment="Center"
                            Spacing="8">
                    <TextBlock Text="{Binding CompanyCountText}"
                               TextAlignment="Center"
                               FontSize="18"
                               FontWeight="SemiBold"
                               Foreground="{ThemeResource SystemBaseMediumColor}" />
                    <TextBlock Text="{Binding CompanyCount}"
                               TextAlignment="Center"
                               FontSize="48"
                               FontWeight="Bold"
                               Foreground="{ThemeResource SystemAccentColor}" />
                </StackPanel>

                <!-- Search Box -->
                <AutoSuggestBox x:Name="CompaniesSearchBox"
                                HorizontalAlignment="Stretch"
                                PlaceholderText="Search companies..."
                                Text="{Binding SearchQuery, Mode=TwoWay}"
                                QuerySubmitted="OnSearchQuerySubmitted"
                                TextChanged="OnSearchTextChanged">
                    <AutoSuggestBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}" />
                        </DataTemplate>
                    </AutoSuggestBox.ItemTemplate>
                </AutoSuggestBox>

                <!-- Loading State -->
                <StackPanel Visibility="{Binding IsLoading}"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            Padding="32"
                            Spacing="16">
                    <ProgressRing IsActive="True"
                                  IsIndeterminate="True"
                                  Height="48"
                                  Width="48" />
                    <TextBlock Text="Loading..."
                               TextAlignment="Center"
                               FontSize="14" />
                </StackPanel>

                <!-- Error State -->
                <StackPanel Visibility="{Binding HasError}"
                            Padding="16"
                            Background="{ThemeResource SystemErrorBackgroundColor}"
                            CornerRadius="8"
                            Spacing="8">
                    <TextBlock Text="Error"
                               FontWeight="Bold"
                               Foreground="{ThemeResource SystemErrorTextForegroundColor}" />
                    <TextBlock Text="{Binding ErrorMessage}"
                               Foreground="{ThemeResource SystemErrorTextForegroundColor}"
                               TextWrapping="Wrap" />
                </StackPanel>

                <!-- Companies List -->
                <StackPanel Visibility="{Binding IsDataLoaded}"
                            Spacing="8">
                    <ItemsRepeater ItemsSource="{Binding CompaniesList}">
                        <ItemsRepeater.ItemTemplate>
                            <DataTemplate>
                                <Button HorizontalAlignment="Stretch"
                                        Command="{Binding DataContext.NavigateToCompanyCommand, ElementName=PageRoot}"
                                        CommandParameter="{Binding}">
                                    <Grid ColumnSpacing="12">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>

                                        <!-- Company Logo -->
                                        <Image Grid.Column="0"
                                               Width="48"
                                               Height="48"
                                               Stretch="Uniform"
                                               VerticalAlignment="Center"
                                               HorizontalAlignment="Left"
                                               Source="{Binding LogoImageSource}"
                                               Visibility="{Binding LogoImageSource, Converter={StaticResource ObjectToVisibilityConverter}}" />

                                        <!-- Company Details -->
                                        <StackPanel Grid.Column="1"
                                                   Spacing="4"
                                                   VerticalAlignment="Center">
                                            <TextBlock Text="{Binding Name}"
                                                       FontSize="16"
                                                       FontWeight="SemiBold" />
                                            <TextBlock Text="{Binding FoundationDateDisplay}"
                                                       FontSize="12"
                                                       Opacity="0.6" />
                                        </StackPanel>
                                    </Grid>
                                </Button>
                            </DataTemplate>
                        </ItemsRepeater.ItemTemplate>
                    </ItemsRepeater>
                </StackPanel>

            </StackPanel>
        </ScrollViewer>

    </Grid>

</Page>