{{!
  Copyright IBM Corp. 2016, 2025
  SPDX-License-Identifier: BUSL-1.1
}}

<div>
  <Page::Header>
    <:breadcrumbs>
      <Page::Breadcrumbs @breadcrumbs={{this.breadcrumbs}} />
    </:breadcrumbs>
  </Page::Header>

  <MessageError @errorMessage={{this.errorMessage}} />

  <form {{on "submit" (perform this.mountBackend)}} class="has-top-padding-m has-bottom-padding-s">
    {{! Plugin registration type (built-in vs external) }}
    <Hds::Form::RadioCard::Group @name="plugin-type" class="has-bottom-margin-m" as |RadioGroup|>
      <RadioGroup.Legend>Plugin registration type</RadioGroup.Legend>
      {{#each this.pluginTypeOptions as |option|}}
        <RadioGroup.RadioCard
          @checked={{eq this.pluginRegistrationType option.type}}
          {{on "change" (fn this.setPluginType option.type)}}
          @disabled={{option.disabled}}
          data-test-radio-card={{option.dataTestAttr}}
          as |Card|
        >
          <Card.Icon @name={{option.icon}} />
          <Card.Label>{{option.label}}</Card.Label>
          {{#if option.showBadge}}
            <Card.Badge @text="Enterprise" data-test-badge="external-enterprise" />
          {{/if}}
          <Card.Description>{{option.description}}</Card.Description>
          {{#if option.showAlert}}
            <Card.Generic>
              <Hds::Alert @type="compact" @color="neutral" class="has-top-padding-xs" data-test-inline-alert as |A|>
                <A.Description>No external plugins for this engine are currently registered in your plugin catalog.</A.Description>
              </Hds::Alert>
            </Card.Generic>
          {{/if}}
        </RadioGroup.RadioCard>
      {{/each}}
    </Hds::Form::RadioCard::Group>

    {{! Plugin version selection (only shows for external plugins) }}
    {{#if this.shouldShowPluginVersionField}}
      <div class="field" data-test-field="config.plugin_version">
        <Hds::Form::Select::Field
          @isRequired={{true}}
          name="plugin-version"
          data-test-select="plugin-version"
          @value={{this.selectedPluginVersion}}
          {{on "change" this.onPluginVersionChange}}
          as |F|
        >
          <F.Label>Plugin version</F.Label>
          <F.HelperText data-test-help-text="config.plugin_version">
            Specifies the semantic version of the plugin to use, e.g. "v1.0.0".
            {{#if @model.hasUnversionedPlugins}}
              Un-versioned plugins are not supported, they must be enabled via CLI.
            {{/if}}
            {{#if this.pinnedVersionForCurrentPlugin}}
              {{this.pinnedVersionForCurrentPlugin}}
              is pinned for this plugin.
            {{/if}}
          </F.HelperText>
          <F.Options>
            {{#each this.filteredVersionOptions as |version|}}
              <option value={{version}} data-test-version-option={{version}}>
                {{version}}
                {{#if (eq version this.pinnedVersionForCurrentPlugin)}}
                  (pinned)
                {{/if}}
              </option>
            {{/each}}
          </F.Options>
          {{#if (get this.formValidations "config.plugin_version.errors.length")}}
            <F.Error>
              {{#each (get this.formValidations "config.plugin_version.errors") as |error|}}
                {{error}}
              {{/each}}
            </F.Error>
          {{/if}}
        </Hds::Form::Select::Field>

        {{! Warning when selected version differs from pinned version }}
        {{#if this.shouldShowPinWarning}}
          <Hds::Alert @type="inline" @color="warning" class="has-top-margin-s" as |A|>
            <A.Title>Version differs from pinned</A.Title>
            <A.Description>
              You have selected
              {{this.selectedPluginVersion}}, but version
              {{this.pinnedVersionForCurrentPlugin}}
              is pinned for this plugin. Enabling the engine with this version will override the pinned version for this
              mount.
            </A.Description>
          </Hds::Alert>
        {{/if}}
      </div>
    {{/if}}

    <FormFieldGroups
      @model={{@model.form}}
      @groupName="formFieldGroups"
      @renderGroup="default"
      @modelValidations={{this.formValidations}}
      @onKeyUp={{this.onKeyUp}}
    />

    <FormFieldGroups @model={{@model.form}} @renderGroup="Method Options" @groupName="formFieldGroups">
      <:identityTokenKey>
        <SearchSelectWithModal
          @id="key"
          @fallbackComponent="input-search"
          @inputValue={{@model.form.data.config.identity_token_key}}
          @onChange={{this.handleIdentityTokenKeyChange}}
          @models={{array "oidc/key"}}
          @selectLimit="1"
          @modalFormTemplate="modal-form/oidc-key-template"
          @placeholder="Search for an existing OIDC key, or type a new key name to create it."
          @fallbackComponentPlaceholder="Input a key name"
          @modalSubtext="This key will be created in the OIDC key path."
          data-test-field="config.identity_token_key"
        />
      </:identityTokenKey>
    </FormFieldGroups>

    <div class="field is-grouped box is-fullwidth is-bottomless">
      <div class="control">
        <Hds::Button
          @text="Enable engine"
          @icon={{if this.mountBackend.isRunning "loading"}}
          type="submit"
          data-test-submit
          disabled={{this.mountBackend.isRunning}}
        />
      </div>
      <div class="control">
        <Hds::Button @text="Back" @color="secondary" {{on "click" this.goBack}} data-test-back-button />
      </div>
      {{#if this.invalidFormAlert}}
        <div class="control">
          <AlertInline @type="danger" class="has-top-padding-s" @message={{this.invalidFormAlert}} />
        </div>
      {{/if}}
    </div>
  </form>
</div>