<app-configuration-form
  [form]="form"
  [submitDisabled]="form.invalid"
  (cancel)="cancel.emit()"
  (triggerSubmit)="submit()">
  <ng-container [formGroup]="configForm">
    <app-driver-select
      [label]="schema?.properties?.['sources']?.title || ''"
      [required]="true"
      [form]="configForm"
      controlName="sources"
      provider="nucliadb"
      [multiselect]="true"></app-driver-select>

    <app-model-select
      [label]="schema?.properties?.['generative_model']?.title || ''"
      [form]="configForm"
      [required]="true"
      controlName="generative_model"></app-model-select>

    <app-model-select
      [label]="schema?.properties?.['rephrase_model']?.title || ''"
      [form]="configForm"
      [required]="true"
      controlName="rephrase_model"></app-model-select>

    <app-model-select
      [label]="schema?.properties?.['context_validation_model']?.title || ''"
      [form]="configForm"
      [required]="true"
      controlName="context_validation_model"></app-model-select>

    <pa-toggle
      formControlName="prune_context"
      withBackground>
      {{ schema?.properties?.['prune_context']?.title || '' }}
    </pa-toggle>

    <app-rules-field
      [form]="configForm"
      [config]="config"></app-rules-field>

    <div class="form-entry">
      <label for="max_retries">{{ schema?.properties?.['max_retries']?.title || '' }}</label>
      <pa-input
        id="max_retries"
        formControlName="max_retries"
        type="number"
        [required]="false"
        externalLabel></pa-input>
    </div>

    <pa-toggle
      [(value)]="useExistingConfiguration"
      (valueChange)="updateValidators($event)"
      withBackground>
      {{ 'retrieval-agents.workflow.node-types.advanced_ask.form.use-existing-config' | translate }}
    </pa-toggle>

    @if (useExistingConfiguration) {
      @let options = (searchConfigurationOptions | async) || [];
      @if (options.length > 0) {
        <div class="form-entry">
          <label for="search_configuration">
            {{ schema?.properties?.['search_configuration']?.title || '' }}
          </label>
          <pa-select
            id="search_configuration"
            formControlName="search_configuration"
            [options]="options"></pa-select>
        </div>
      } @else {
        <nsi-info-card
          type="warning"
          icon="warning">
          {{
            (sources.length > 1
              ? 'retrieval-agents.workflow.node-types.advanced_ask.form.existing-config-warning'
              : 'retrieval-agents.workflow.node-types.advanced_ask.form.no-existing-configs'
            ) | translate
          }}
        </nsi-info-card>
      }
    }
  </ng-container>
  @if (!useExistingConfiguration) {
    <app-ask-configuration
      [config]="initialSearchConfig"
      [sources]="sources"
      (configChanged)="updateConfig($event)"></app-ask-configuration>
  }
</app-configuration-form>
