<% if @newly_created && @plain_key %>
  <%= content_for :page_title, "API Key Created Successfully" %>

  <div class="bg-container rounded-xl shadow-border-xs p-4">
    <div class="space-y-4">
      <div class="p-3 shadow-border-xs bg-container rounded-lg">
        <div class="flex items-start gap-3">
          <%= render DS::FilledIcon.new(
            icon: "check-circle",
            rounded: true,
            size: "lg",
            variant: :success
          ) %>
          <div class="flex-1">
            <h3 class="font-medium text-primary">API Key Created Successfully!</h3>
            <p class="text-secondary text-sm mt-1">Your new API key "<%= @current_api_key.name %>" has been created and is ready to use.</p>
          </div>
        </div>
      </div>

      <div class="bg-surface-inset rounded-xl p-4">
        <h4 class="font-medium text-primary mb-3">Your API Key</h4>
        <p class="text-secondary text-sm mb-3">Copy and store this key securely. You'll need it to authenticate your API requests.</p>

        <div class="bg-container rounded-lg p-3 border border-primary" data-controller="clipboard">
          <div class="flex items-center justify-between gap-3">
            <code id="api-key-display" class="font-mono text-sm text-primary break-all" data-clipboard-target="source"><%= @current_api_key.plain_key %></code>
            <%= render DS::Button.new(
              text: "Copy API Key",
              variant: "ghost",
              icon: "copy",
              data: { action: "clipboard#copy" }
            ) %>
          </div>
        </div>
      </div>

      <div class="bg-surface-inset rounded-xl p-4">
        <h4 class="font-medium text-primary mb-3">How to use your API key</h4>
        <p class="text-secondary text-sm mb-3"><%= t(".current_api_key.usage_instructions", product_name: product_name) %></p>
        <div class="bg-container rounded-lg p-3 font-mono text-sm text-primary border border-primary">
          curl -H "X-Api-Key: <%= @current_api_key.plain_key %>" <%= request.base_url %>/api/v1/accounts
        </div>
      </div>

      <div class="flex justify-end pt-4 border-t border-primary">
        <%= render DS::Link.new(
          text: "Continue to API Key Settings",
          href: settings_api_key_path,
          variant: "primary"
        ) %>
      </div>
    </div>
  </div>
<% elsif @current_api_key %>
  <%= content_for :page_title, "Your API Key" %>
  <%= content_for :page_actions do %>
    <%= render DS::Link.new(
      text: "Create New Key",
      href: new_settings_api_key_path(regenerate: true),
      variant: "secondary"
    ) %>
  <% end %>

  <div class="bg-container rounded-xl shadow-border-xs p-4">
    <div class="space-y-4">
      <div class="p-3 shadow-border-xs bg-container rounded-lg flex justify-between items-center">
        <div class="flex items-center gap-3">
          <%= render DS::FilledIcon.new(
            icon: "key",
            rounded: true,
            size: "lg"
          ) %>

          <div class="text-sm space-y-1">
            <p class="text-primary font-medium"><%= @current_api_key.name %></p>
            <p class="text-secondary">
              Created <%= time_ago_in_words(@current_api_key.created_at) %> ago
              <% if @current_api_key.last_used_at %>
                • Last used <%= time_ago_in_words(@current_api_key.last_used_at) %> ago
              <% else %>
                • Never used
              <% end %>
            </p>
          </div>
        </div>

        <div class="rounded-md bg-success px-2 py-1">
          <p class="text-success-foreground font-medium text-xs">Active</p>
        </div>
      </div>

      <div class="bg-surface-inset rounded-xl p-4">
        <h4 class="font-medium text-primary mb-3">Permissions</h4>
        <div class="flex flex-wrap gap-2">
          <% @current_api_key.scopes.each do |scope| %>
            <span class="inline-flex items-center gap-1 px-2 py-1 button-bg-primary text-inverse rounded-full text-xs font-medium">
              <%= icon("shield-check", class: "w-3 h-3") %>
              <%= case scope
                  when "read" then "Read Only"
                  when "read_write" then "Read/Write"
                  else scope.humanize
                  end %>
            </span>
          <% end %>
        </div>
      </div>

      <div class="bg-surface-inset rounded-xl p-4">
        <h4 class="font-medium text-primary mb-3">Your API Key</h4>
        <p class="text-secondary text-sm mb-3">Copy and store this key securely. You'll need it to authenticate your API requests.</p>

        <div class="bg-container rounded-lg p-3 border border-primary" data-controller="clipboard">
          <div class="flex items-center justify-between gap-3">
            <code id="api-key-display" class="font-mono text-sm text-primary break-all" data-clipboard-target="source"><%= @current_api_key.plain_key %></code>
            <%= render DS::Button.new(
              text: "Copy API Key",
              variant: "ghost",
              icon: "copy",
              data: { action: "clipboard#copy" }
            ) %>
          </div>
        </div>
      </div>

      <div class="bg-surface-inset rounded-xl p-4">
        <h4 class="font-medium text-primary mb-3">How to use your API key</h4>
        <p class="text-secondary text-sm mb-3"><%= t(".current_api_key.usage_instructions", product_name: product_name) %></p>
        <div class="bg-container rounded-lg p-3 font-mono text-sm text-primary border border-primary">
          curl -H "X-Api-Key: <%= @current_api_key.plain_key %>" <%= request.base_url %>/api/v1/accounts
        </div>
      </div>

      <div class="flex justify-end pt-4 border-t border-primary">
        <%= render DS::Button.new(
          text: "Revoke Key",
          href: settings_api_key_path,
          method: :delete,
          variant: "destructive",
          data: {
            turbo_confirm: "Are you sure you want to revoke this API key?"
          }
        ) %>
      </div>
    </div>
  </div>
<% else %>
  <%= content_for :page_title, t(".no_api_key.title") %>
  <%= content_for :page_actions do %>
    <%= render DS::Link.new(
      text: t(".no_api_key.create_api_key"),
      href: new_settings_api_key_path,
      variant: "primary"
    ) %>
  <% end %>

  <div class="bg-container rounded-xl shadow-border-xs p-4">
    <div class="space-y-4">
      <div class="p-3 shadow-border-xs bg-container rounded-lg">
        <div class="flex items-start gap-3">
          <%= render DS::FilledIcon.new(
            icon: "key",
            rounded: true,
            size: "lg"
          ) %>
          <div class="flex-1">
            <h3 class="font-medium text-primary"><%= t(".no_api_key.heading", product_name: product_name) %></h3>
            <p class="text-secondary text-sm mt-1"><%= t(".no_api_key.description") %></p>
          </div>
        </div>
      </div>

      <div class="bg-surface-inset rounded-xl p-4">
        <h4 class="font-medium text-primary mb-3"><%= t(".no_api_key.what_you_can_do") %></h4>
        <ul class="space-y-2 text-sm text-secondary">
          <li class="flex items-start gap-2">
            <%= icon("check", class: "w-4 h-4 text-primary mt-0.5") %>
            <span><%= t(".no_api_key.feature_1") %></span>
          </li>
          <li class="flex items-start gap-2">
            <%= icon("check", class: "w-4 h-4 text-primary mt-0.5") %>
            <span><%= t(".no_api_key.feature_2") %></span>
          </li>
          <li class="flex items-start gap-2">
            <%= icon("check", class: "w-4 h-4 text-primary mt-0.5") %>
            <span><%= t(".no_api_key.feature_3") %></span>
          </li>
        </ul>
      </div>

      <div class="bg-surface-inset rounded-xl p-4">
        <h4 class="font-medium text-primary mb-2"><%= t(".no_api_key.security_note_title") %></h4>
        <p class="text-secondary text-sm"><%= t(".no_api_key.security_note") %></p>
      </div>
    </div>
  </div>
<% end %>
