{% extends "/layouts/main.twig" %}
{% set active_menu = 'settings' %}

{% set xdata = 'settings' %}
{% block title p__('title', 'Chat configuration') %}

{% set adapters = [
  {
    name: 'OpenAI',
    tooltip: __('Missing OpenAI API key'),
    is_available: option.openai.api_secret_key is defined and option.openai.api_secret_key is not empty, 
    models: [
      {
        name: 'GPT·4o',
        value: 'gpt-4o',
      },
      {
        name: 'GPT·4o mini',
        value: 'gpt-4o-mini',
      },
      {
        name: 'GPT·4 Turbo',
        value: 'gpt-4-turbo',
      },
      {
        name: 'GPT·4',
        value: 'gpt-4',
      },
      {
        name: 'GPT·3.5 Turbo',
        value: 'gpt-3.5-turbo',
      },
    ]
  },
  {
    name: 'Anthropic / Claude',
    tooltip: __('Missing Anthropic API key'),
    is_available: option.anthropic.api_key is defined and option.anthropic.api_key is not empty,
    models: [
      {
        name: 'Anthropic / Claude 3.5 Sonnet',
        value: 'claude-3-5-sonnet-20240620',
      },
      {
        name: 'Anthropic / Claude 3 Haiku',
        value: 'claude-3-haiku-20240307',
      },
      {
        name: 'Anthropic / Claude 3 Sonnet',
        value: 'claude-3-sonnet-20240229',
      },
      {
        name: 'Anthropic / Claude 3 Opus',
        value: 'claude-3-opus-20240229',
      },
    ]
  },
  {
    name: 'Cohere',
    tooltip: __('Missing Cohere API key'),
    is_available: option.cohere.api_key is defined and option.cohere.api_key is not empty, 
    models: [
      {
        name: 'Command R+',
        value: 'command-r-plus',
      },
      {
        name: 'Command R',
        value: 'command-r',
      },
      {
        name: 'Command',
        value: 'command',
      },
      {
        name: 'Command Light',
        value: 'command-light',
      }
    ]
  },
] %}

{% block template %}
<div>
  {% include "snippets/back.twig" with {link: 'admin/settings/features', label: 'Features'} %}

  <h1 class="mt-4">{{ p__('heading', 'Chat') }}</h1>
</div>

<x-form>
  <form class="flex flex-col gap-8" @submit.prevent="submit" x-ref="form"
    x-data="{chat: {name: null, avatar: `{{ option.features.chat.avatar ?? '' }}`, is_enabled: null}}">
    <div class="flex flex-col gap-2">
      <section class="flex flex-col gap-6 box" data-density="comfortable">
        <h2>{{ p__('heading', 'Configuration') }}</h2>

        <div
          class="flex items-center justify-between p-3 rounded-lg bg-intermediate">
          {{ p__('label', 'Status') }}

          <label class="inline-flex items-center gap-2 cursor-pointer">
            <input type="checkbox" name="features[chat][is_enabled]"
              class="hidden peer"
              {{ option.features.chat.is_enabled is defined and option.features.chat.is_enabled ? 'checked' : '' }}
              x-model.fill="chat.is_enabled">

            <span
              class="h-6 w-10 rounded-3xl bg-line relative block peer-checked:bg-success transition-all after:h-5 after:w-5 after:top-0.5 after:absolute after:left-0 after:ml-0.5 after:transition-all after:rounded-full after:bg-white peer-checked:after:left-4"></span>

            <span class="text-content-dimmed peer-checked:hidden">
              {{ p__('input-value', 'Disabled') }}
            </span>

            <span class="hidden text-success peer-checked:inline">
              {{ p__('input-value', 'Enabled') }}
            </span>
          </label>
        </div>

        <div class="grid grid-cols-2 gap-6">
          <div>
            <label>{{ p__('label', 'Default chat bot name') }}</label>

            <input type="text" name="features[chat][name]" class="mt-2 input"
              autocomplete="off"
              placeholder="{{ __('Include a name for the default chat bot') }}"
              value="{{ option.features.chat.name ?? '' }}"
              x-model.fill="chat.name" />
          </div>

          <div>
            <label>{{ p__('label', 'Default model') }}</label>

            <select name="features[chat][default_model]" class="mt-2 input">
              {% for provider in adapters %}
              {% for model in provider.models %}
              <option value="{{ model.value }}"
                {{ option.features.chat.default_model is defined and option.features.chat.default_model == model.value ? 'selected' : '' }}>
                {{ model.name }}</option>
              {% endfor %}
              {% endfor %}
            </select>
          </div>
        </div>

        <div>
          <div class="flex items-center justify-between">
            <label
              for="brand.favicon">{{ p__('label', 'Default chat bot') }}</label>
          </div>

          <div class="relative p-6 mt-2 box" x-data="{file:null}">
            <div
              class="relative z-10 flex flex-col items-start justify-between gap-6 sm:flex-row">
              <div class="flex items-center gap-2">
                <div class="avatar">
                  <template x-if="file">
                    <img :src="URL.createObjectURL(file)" alt="Chat avatar">
                  </template>

                  <template x-if="!file && chat.avatar">
                    <img :src=" chat.avatar" alt="Chat avatar">
                  </template>

                  <template x-if="!file && !chat.avatar">
                    <i class="ti ti-sparkles"></i>
                  </template>
                </div>

                <div>
                  <div class="font-bold" x-text="chat.name || `{{ __("AI") }}`">
                  </div>
                  <div class="text-xs text-content-dimmed">
                    {{ __('Chat bot name') }}</div>
                </div>
              </div>

              <button type="button"
                class="w-full button button-sm button-dimmed sm:w-auto"
                @click="$refs.file.click()">{{ p__('button', 'Browse files') }}</button>
            </div>

            <input type="file" @change="file = $refs.file.files[0];"
              name="features[chat][avatar]" class="hidden" accept="image/*"
              x-ref="file">
          </div>
        </div>
      </section>

      <section class="flex flex-col gap-6 box" data-density="comfortable">
        <h2>{{ p__('heading', 'Capabilities') }}</h2>

        <div class="grid gap-6">
          <label
            class="flex items-center gap-4 cursor-pointer box hover:border-line">
            <div>
              <div class="flex items-center gap-2 text-sm">
                {{ __('File analysis') }}
              </div>

              <div class="font-normal text-content-dimmed">
                {{ __('Chat with PDF, Doc and other text based documents') }}
              </div>
            </div>

            <div class="ml-auto">
              <input type="checkbox" class="hidden peer"
                name="features[tools][embedding_search][is_enabled]"
                {{ option.features.tools.embedding_search.is_enabled is defined and option.features.tools.embedding_search.is_enabled ? 'checked' : '' }}>

              <span
                class="h-6 w-10 rounded-3xl bg-line relative block peer-checked:bg-success transition-all after:h-5 after:w-5 after:top-0.5 after:absolute after:left-0 after:ml-0.5 after:transition-all after:rounded-full after:bg-white peer-checked:after:left-4"></span>
            </div>
          </label>

          <label
            class="flex items-center gap-4 cursor-pointer box hover:border-line">
            <div>
              <div class="flex items-center gap-2 text-sm">
                {{ __('Google search') }}

                {% if option.serper.api_key is not defined or option.serper.api_key is empty %}
                <i class="text-base ti ti-alert-square-rounded-filled text-failure"
                  x-tooltip.raw="{{ __('Missing Serper API key') }}"></i>
                {% endif %}
              </div>

              <div class="font-normal text-content-dimmed">
                {{ __('Search with Serper API') }}
              </div>
            </div>

            <div class="ml-auto">
              <input type="checkbox" class="hidden peer"
                name="features[tools][google_search][is_enabled]"
                {{ option.features.tools.google_search.is_enabled is defined and option.features.tools.google_search.is_enabled ? 'checked' : '' }}>

              <span
                class="h-6 w-10 rounded-3xl bg-line relative block peer-checked:bg-success transition-all after:h-5 after:w-5 after:top-0.5 after:absolute after:left-0 after:ml-0.5 after:transition-all after:rounded-full after:bg-white peer-checked:after:left-4"></span>
            </div>
          </label>

          <label
            class="flex items-center gap-4 cursor-pointer box hover:border-line">
            <div>
              <div class="flex items-center gap-2 text-sm">
                {{ __('Web browsing') }}
              </div>

              <div class="font-normal text-content-dimmed">
                {{ __('Scrap web pages for information') }}
              </div>
            </div>

            <div class="ml-auto">
              <input type="checkbox" class="hidden peer"
                name="features[tools][web_scrap][is_enabled]"
                {{ option.features.tools.web_scrap.is_enabled is defined and option.features.tools.web_scrap.is_enabled ? 'checked' : '' }}>

              <span
                class="h-6 w-10 rounded-3xl bg-line relative block peer-checked:bg-success transition-all after:h-5 after:w-5 after:top-0.5 after:absolute after:left-0 after:ml-0.5 after:transition-all after:rounded-full after:bg-white peer-checked:after:left-4"></span>
            </div>
          </label>

          <label
            class="flex items-center gap-4 cursor-pointer box hover:border-line">
            <div>
              <div class="flex items-center gap-2 text-sm">
                {{ __('Image generation') }}
              </div>

              <div class="font-normal text-content-dimmed">
                {{ __('Generate images based on prompts') }}
              </div>
            </div>

            <div class="ml-auto">
              <input type="checkbox" class="hidden peer"
                name="features[tools][generate_image][is_enabled]"
                {{ option.features.tools.generate_image.is_enabled is defined and option.features.tools.generate_image.is_enabled ? 'checked' : '' }}>

              <span
                class="h-6 w-10 rounded-3xl bg-line relative block peer-checked:bg-success transition-all after:h-5 after:w-5 after:top-0.5 after:absolute after:left-0 after:ml-0.5 after:transition-all after:rounded-full after:bg-white peer-checked:after:left-4"></span>
            </div>
          </label>

          <label
            class="flex items-center gap-4 cursor-pointer box hover:border-line">
            <div>
              <div class="flex items-center gap-2 text-sm">
                {{ __('Cohere web search') }}

                {% if option.cohere.api_key is not defined or option.cohere.api_key is empty %}
                <i class="text-base ti ti-alert-square-rounded-filled text-failure"
                  x-tooltip.raw="{{ __('Missing Cohere API key') }}"></i>
                {% endif %}
              </div>

              <div class="font-normal text-content-dimmed">
                {{ __('Feature is available only for Cohere models.') }}
              </div>
            </div>

            <div class="ml-auto">
              <input type="checkbox" class="hidden peer"
                name="features[tools][cohere_web_search][is_enabled]"
                {{ option.features.tools.cohere_web_search.is_enabled is defined and option.features.tools.cohere_web_search.is_enabled ? 'checked' : '' }}>

              <span
                class="h-6 w-10 rounded-3xl bg-line relative block peer-checked:bg-success transition-all after:h-5 after:w-5 after:top-0.5 after:absolute after:left-0 after:ml-0.5 after:transition-all after:rounded-full after:bg-white peer-checked:after:left-4"></span>
            </div>
          </label>
        </div>
      </section>

      <section class="flex flex-col gap-6 box" data-density="comfortable">
        <h2>{{ p__('heading', 'Models') }}</h2>

        {% for provider in adapters %}
        {% if loop.index0 > 0 %}
        <hr class="col-span-2 my-2">
        {% endif %}

        <h3>{{ provider.name }}</h3>

        <div class="grid gap-6 md:grid-cols-2">
          {% for model in provider.models %}
          <label
            class="flex items-center gap-4 cursor-pointer box hover:border-line">
            <div>
              <div class="flex items-center gap-2 text-sm">
                {{ model.name }}

                {% if provider.is_available is empty %}
                <i class="text-base ti ti-alert-square-rounded-filled text-failure"
                  x-tooltip.raw="{{ provider.tooltip }}"></i>
                {% endif %}
              </div>

              <div class="font-normal text-content-dimmed">
                {{ provider.name }}
              </div>
            </div>

            <div class="ml-auto">
              <input type="checkbox" class="hidden peer"
                name="features[chat][models][]" value="{{ model.value }}"
                {{ option.features.chat.models is not defined or model.value in option.features.chat.models ? 'checked' : '' }}>

              <span
                class="h-6 w-10 rounded-3xl bg-line relative block peer-checked:bg-success transition-all after:h-5 after:w-5 after:top-0.5 after:absolute after:left-0 after:ml-0.5 after:transition-all after:rounded-full after:bg-white peer-checked:after:left-4"></span>
            </div>
          </label>
          {% endfor %}

        </div>
        {% endfor %}
      </section>
    </div>

    <div class="flex justify-end gap-4">
      <a href="admin/settings" class="button button-outline">
        {{ p__('button', 'Cancel') }}
      </a>

      <button class="button button-accent" type="submit"
        :processing="isProcessing">
        {% include "/snippets/spinner.twig" %}

        {{ p__('button', 'Save changes') }}
      </button>
    </div>
  </form>
</x-form>
{% endblock %}