@using AIStudio.Provider
@using AIStudio.Settings
@using AIStudio.Provider.SelfHosted
@inherits SettingsPanelBase

<ExpansionPanel HeaderIcon="@Icons.Material.Filled.Layers" HeaderText="@T("Configure Providers")">
    <MudText Typo="Typo.h4" Class="mb-3">
        @T("Configured Providers")
    </MudText>
    <MudJustifiedText Typo="Typo.body1" Class="mb-3">
        @T("What we call a provider is the combination of an LLM provider such as OpenAI and a model like GPT-4o. You can configure as many providers as you want. This way, you can use the appropriate model for each task. As an LLM provider, you can also choose local providers. However, to use this app, you must configure at least one provider.")
    </MudJustifiedText>
    <MudTable Items="@this.SettingsManager.ConfigurationData.Providers" Hover="@true" Class="border-dashed border rounded-lg">
        <ColGroup>
            <col style="width: 3em;"/>
            <col style="width: 12em;"/>
            <col style="width: 12em;"/>
            <col/>
            <col style="width: 34em;"/>
        </ColGroup>
        <HeaderContent>
            <MudTh>#</MudTh>
            <MudTh>@T("Instance Name")</MudTh>
            <MudTh>@T("Provider")</MudTh>
            <MudTh>@T("Model")</MudTh>
            <MudTh>@T("Actions")</MudTh>
        </HeaderContent>
        <RowTemplate>
            <MudTd>@context.Num</MudTd>
            <MudTd>@context.InstanceName</MudTd>
            <MudTd>@context.UsedLLMProvider.ToName()</MudTd>
            <MudTd>
                @if (context.UsedLLMProvider is not LLMProviders.SELF_HOSTED)
                {
                    @this.GetLLMProviderModelName(context)
                }
                else if (context.UsedLLMProvider is LLMProviders.SELF_HOSTED && context.Host is not Host.LLAMACPP)
                {
                    @this.GetLLMProviderModelName(context)
                }
                else
                {
                    @T("as selected by provider")
                }
            </MudTd>
            <MudTd>
                <MudStack Row="true" Class="mb-2 mt-2" Wrap="Wrap.Wrap">
                    <MudButton Variant="Variant.Filled" Color="Color.Info" StartIcon="@Icons.Material.Filled.OpenInBrowser" Href="@context.UsedLLMProvider.GetDashboardURL()" Target="_blank" Disabled="@(!context.UsedLLMProvider.HasDashboard())">
                        @T("Open Dashboard")
                    </MudButton>
                    <MudButton Variant="Variant.Filled" Color="Color.Info" StartIcon="@Icons.Material.Filled.Edit" OnClick="() => this.EditLLMProvider(context)">
                        @T("Edit")
                    </MudButton>
                    <MudButton Variant="Variant.Filled" Color="Color.Error" StartIcon="@Icons.Material.Filled.Delete" OnClick="() => this.DeleteLLMProvider(context)">
                        @T("Delete")
                    </MudButton>
                </MudStack>
            </MudTd>
        </RowTemplate>
    </MudTable>

    @if(this.SettingsManager.ConfigurationData.Providers.Count == 0)
    {
        <MudText Typo="Typo.h6" Class="mt-3">
            @T("No providers configured yet.")
        </MudText>
    }

    <MudButton Variant="Variant.Filled" Color="@Color.Primary" StartIcon="@Icons.Material.Filled.AddRoad" Class="mt-3 mb-6" OnClick="@this.AddLLMProvider">
        @T("Add Provider")
    </MudButton>
    
    <MudText Typo="Typo.h4" Class="mb-3">
        @T("LLM Provider Confidence")
    </MudText>
    <MudJustifiedText Class="mb-3">
        @T("Do you want to always be able to recognize how trustworthy your LLM providers are? This way, you keep control over which provider you send your data to. You have two options for this: Either you choose a common schema, or you configure the trust levels for each LLM provider yourself.")
    </MudJustifiedText>
    
    <ConfigurationOption OptionDescription="@T("Do you want to enforce an app-wide minimum confidence level?")" LabelOn="@T("Yes, enforce a minimum confidence level")" LabelOff="@T("No, do not enforce a minimum confidence level")" State="@(() => this.SettingsManager.ConfigurationData.LLMProviders.EnforceGlobalMinimumConfidence)" StateUpdate="@(updatedState => this.SettingsManager.ConfigurationData.LLMProviders.EnforceGlobalMinimumConfidence = updatedState)" OptionHelp="@T("When enabled, you can enforce a minimum confidence level for all LLM providers. This way, you can ensure that only trustworthy providers are used.")"/>
    @if(this.SettingsManager.ConfigurationData.LLMProviders.EnforceGlobalMinimumConfidence)
    {
        <ConfigurationMinConfidenceSelection RestrictToGlobalMinimumConfidence="@false" SelectedValue="@(() => this.SettingsManager.ConfigurationData.LLMProviders.GlobalMinimumConfidence)" SelectionUpdate="@(selectedValue => this.SettingsManager.ConfigurationData.LLMProviders.GlobalMinimumConfidence = selectedValue)"/>
    }
    
    <ConfigurationOption OptionDescription="@T("Show provider's confidence level?")" LabelOn="@T("Yes, show me the confidence level")" LabelOff="@T("No, please hide the confidence level")" State="@(() => this.SettingsManager.ConfigurationData.LLMProviders.ShowProviderConfidence)" StateUpdate="@(updatedState => this.SettingsManager.ConfigurationData.LLMProviders.ShowProviderConfidence = updatedState)" OptionHelp="@T("When enabled, we show you the confidence level for the selected provider in the app. This helps you assess where you are sending your data at any time. Example: are you currently working with sensitive data? Then choose a particularly trustworthy provider, etc.")"/>
    @if (this.SettingsManager.ConfigurationData.LLMProviders.ShowProviderConfidence)
    {
        <ConfigurationSelect OptionDescription="@T("Select a confidence scheme")" SelectedValue="@(() => this.SettingsManager.ConfigurationData.LLMProviders.ConfidenceScheme)" Data="@ConfigurationSelectDataFactory.GetConfidenceSchemesData()" SelectionUpdate="@(selectedValue => this.SettingsManager.ConfigurationData.LLMProviders.ConfidenceScheme = selectedValue)" OptionHelp="@T("Choose the scheme that best suits you and your life. Do you trust any western provider? Or only providers from the USA or exclusively European providers? Then choose the appropriate scheme. Alternatively, you can assign the confidence levels to each provider yourself.")"/>
        @if (this.SettingsManager.ConfigurationData.LLMProviders.ConfidenceScheme is ConfidenceSchemes.CUSTOM)
        {
            <MudTable Items="@(Enum.GetValues<LLMProviders>().Where(x => x is not LLMProviders.NONE))" Hover="@true" Class="border-dashed border rounded-lg">
                <ColGroup>
                    <col style="width: 12em;"/>
                    <col/>
                    <col style="width: 22em;"/>
                </ColGroup>
                <HeaderContent>
                    <MudTh>@T("LLM Provider")</MudTh>
                    <MudTh>@T("Description")</MudTh>
                    <MudTh>@T("Confidence Level")</MudTh>
                </HeaderContent>
                <RowTemplate>
                    <MudTd Style="vertical-align: top;">
                        @context.ToName()
                    </MudTd>
                    <MudTd>
                        <MudMarkdown Value="@context.GetConfidence(this.SettingsManager).Description"/>
                    </MudTd>
                    <MudTd Style="vertical-align: top;">
                        <MudMenu StartIcon="@Icons.Material.Filled.Security" EndIcon="@Icons.Material.Filled.KeyboardArrowDown" Label="@this.GetCurrentConfidenceLevelName(context)" Variant="Variant.Filled" Style="@this.SetCurrentConfidenceLevelColorStyle(context)">
                            @foreach (var confidenceLevel in Enum.GetValues<ConfidenceLevel>().OrderBy(n => n))
                            {
                                if(confidenceLevel is ConfidenceLevel.NONE or ConfidenceLevel.UNKNOWN)
                                    continue;

                                <MudMenuItem OnClick="@(async () => await this.ChangeCustomConfidenceLevel(context, confidenceLevel))">
                                    @confidenceLevel.GetName()
                                </MudMenuItem>
                            }
                        </MudMenu>
                    </MudTd>
                </RowTemplate>
            </MudTable>
        }
    }
</ExpansionPanel>