@if (vllmOnly && useCustomModel) {
  <nsi-info-card type="warning">
    {{ 'kb.ai-models.extraction.extract.processing.vllm_config.warning' | translate }}
  </nsi-info-card>
}
@if (isAiTable) {
  <nsi-info-card type="warning">
    <span [innerHTML]="'kb.ai-models.extraction.extract.processing.ai_tables.warning' | translate"></span>
  </nsi-info-card>
}
<div [formGroup]="configForm">
  <div class="title-s">{{ 'kb.ai-models.extraction.llm-configuration.generative-model' | translate }}</div>
  <pa-radio-group
    [formControl]="modelTypeControl"
    (valueChange)="onTypeChanges($event)">
    <div class="container-with-border">
      @let showPagehound = vllmOnly && hasPagehound;
      <pa-radio
        value="default"
        noEllipsis
        [popoverHelp]="showPagehound ? ('kb.ai-models.extraction.llm-configuration.pagehound-info' | translate) : ''"
        help="kb.ai-models.extraction.llm-configuration.default-model-description">
        @if (showPagehound) {
          {{ pagehoundModel }}
        } @else {
          {{ 'kb.ai-models.extraction.llm-configuration.default-model' | translate }}
        }
      </pa-radio>
    </div>
    <div class="container-with-border">
      <pa-radio
        value="custom"
        help="kb.ai-models.extraction.llm-configuration.custom-model-description">
        {{ 'kb.ai-models.extraction.llm-configuration.custom-model' | translate }}
      </pa-radio>
      @if (useCustomModel) {
        <div class="custom-llm">
          <ng-container formGroupName="llm">
            <stf-model-selector
              formControlName="generative_model"
              [providers]="visualProviders()"
              [learningConfigurations]="learningConfigurations()"></stf-model-selector>
          </ng-container>
          <div>
            <stf-reasoning-config
              [(config)]="reasoningConfig"
              (configChange)="onReasoningChange()"
              [disabled]="reasoningDisabled"></stf-reasoning-config>
          </div>
        </div>
      }
    </div>
  </pa-radio-group>

  <div class="options">
    <div class="title-s">{{ 'kb.ai-models.extraction.llm-configuration.options' | translate }}</div>
    @if (isAiTable) {
      <div>
        <pa-toggle
          labelOnRight
          formControlName="merge_pages">
          {{ 'kb.ai-models.extraction.extract.processing.ai_tables.merge-pages' | translate }}
        </pa-toggle>
      </div>
      @if (mergePages) {
        <pa-input
          type="number"
          formControlName="max_pages_to_merge"
          [help]="'kb.ai-models.extraction.extract.processing.ai_tables.max-merge-pages-help' | translate">
          {{ 'kb.ai-models.extraction.extract.processing.ai_tables.max-merge-pages' | translate }}
        </pa-input>
      }
    }
    <pa-toggle
      labelOnRight
      formControlName="customRules">
      {{ 'kb.ai-models.extraction.llm-configuration.rules.define-rules' | translate }}
    </pa-toggle>
    @if (useCustomRules) {
      @if (rulesDisabled) {
        <nsi-info-card type="warning">
          {{ 'kb.ai-models.extraction.llm-configuration.rules.disabled' | translate: { model: pagehoundModel } }}
        </nsi-info-card>
      } @else {
        <div
          class="rules"
          formArrayName="rules">
          @if (rulesDescription) {
            <div class="body-m">
              {{ rulesDescription | translate }}
            </div>
          }
          @for (rule of rules.controls; track rule) {
            <div class="rule">
              <nsi-expandable-textarea
                [formControl]="rule"
                [rows]="2"
                resizable
                modalTitle="kb.ai-models.extraction.llm-configuration.rules.rule"
                [placeholder]="rulesPlaceholder || '' | translate">
                {{ 'kb.ai-models.extraction.llm-configuration.rules.rule' | translate }}
              </nsi-expandable-textarea>
              @if (createMode) {
                <pa-button
                  size="small"
                  aspect="basic"
                  icon="trash"
                  kind="destructive"
                  (click)="removeRule($index)">
                  {{ 'generic.delete' | translate }}
                </pa-button>
              }
            </div>
          }
          @if (createMode) {
            <nsi-button-mini
              icon="plus"
              (click)="addRule()">
              {{ 'kb.ai-models.extraction.llm-configuration.rules.add-rule' | translate }}
            </nsi-button-mini>
          }
        </div>
      }
    }
  </div>
</div>
