<% content_for :title, "DSN - #{@dsn.name}" %>

<div class="mx-auto max-w-7xl px-4 py-6 sm:px-6 lg:px-8">
  <div class="md:flex md:items-center md:justify-between mb-8">
    <div class="flex-1 min-w-0">
      <div class="flex items-center">
        <h2 class="text-2xl font-bold leading-7 text-gray-900 sm:text-3xl sm:truncate">
          <%= @dsn.name %>
        </h2>
        <span class="ml-3 inline-flex items-center px-3 py-0.5 rounded-full text-sm font-medium <%= @dsn.enabled? ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800' %>">
          <%= @dsn.enabled? ? 'Enabled' : 'Disabled' %>
        </span>
      </div>
    </div>
    <div class="mt-4 flex md:mt-0 md:ml-4 space-x-3">
      <%= link_to "Back to DSNs", dsns_path, class: "inline-flex items-center px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50" %>
      <% if policy(@dsn).edit? %>
        <%= link_to "Edit", edit_dsn_path(@dsn), class: "inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700" %>
      <% end %>
    </div>
  </div>

  <div class="bg-white shadow overflow-hidden sm:rounded-lg">
    <div class="px-4 py-5 sm:px-6">
      <h3 class="text-lg leading-6 font-medium text-gray-900">DSN Information</h3>
      <p class="mt-1 max-w-2xl text-sm text-gray-500">
        DSN key details and usage information.
      </p>
    </div>
    <div class="border-t border-gray-200">
      <dl>
        <div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
          <dt class="text-sm font-medium text-gray-500">Name</dt>
          <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2"><%= @dsn.name %></dd>
        </div>
        <div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
          <dt class="text-sm font-medium text-gray-500">DSN Key</dt>
          <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
            <div class="flex items-center space-x-2">
              <code class="bg-gray-100 px-2 py-1 rounded text-sm font-mono"><%= @dsn.key %></code>
              <button onclick="copyToClipboard('<%= @dsn.key %>')" class="text-blue-600 hover:text-blue-800 text-sm">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" />
                </svg>
                Copy
              </button>
            </div>
          </dd>
        </div>
        <div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
          <dt class="text-sm font-medium text-gray-500">Status</dt>
          <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium <%= @dsn.enabled? ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800' %>">
              <%= @dsn.enabled? ? 'Enabled' : 'Disabled' %>
            </span>
          </dd>
        </div>
        <div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
          <dt class="text-sm font-medium text-gray-500">Created</dt>
          <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
            <%= @dsn.created_at.strftime('%B %d, %Y at %I:%M %p') %>
          </dd>
        </div>
        <% if @dsn.updated_at != @dsn.created_at %>
          <div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
            <dt class="text-sm font-medium text-gray-500">Last Updated</dt>
            <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
              <%= @dsn.updated_at.strftime('%B %d, %Y at %I:%M %p') %>
            </dd>
          </div>
        <% end %>
      </dl>
    </div>
  </div>

  <!-- Actions -->
  <% if policy(@dsn).disable? || policy(@dsn).enable? %>
    <div class="mt-6 bg-white shadow sm:rounded-lg">
      <div class="px-4 py-5 sm:px-6">
        <h3 class="text-lg leading-6 font-medium text-gray-900">Actions</h3>
        <div class="mt-5 space-x-3">
          <% if @dsn.enabled? && policy(@dsn).disable? %>
            <%= link_to "Disable DSN", disable_dsn_path(@dsn), method: :post,
                data: { confirm: "Are you sure you want to disable this DSN? Agents will no longer be able to authenticate with this key." },
                class: "inline-flex items-center px-4 py-2 border border-red-300 rounded-md shadow-sm text-sm font-medium text-red-700 bg-white hover:bg-red-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500" %>
          <% elsif !@dsn.enabled? && policy(@dsn).enable? %>
            <%= link_to "Enable DSN", enable_dsn_path(@dsn), method: :post,
                class: "inline-flex items-center px-4 py-2 border border-green-300 rounded-md shadow-sm text-sm font-medium text-green-700 bg-white hover:bg-green-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500" %>
          <% end %>
        </div>
      </div>
    </div>
  <% end %>

  <!-- Usage Instructions -->
  <div class="mt-6 bg-white shadow sm:rounded-lg">
    <div class="px-4 py-5 sm:px-6">
      <h3 class="text-lg leading-6 font-medium text-gray-900">Usage Instructions</h3>
      <p class="mt-1 max-w-2xl text-sm text-gray-500">
        How to use this DSN key with your baffle-agents.
      </p>
    </div>
    <div class="border-t border-gray-200 px-4 py-5 sm:px-6">
      <div class="prose max-w-none">
        <h4>HTTP Header Authentication</h4>
        <p>Include the DSN key in the <code>Authorization</code> header:</p>
        <pre class="bg-gray-100 p-3 rounded text-sm"><code>Authorization: Bearer <%= @dsn.key %></code></pre>

        <h4 class="mt-4">Query Parameter Authentication</h4>
        <p>Include the DSN key as a query parameter:</p>
        <pre class="bg-gray-100 p-3 rounded text-sm"><code>/api/events?baffle_key=<%= @dsn.key %></code></pre>

        <h4 class="mt-4">X-Baffle-Auth Header</h4>
        <p>Use the custom Baffle authentication header:</p>
        <pre class="bg-gray-100 p-3 rounded text-sm"><code>X-Baffle-Auth: Baffle baffle_key=<%= @dsn.key %>, baffle_version=1</code></pre>
      </div>
    </div>
  </div>
</div>

<script>
function copyToClipboard(text) {
  navigator.clipboard.writeText(text).then(function() {
    // Show a brief confirmation
    const button = event.target.closest('button');
    const originalText = button.innerHTML;
    button.innerHTML = '<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg> Copied!';
    button.classList.add('text-green-600');
    button.classList.remove('text-blue-600');

    setTimeout(function() {
      button.innerHTML = originalText;
      button.classList.remove('text-green-600');
      button.classList.add('text-blue-600');
    }, 2000);
  });
}
</script>