<% content_for :title, "IP Address Lookup" %>

<div class="mx-auto max-w-7xl px-4 py-6 sm:px-6 lg:px-8">
  <div class="mb-8">
    <h1 class="text-3xl font-bold text-gray-900">IP Address Lookup</h1>
    <p class="mt-2 text-gray-600">Lookup IP addresses to find matching network ranges and intelligence</p>
  </div>

  <!-- Search Form -->
  <div class="bg-white shadow rounded-lg mb-6">
    <div class="p-6">
      <%= form_with url: lookup_network_ranges_path, method: :get, local: true, class: "space-y-4" do |form| %>
        <div>
          <%= form.label :ip_address, "IP Address", class: "block text-sm font-medium text-gray-700" %>
          <div class="mt-1 flex rounded-md shadow-sm">
            <%= form.text_field :ip, value: params[:ip], placeholder: "Enter IP address (e.g., 8.8.8.8)", class: "flex-1 min-w-0 block w-full rounded-none rounded-l-md border-gray-300 focus:border-blue-500 focus:ring-blue-500 sm:text-sm" %>
            <%= form.submit "Lookup", class: "-ml-px relative inline-flex items-center px-4 py-2 border border-transparent rounded-r-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700" %>
          </div>
        </div>
      <% end %>
    </div>
  </div>

  <% if params[:ip].present? && @ranges %>
    <!-- IP Intelligence -->
    <div class="bg-white shadow rounded-lg mb-6">
      <div class="px-6 py-4 border-b border-gray-200">
        <h3 class="text-lg font-medium text-gray-900">IP Intelligence: <%= params[:ip] %></h3>
      </div>
      <div class="p-6">
        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
          <div>
            <h4 class="text-sm font-medium text-gray-900 mb-3">Matching Network Ranges</h4>
            <% if @ranges.any? %>
              <div class="space-y-3">
                <% @ranges.each do |range| %>
                  <div class="border border-gray-200 rounded-lg p-4">
                    <div class="flex items-center justify-between mb-2">
                      <%= link_to range.cidr, network_range_path(range), class: "text-sm font-medium text-blue-600 hover:text-blue-900" %>
                      <span class="text-xs text-gray-500">Priority: <%= range[:specificity] %></span>
                    </div>

                    <% intel = range[:intelligence] %>
                    <div class="space-y-1 text-sm">
                      <% if intel[:company] %>
                        <div><span class="font-medium">Company:</span> <%= intel[:company] %></div>
                      <% end %>
                      <% if intel[:asn] %>
                        <div><span class="font-medium">ASN:</span> <%= intel[:asn] %> (<%= intel[:asn_org] %>)</div>
                      <% end %>
                      <% if intel[:country] %>
                        <div><span class="font-medium">Country:</span> <%= intel[:country] %></div>
                      <% end %>

                      <div class="flex space-x-2 pt-2">
                        <% if intel[:is_datacenter] %>
                          <span class="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-orange-100 text-orange-800">DC</span>
                        <% end %>
                        <% if intel[:is_vpn] %>
                          <span class="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-purple-100 text-purple-800">VPN</span>
                        <% end %>
                        <% if intel[:is_proxy] %>
                          <span class="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-red-100 text-red-800">Proxy</span>
                        <% end %>
                        <% if intel[:inherited] %>
                          <span class="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-gray-100 text-gray-800">Inherited</span>
                        <% end %>
                      </div>
                    </div>
                  </div>
                <% end %>
              </div>
            <% else %>
              <p class="text-sm text-gray-500">No network ranges found for this IP address.</p>
            <% end %>
          </div>

          <div>
            <h4 class="text-sm font-medium text-gray-900 mb-3">Suggested Block Ranges</h4>
            <% if @suggested_blocks.any? %>
              <div class="space-y-2">
                <% @suggested_blocks.each do |suggestion| %>
                  <div class="flex items-center justify-between p-3 border border-gray-200 rounded">
                    <div>
                      <div class="text-sm font-medium"><%= suggestion[:cidr] %></div>
                      <div class="text-xs text-gray-500"><%= suggestion[:description] %></div>
                    </div>
                    <% if suggestion[:current_block] %>
                      <span class="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-red-100 text-red-800">Already Blocked</span>
                    <% else %>
                      <%= link_to "Block Range", new_rule_path(cidr: suggestion[:cidr]), class: "inline-flex items-center px-3 py-1 border border-transparent text-xs font-medium rounded text-white bg-red-600 hover:bg-red-700" %>
                    <% end %>
                  </div>
                <% end %>
              </div>
            <% else %>
              <p class="text-sm text-gray-500">No suggestions available.</p>
            <% end %>
          </div>
        </div>
      </div>
    </div>
  <% end %>

  <!-- Quick Actions -->
  <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
    <div class="bg-white shadow rounded-lg p-6 text-center">
      <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 15l-2 5L9 9l11 4-5 2z" />
      </svg>
      <h3 class="mt-2 text-lg font-medium text-gray-900">Quick Lookup</h3>
      <p class="mt-1 text-sm text-gray-500">Enter any IP address to instantly find matching network ranges</p>
    </div>

    <div class="bg-white shadow rounded-lg p-6 text-center">
      <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="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
      </svg>
      <h3 class="mt-2 text-lg font-medium text-gray-900">Network Intelligence</h3>
      <p class="mt-1 text-sm text-gray-500">View company, ASN, country, and classification data</p>
    </div>

    <div class="bg-white shadow rounded-lg p-6 text-center">
      <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="M18.364 18.364A9 9 0 005.636 5.636m12.728 0A9 9 0 005.636 5.636m12.728 0L5.636 5.636" />
      </svg>
      <h3 class="mt-2 text-lg font-medium text-gray-900">Create Rules</h3>
      <p class="mt-1 text-sm text-gray-500">Block ranges or create surgical exceptions instantly</p>
    </div>
  </div>
</div>