@page "/chatroom_rag"

@inject ReAct_PME.WebUI.ServicesUI.ApiService ApiService
@using Services;
@using ReAct_PME.WebUI.ServicesUI;
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager Navigation
@inject HttpClient Http
@using Microsoft.AspNetCore.Components.Authorization;
@inject ReAct_PME.WebUI.ServicesUI.AuthService AuthService

<PageTitle>R.A.G.</PageTitle>

<h3>R.A.G.</h3>

<div class="container-fluid">

    <div class="container">
        <div class="form-group row">
            <label for="Question" class="col-sm-2 col-form-label">Domaine* : </label>
            <div class="col-sm-10">
                <select @bind="DomainSelected" class="form-select">
                    <option value="">-- Sélectionnez un statut --</option>
                    @if (DomainListe != null)
                    {
                        @foreach (var oneRef in DomainListe)
                        {
                            <option value="@oneRef">@oneRef</option>
                        }
                    }
                </select>
            </div>
        </div>
        <div class="form-group row">
            <label for="Question" class="col-sm-2 col-form-label">Question* : </label>
            <div class="col-sm-10">
                <textarea id="Question" @bind="Question" class="form-control" @oninput="AutoResizeQuestion" rows="3" />
            </div>
        </div>
        <div class="form-group row">
            <label for="EnvoiRequete" class="col-sm-2 col-form-label"></label>
            <div class="col-sm-10">
                <button class="btn-action btn-primary" @onclick="EnvoiRequete">
                    <i class="bi bi-arrow-clockwise"></i> Envoyer la question
                </button>
            </div>

        </div>
        <div class="form-group row">
            <label for="Reponse" class="col-sm-2 col-form-label">Reponse : </label>
            <div class="col-sm-10">
                <textarea id="Reponse" @bind="Reponse" class="form-control" @oninput="AutoResizeReponse" rows="5" disabled />
            </div>
        </div>
    </div>
</div>


@code {

    private string Question = "Que doit faire l'assistante à la réception d'une facture fournisseur ?";
    private string Reponse = string.Empty;
    public List<string>? DomainListe { get; set; } = new();
    public string DomainSelected { get; set; } = "";
    private Guid IdConversationSelected = Guid.Empty;

    #region Rendre les zones extensibles
    [Inject] IJSRuntime? JS { get; set; }
    private void AutoResizeQuestion(ChangeEventArgs e)
    {
        if (JS != null)
        {
            JS.InvokeVoidAsync("resizeTextarea", "Question");
        }
    }
    private void AutoResizeReponse(ChangeEventArgs e)
    {
        if (JS != null)
        {
            JS.InvokeVoidAsync("resizeTextarea", "Reponse");
        }
    }


    #endregion

    protected override async Task OnInitializedAsync()
    {
        DomainListe = await ApiService.GetAllAsync<List<string>?>("api/ChatRoom/rag/listeDomaines");
        if (DomainListe != null && DomainListe.Count > 0)
        {
            DomainSelected = DomainListe[0];
        }

    }

    private async Task EnvoiRequete()
    {
        try
        {
            Reponse = "";
            var rep = await ApiService.EnvoiRequete<string>(
                "api/ChatRoom/rag", DomainSelected, IdConversationSelected.ToString(), AuthService!.ID!,
                Question
            );
            Reponse = rep;
        }
        catch (Exception ex)
        {
            Reponse = ex.Message;
        }
    }

}
