<% content_for :title, "DSNs" %>

<div class="mx-auto max-w-7xl px-4 py-6 sm:px-6 lg:px-8">
  <!-- Header -->
  <div class="mb-8">
    <div class="flex items-center justify-between">
      <div>
        <h1 class="text-3xl font-bold text-gray-900">DSN Management</h1>
        <p class="mt-2 text-gray-600">Manage DSN keys for agent authentication</p>
      </div>
      <% if policy(Dsn).create? %>
        <div class="flex space-x-3">
          <%= link_to "New DSN", new_dsn_path, class: "inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700" %>
        </div>
      <% end %>
    </div>
  </div>

  
  <!-- Database DSNs -->
  <div class="bg-white shadow overflow-hidden sm:rounded-md">
    <% if @dsns.any? %>
      <ul role="list" class="divide-y divide-gray-200">
        <% @dsns.each do |dsn| %>
          <li>
            <div class="px-4 py-4 flex items-center justify-between">
              <div class="flex items-center">
                <div class="flex-shrink-0">
                  <% if dsn.enabled? %>
                    <div class="w-8 h-8 bg-green-100 rounded-full flex items-center justify-center">
                      <svg class="w-5 h-5 text-green-600" fill="currentColor" viewBox="0 0 20 20">
                        <path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd" />
                      </svg>
                    </div>
                  <% else %>
                    <div class="w-8 h-8 bg-red-100 rounded-full flex items-center justify-center">
                      <svg class="w-5 h-5 text-red-600" fill="currentColor" viewBox="0 0 20 20">
                        <path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd" />
                      </svg>
                    </div>
                  <% end %>
                </div>
                <div class="ml-4">
                  <div class="flex items-center">
                    <p class="text-sm font-medium text-gray-900"><%= dsn.name %></p>
                    <span class="ml-2 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>
                  </div>
                  <p class="text-sm text-gray-500 font-mono">
                    Key: <%= dsn.key[0..15] + "..." %>
                  </p>
                  <p class="text-xs text-gray-400">
                    Created: <%= dsn.created_at.strftime('%Y-%m-%d %H:%M') %>
                  </p>
                </div>
              </div>
              <div class="flex items-center space-x-2">
                <%= link_to "View", dsn, class: "text-blue-600 hover:text-blue-900 text-sm font-medium" %>
                <% if policy(dsn).edit? %>
                  <%= link_to "Edit", edit_dsn_path(dsn), class: "text-indigo-600 hover:text-indigo-900 text-sm font-medium" %>
                <% end %>
                <% if policy(dsn).disable? && dsn.enabled? %>
                  <%= link_to "Disable", disable_dsn_path(dsn), method: :post,
                      data: { confirm: "Are you sure you want to disable this DSN?" },
                      class: "text-red-600 hover:text-red-900 text-sm font-medium" %>
                <% elsif policy(dsn).enable? && !dsn.enabled? %>
                  <%= link_to "Enable", enable_dsn_path(dsn), method: :post,
                      class: "text-green-600 hover:text-green-900 text-sm font-medium" %>
                <% end %>
                <% if policy(dsn).destroy? && !dsn.enabled? %>
                  <%= button_to "Delete", dsn, method: :delete,
                      data: {
                        confirm: "Are you sure you want to delete '#{dsn.name}'? This action cannot be undone."
                      },
                      class: "text-red-700 hover:text-red-900 text-sm font-medium font-semibold bg-transparent border-none cursor-pointer p-0" %>
                <% end %>
              </div>
            </div>
          </li>
        <% end %>
      </ul>
    <% else %>
      <div class="text-center py-12">
        <svg class="mx-auto h-12 w-12 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z" />
        </svg>
        <h3 class="mt-2 text-sm font-medium text-gray-900">No DSNs</h3>
        <p class="mt-1 text-sm text-gray-500">Get started by creating a new DSN.</p>
        <% if policy(Dsn).create? %>
          <div class="mt-6">
            <%= link_to "New DSN", new_dsn_path, class: "inline-flex items-center px-4 py-2 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700" %>
          </div>
        <% end %>
      </div>
    <% end %>
  </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>