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

<Page x:Class="Marechai.App.Presentation.Views.UsersPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:controls="using:CommunityToolkit.WinUI.UI.Controls"
      x:Name="PageRoot"
      NavigationCacheMode="Required"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid RowSpacing="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" /> <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <!-- Header with Title -->
        <Grid Grid.Row="0"
              Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
              BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}"
              BorderThickness="0,0,0,1"
              Padding="16,12">

            <!-- Page Title -->
            <StackPanel VerticalAlignment="Center">
                <TextBlock Text="User Management"
                           FontSize="20"
                           FontWeight="SemiBold"
                           Foreground="{ThemeResource TextControlForeground}" />
                <TextBlock Text="Manage users, roles, and permissions"
                           FontSize="12"
                           Foreground="{ThemeResource SystemBaseMediumColor}"
                           Margin="0,4,0,0" />
            </StackPanel>
        </Grid>

        <!-- Main Content -->
        <Grid Grid.Row="1">

            <!-- Access Denied Message -->
            <StackPanel Visibility="{Binding IsUberadmin, Converter={StaticResource InvertBoolToVisibilityConverter}}"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Center"
                        Padding="24"
                        Spacing="16"
                        MaxWidth="400">
                <InfoBar IsOpen="True"
                         Severity="Warning"
                         Title="Access Denied"
                         Message="You must be an Uberadmin to access user management."
                         IsClosable="False" />
            </StackPanel>

            <!-- Content (only visible to Uberadmin) -->
            <Grid Visibility="{Binding IsUberadmin, Converter={StaticResource BoolToVisibilityConverter}}"
                  Canvas.ZIndex="0">

                <!-- Loading State -->
                <StackPanel Visibility="{Binding IsLoading, Converter={StaticResource BoolToVisibilityConverter}}"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            Padding="32"
                            Spacing="16">
                    <ProgressRing IsActive="True"
                                  IsIndeterminate="True"
                                  Height="64"
                                  Width="64"
                                  Foreground="{ThemeResource SystemAccentColor}" />
                    <TextBlock Text="Loading users..."
                               FontSize="14"
                               TextAlignment="Center"
                               Foreground="{ThemeResource SystemBaseMediumColor}" />
                </StackPanel>

                <!-- Error State -->
                <StackPanel Visibility="{Binding HasError, Converter={StaticResource BoolToVisibilityConverter}}"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            Padding="24"
                            Spacing="16"
                            MaxWidth="400"
                            Canvas.ZIndex="100">
                    <InfoBar IsOpen="True"
                             Severity="Error"
                             Title="Unable to Load Users"
                             Message="{Binding ErrorMessage}"
                             IsClosable="False" />
                    <Button Content="Retry"
                            Command="{Binding LoadUsersCommand}"
                            HorizontalAlignment="Center"
                            Style="{ThemeResource AccentButtonStyle}" />
                </StackPanel>

                <!-- Users DataGrid -->
                <Grid Visibility="{Binding IsDataLoaded, Converter={StaticResource BoolToVisibilityConverter}}"
                      Canvas.ZIndex="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" /> <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <!-- Command Bar -->
                    <CommandBar Grid.Row="0"
                                DefaultLabelPosition="Right">
                        <AppBarButton Icon="Add"
                                      Label="Add User"
                                      Command="{Binding OpenAddUserDialogCommand}" />
                        <AppBarButton Icon="Refresh"
                                      Label="Refresh"
                                      Command="{Binding LoadUsersCommand}" />
                    </CommandBar>

                    <!-- DataGrid -->
                    <controls:DataGrid Grid.Row="1"
                                       Margin="16"
                                       ItemsSource="{Binding Users}"
                                       SelectedItem="{Binding SelectedUser, Mode=TwoWay}"
                                       AutoGenerateColumns="False"
                                       GridLinesVisibility="None"
                                       HeadersVisibility="Column"
                                       AlternatingRowBackground="{ThemeResource CardBackgroundFillColorSecondaryBrush}"
                                       CanUserReorderColumns="True"
                                       CanUserResizeColumns="True"
                                       CanUserSortColumns="True"
                                       IsReadOnly="True"
                                       SelectionMode="Single"
                                       HorizontalScrollBarVisibility="Auto"
                                       VerticalScrollBarVisibility="Auto">

                        <controls:DataGrid.Columns>
                            <controls:DataGridTextColumn Header="Email"
                                                         Binding="{Binding Email}"
                                                         Width="2*" />

                            <controls:DataGridTextColumn Header="Username"
                                                         Binding="{Binding UserName}"
                                                         Width="*" />

                            <controls:DataGridTextColumn Header="Phone"
                                                         Binding="{Binding PhoneNumber}"
                                                         Width="*" />

                            <controls:DataGridTextColumn Header="Roles"
                                                         Width="2*"
                                                         Binding="{Binding Roles, Converter={StaticResource RolesListConverter}}" />

                            <controls:DataGridTemplateColumn Header="Actions"
                                                             Width="Auto">
                                <controls:DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal"
                                                    Spacing="4">
                                            <Button Content="Edit"
                                                    Command="{Binding DataContext.OpenEditUserDialogCommand, ElementName=PageRoot}"
                                                    CommandParameter="{Binding}"
                                                    Style="{ThemeResource TextBlockButtonStyle}"
                                                    Padding="8,4" />
                                            <Button Content="Password"
                                                    Command="{Binding DataContext.OpenChangePasswordDialogCommand, ElementName=PageRoot}"
                                                    CommandParameter="{Binding}"
                                                    Style="{ThemeResource TextBlockButtonStyle}"
                                                    Padding="8,4" />
                                            <Button Content="Roles"
                                                    Command="{Binding DataContext.OpenManageRolesDialogCommand, ElementName=PageRoot}"
                                                    CommandParameter="{Binding}"
                                                    Style="{ThemeResource TextBlockButtonStyle}"
                                                    Padding="8,4" />
                                            <Button Content="Delete"
                                                    Command="{Binding DataContext.DeleteUserCommand, ElementName=PageRoot}"
                                                    CommandParameter="{Binding}"
                                                    Foreground="{ThemeResource SystemErrorTextColor}"
                                                    Style="{ThemeResource TextBlockButtonStyle}"
                                                    Padding="8,4" />
                                        </StackPanel>
                                    </DataTemplate>
                                </controls:DataGridTemplateColumn.CellTemplate>
                            </controls:DataGridTemplateColumn>
                        </controls:DataGrid.Columns>
                    </controls:DataGrid>
                </Grid>

            </Grid>
        </Grid>
    </Grid>
</Page>