<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:viewmodels="clr-namespace:MaUI"
             x:Class="MaUI.ChatRoomPage"
             BackgroundColor="#FFF0F0F0">

    <ContentPage.BindingContext>
        <viewmodels:ChatRoom_VM />
    </ContentPage.BindingContext>

    <Grid RowDefinitions="Auto,*">
        <!-- Barre supérieure -->
        <HorizontalStackLayout Padding="10" Spacing="20">
            <Button Text="🗑" WidthRequest="74" Command="{Binding DeleteConversationCommand}" />
            <Button Text="Nouvelle conversation" WidthRequest="138" Command="{Binding NewConversationCommand}" />
            <Picker ItemsSource="{Binding InstalledModels}"
                    ItemDisplayBinding="{Binding Name}"
                    SelectedItem="{Binding SelectedModel}" WidthRequest="300" />
        </HorizontalStackLayout>

        <!-- Contenu principal -->
        <Grid Row="1" ColumnDefinitions="200,*" Padding="5">

            <!-- Colonne gauche -->
            <VerticalStackLayout>
                <CollectionView ItemsSource="{Binding Conversations}" SelectionMode="Single"
                                SelectedItem="{Binding SelectedConversation}">
                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <Label Text="{Binding Title}" />
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>

                <Button Text="Ajout de documents" Command="{Binding AddDocumentsCommand}" />
                <CollectionView ItemsSource="{Binding SelectedDocumentsListe}" />
                <Button Text="🗑" Command="{Binding ClearDocumentsCommand}" />
            </VerticalStackLayout>

            <!-- Colonne droite : chat -->
            <Grid Grid.Column="1" RowDefinitions="*,Auto">
                <!-- Historique -->
                <ScrollView>
                    <VerticalStackLayout BindableLayout.ItemsSource="{Binding CurrentMessages}">
                        <BindableLayout.ItemTemplate>
                            <DataTemplate>
                                <Frame Padding="10" CornerRadius="10" Margin="5"
                                       BackgroundColor="{Binding Role, Converter={StaticResource RoleToColorConverter}}">
                                    <Label Text="{Binding Content}" LineBreakMode="WordWrap" />
                                </Frame>
                            </DataTemplate>
                        </BindableLayout.ItemTemplate>
                    </VerticalStackLayout>
                </ScrollView>

                <!-- Saisie -->
                <VerticalStackLayout Grid.Row="1" Padding="5">
                    <Editor Text="{Binding NewMessage}" AutoSize="TextChanges" HeightRequest="70" />
                    
                    <HorizontalStackLayout Spacing="10">
                        <CheckBox IsChecked="{Binding IsApiExterne}" />
                        <Label Text="API Externe" VerticalOptions="Center" />
                    </HorizontalStackLayout>

                    <HorizontalStackLayout Spacing="10">
                        <CheckBox IsChecked="{Binding IsRAG}" />
                        <Label Text="RAG ?" VerticalOptions="Center" />
                    </HorizontalStackLayout>
                    <HorizontalStackLayout Spacing="10">
                        <CheckBox IsChecked="{Binding IsGenerateImg}" />
                        <Label Text="Générer une image ?" VerticalOptions="Center" />
                    </HorizontalStackLayout>
                    <HorizontalStackLayout Spacing="10">
                        <CheckBox IsChecked="{Binding IsWithAssistant}" />
                        <Label Text="Assistant IA ?" VerticalOptions="Center" />
                    </HorizontalStackLayout>

                    <Button Text="Envoyer" Command="{Binding SendMessageCommand}" />
                </VerticalStackLayout>
            </Grid>
        </Grid>
    </Grid>
</ContentPage>
