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

{{#if (has-feature "Namespaces")}}
  {{#if this.showPageHeader}}
    <Page::Header @title="Namespaces">
      <:breadcrumbs>
        <Page::Breadcrumbs
          @breadcrumbs={{array (hash label="Vault" route="vault.cluster.dashboard" icon="vault") (hash label="Namespaces")}}
        />
      </:breadcrumbs>
      <:badges>
        <Hds::Badge @icon="org" @text={{this.namespacePath}} data-test-badge="namespace-path" />
      </:badges>
      <:description>
        Create logically separated, multi-tenant environments so teams can manage secrets, policies, and authentication
        methods independently.
        <Hds::Link::Inline @isHrefExternal={{true}} @href={{doc-link "/vault/gui/namespaces"}}>
          Learn more
        </Hds::Link::Inline>
      </:description>
      <:actions>
        {{#if this.showIntroButton}}
          <Hds::Button
            @color="secondary"
            @icon="bulb"
            @text="New to Namespaces?"
            {{on "click" this.showIntroPage}}
            data-test-button="guided-start"
          />
        {{/if}}
        {{#unless this.hasNamespaces}}
          <Hds::Button
            @color={{if this.showContent "primary" "secondary"}}
            @icon="plus"
            @route="vault.cluster.access.namespaces.create"
            @text="Create namespace"
            data-test-button="create-namespace"
          />
        {{/unless}}
      </:actions>
    </Page::Header>
  {{/if}}

  {{#if this.showWizard}}
    <Wizard::Namespaces::NamespaceWizard
      @isIntroModal={{this.shouldRenderIntroModal}}
      @onFlexiblePolicyComplete={{fn (mut this.showSetupAlert) true}}
      @onRefresh={{this.refreshNamespaceList}}
    />
  {{/if}}

  {{#if this.showContent}}
    {{! Show namespace list }}
    {{#if this.hasNamespaces}}
      <Toolbar class="top-margin-16 has-bottom-margin-s">
        <ToolbarFilters>
          <FilterInputExplicit
            @query={{@model.pageFilter}}
            @placeholder="Search"
            @handleSearch={{this.handleSearch}}
            @handleInput={{this.handleInput}}
            @handleKeyDown={{this.handleKeyDown}}
          />
        </ToolbarFilters>
        <ToolbarActions>
          <Hds::Button
            class="has-right-margin-4"
            @color="secondary"
            @icon="reload"
            @iconPosition="trailing"
            @text="Refresh list"
            {{on "click" this.refreshNamespaceList}}
            data-test-button="refresh-namespace-list"
          />
          <ToolbarLink @route="vault.cluster.access.namespaces.create" @type="add" data-test-link-to="create-namespace">
            Create namespace
          </ToolbarLink>
        </ToolbarActions>
      </Toolbar>

      {{#if @model.namespaces.length}}
        <ListTable @columns={{this.tableColumns}} @data={{this.namespaceIds}}>
          <:popupMenu as |rowData|>
            <Hds::Dropdown @isInline={{true}} @listPosition="bottom-right" as |dd|>
              <dd.ToggleIcon
                @icon="more-horizontal"
                @text="More options"
                @hasChevron={{false}}
                data-test-popup-menu-trigger
              />
              {{#let (concat this.namespace.path (if this.namespace.path "/") rowData.id) as |targetNamespace|}}
                {{#if (includes targetNamespace this.namespace.accessibleNamespaces)}}
                  <dd.Interactive
                    {{on "click" (fn this.switchNamespace targetNamespace)}}
                    data-test-popup-menu="switch"
                  >Switch to namespace</dd.Interactive>
                {{/if}}
              {{/let}}
              <dd.Interactive
                @color="critical"
                {{on "click" (fn (mut this.nsToDelete) rowData.id)}}
                data-test-popup-menu="delete"
              >Delete</dd.Interactive>
            </Hds::Dropdown>
            {{#if (eq this.nsToDelete rowData.id)}}
              <ConfirmModal
                @color="critical"
                @onClose={{fn (mut this.nsToDelete) null}}
                @onConfirm={{fn this.deleteNamespace rowData.id}}
                @confirmTitle="Delete this namespace?"
                @confirmMessage="Any engines or mounts in this namespace will also be removed."
              />
            {{/if}}
          </:popupMenu>
        </ListTable>
      {{else}}
        <Hds::ApplicationState class="top-padding-32 is-marginless" as |A|>
          <A.Header @title="No Namespaces found" @titleTag="h2" data-test-empty-state-title />
        </Hds::ApplicationState>
      {{/if}}
    {{else}}
      {{! Show empty state }}
      {{#if this.showSetupAlert}}
        <Hds::Alert @type="inline" class="top-margin-32" as |A|>
          <A.Title>Your current setup is 1 namespace.</A.Title>
          <A.Description>Based on your answer about your security policy in the Guided start, no new namespaces are required.</A.Description>
        </Hds::Alert>
      {{/if}}

      <Hds::ApplicationState class="top-padding-32 is-marginless" as |A|>
        <A.Header @title="No namespaces yet" @titleTag="h2" @icon="skip" data-test-empty-state-title />
        <A.Body @text="Your namespaces will be listed here. Add a namespace to get started." />
        <A.Footer as |F|>
          <F.Button
            @color="secondary"
            @icon="reload"
            @text="Refresh"
            {{on "click" this.refreshNamespaceList}}
            data-test-button="refresh-namespace-list"
          />
          <F.LinkStandalone
            @icon="learn-link"
            @text="Secure multi-tenancy with namespaces tutorial"
            @href={{doc-link "/vault/tutorials/enterprise/namespaces"}}
          />
        </A.Footer>
      </Hds::ApplicationState>
    {{/if}}

  {{/if}}

{{else}}
  <UpgradePage @title="Namespaces" @minimumEdition="Vault Enterprise Pro" />
{{/if}}