
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using System.Xml.Linq;


namespace MaUI;


public class ChatRoom_VM : BindableObject
{
    // === PROPRIÉTÉS ===
    private string _newMessage = "";
    public string NewMessage
    {
        get => _newMessage;
        set { _newMessage = value; OnPropertyChanged(); }
    }

    public ObservableCollection<Conversation> Conversations { get; } = new();
    public ObservableCollection<ChatMessage> CurrentMessages { get; } = new();

    private Conversation? _selectedConversation;
    public Conversation? SelectedConversation
    {
        get => _selectedConversation;
        set { _selectedConversation = value; OnPropertyChanged(); }
    }

    public ObservableCollection<string> SelectedDocumentsListe { get; } = new();

    public List<ModelInfo> InstalledModels { get; } = new()
    {
        new ModelInfo { Name = "gemma3:12b" },
        new ModelInfo { Name = "llama3.1:8b" }
    };

    private ModelInfo? _selectedModel;
    public ModelInfo? SelectedModel
    {
        get => _selectedModel;
        set { _selectedModel = value; OnPropertyChanged(); }
    }

    public bool IsApiExterne { get; set; }
    public bool IsRAG { get; set; }
    public bool IsGenerateImg { get; set; }
    public bool IsWithAssistant { get; set; }
    public bool IsCODER { get; set; }

    // === COMMANDES ===
    public ICommand DeleteConversationCommand { get; }
    public ICommand NewConversationCommand { get; }
    public ICommand AddDocumentsCommand { get; }
    public ICommand ClearDocumentsCommand { get; }
    public ICommand SendMessageCommand { get; }

    private readonly ApiService _apiService;

    // === CONSTRUCTEUR ===
    public ChatRoom_VM()
    {
        DeleteConversationCommand = new Command(DeleteConversation);
        NewConversationCommand = new Command(NewConversation);
        AddDocumentsCommand = new Command(AddDocuments);
        ClearDocumentsCommand = new Command(ClearDocuments);
        SendMessageCommand = new Command(async () => await SendMessageAsync());

        _apiService = new ApiService();
    }

    // === MÉTHODES ===
    private async void DeleteConversation()
    {
        if (SelectedConversation != null)
        {
            Conversations.Remove(SelectedConversation);
            SelectedConversation = null;
            CurrentMessages.Clear();
            await Application.Current.MainPage.DisplayAlert("Info", "Conversation supprimée", "OK");
        }
    }

    private void NewConversation()
    {
        var conv = new Conversation { Title = $"Conversation {Conversations.Count + 1}" };
        Conversations.Add(conv);
        SelectedConversation = conv;
        CurrentMessages.Clear();
    }

    private async void AddDocuments()
    {
        // Ici tu pourras brancher le FilePicker MAUI
        var result = await FilePicker.Default.PickAsync();
        if (result != null)
        {
            SelectedDocumentsListe.Add(result.FullPath);
        }
    }

    private void ClearDocuments()
    {
        SelectedDocumentsListe.Clear();
    }

    private async Task SendMessageAsync()
    {
        if (string.IsNullOrWhiteSpace(NewMessage))
            return;

        var userMsg = new ChatMessage { Role = "user", Content = NewMessage };
        CurrentMessages.Add(userMsg);
        NewMessage = "";

        try
        {
            // Appel API (à brancher avec ton ApiService)
            var dto = new ChatRequest { Requete = userMsg.Content };
            var response = await _apiService.EnvoiRequete<string>(
                "api/ChatRoom/llm",
                userMsg.Content
            );

            if (response != null)
            {
                var assistantMsg = new ChatMessage { Role = "assistant", Content = response };
                CurrentMessages.Add(assistantMsg);
            }
        }
        catch (Exception ex)
        {
            await Application.Current.MainPage.DisplayAlert("Erreur", ex.Message, "OK");
        }
    }
}

// === CLASSES D’APPUIS ===
public class Conversation
{
    public string Title { get; set; } = "";
}

public class ChatMessage
{
    public string Role { get; set; } = "";
    public string Content { get; set; } = "";
}

public class ModelInfo
{
    public string Name { get; set; } = "";
}

public class ChatRequest
{
    public string Requete { get; set; } = "";
}
