<div class="max-w-2xl mx-auto">
  <div class="bg-white shadow-sm rounded-lg">
    <div class="px-6 py-4 border-b border-gray-200">
      <h1 class="text-2xl font-semibold text-gray-900">Import GeoLite2 Data</h1>
      <p class="mt-1 text-sm text-gray-600">
        Upload GeoLite2-ASN-CSV or GeoLite2-Country-CSV files to import network range data.
      </p>
    </div>

    <div class="px-6 py-4">
      <%= form_with(model: @data_import, local: true, class: "space-y-6") do |form| %>
        <% if @data_import.errors.any? %>
          <div class="rounded-md bg-red-50 p-4">
            <div class="flex">
              <div class="flex-shrink-0">
                <svg class="h-5 w-5 text-red-400" viewBox="0 0 20 20" fill="currentColor">
                  <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd" />
                </svg>
              </div>
              <div class="ml-3">
                <h3 class="text-sm font-medium text-red-800">
                  There were <%= pluralize(@data_import.errors.count, "error") %> with your submission:
                </h3>
                <div class="mt-2 text-sm text-red-700">
                  <ul class="list-disc list-inside space-y-1">
                    <% @data_import.errors.full_messages.each do |message| %>
                      <li><%= message %></li>
                    <% end %>
                  </ul>
                </div>
              </div>
            </div>
          </div>
        <% end %>

        <!-- File Upload Section -->
        <div class="space-y-2">
          <%= form.label :file, "Select File", class: "block text-sm font-medium text-gray-700" %>
          <div class="flex justify-center px-6 pt-5 pb-6 border-2 border-gray-300 border-dashed rounded-md hover:border-gray-400 transition-colors">
            <div class="space-y-1 text-center">
              <svg class="mx-auto h-12 w-12 text-gray-400" stroke="currentColor" fill="none" viewBox="0 0 48 48">
                <path d="M28 8H12a4 4 0 00-4 4v20m32-12v8m0 0v8a4 4 0 01-4 4H12a4 4 0 01-4-4v-4m32-4l-3.172-3.172a4 4 0 00-5.656 0L28 28M8 32l9.172-9.172a4 4 0 015.656 0L28 28m0 0l4 4m4-24h8m-4-4v8m-12 4h.02" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" />
              </svg>
              <div class="flex text-sm text-gray-600">
                <label for="data_import_file" class="relative cursor-pointer bg-white rounded-md font-medium text-blue-600 hover:text-blue-500 focus-within:outline-none focus-within:ring-2 focus-within:ring-offset-2 focus-within:ring-blue-500">
                  <span>Upload a file</span>
                  <%= form.file_field :file, id: "data_import_file", class: "sr-only", accept: ".csv,.zip", required: true %>
                </label>
                <p class="pl-1">or drag and drop</p>
              </div>
              <p class="text-xs text-gray-500">
                CSV or ZIP files up to 500MB
              </p>
            </div>
          </div>
        </div>

        <!-- File Type Detection -->
        <div class="bg-blue-50 border border-blue-200 rounded-md p-4">
          <div class="flex">
            <div class="flex-shrink-0">
              <svg class="h-5 w-5 text-blue-400" viewBox="0 0 20 20" fill="currentColor">
                <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd" />
              </svg>
            </div>
            <div class="ml-3">
              <h3 class="text-sm font-medium text-blue-800">Automatic Detection</h3>
              <div class="mt-2 text-sm text-blue-700">
                <p>The system will automatically detect whether your file contains ASN or Country data based on:</p>
                <ul class="mt-1 list-disc list-inside">
                  <li>Filename (containing "ASN" or "Country")</li>
                  <li>Column headers in the CSV file</li>
                </ul>
              </div>
            </div>
          </div>
        </div>

        <!-- Supported Formats -->
        <div class="bg-gray-50 border border-gray-200 rounded-md p-4">
          <h3 class="text-sm font-medium text-gray-800 mb-3">Supported File Formats</h3>

          <div class="space-y-3">
            <div>
              <h4 class="text-sm font-medium text-gray-700">GeoLite2-ASN-CSV</h4>
              <p class="text-xs text-gray-600">Contains network ranges with ASN and organization information</p>
              <p class="text-xs text-gray-500">Expected files: GeoLite2-ASN-Blocks-IPv4.csv, GeoLite2-ASN-Blocks-IPv6.csv</p>
            </div>

            <div>
              <h4 class="text-sm font-medium text-gray-700">GeoLite2-Country-CSV</h4>
              <p class="text-xs text-gray-600">Contains network ranges with country geolocation data</p>
              <p class="text-xs text-gray-500">Expected files: GeoLite2-Country-Blocks-IPv4.csv, GeoLite2-Country-Blocks-IPv6.csv, GeoLite2-Country-Locations-*.csv</p>
            </div>
          </div>
        </div>

        <!-- Submit Button -->
        <div class="pt-4">
          <%= form.submit "Start Import", class: "w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 cursor-pointer transition-colors" %>
        </div>
      <% end %>
    </div>
  </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
  const fileInput = document.getElementById('data_import_file');
  const dropZone = fileInput.closest('.border-dashed');

  // Handle drag and drop
  ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
    dropZone.addEventListener(eventName, preventDefaults, false);
  });

  function preventDefaults(e) {
    e.preventDefault();
    e.stopPropagation();
  }

  ['dragenter', 'dragover'].forEach(eventName => {
    dropZone.addEventListener(eventName, highlight, false);
  });

  ['dragleave', 'drop'].forEach(eventName => {
    dropZone.addEventListener(eventName, unhighlight, false);
  });

  function highlight(e) {
    dropZone.classList.add('border-blue-400', 'bg-blue-50');
  }

  function unhighlight(e) {
    dropZone.classList.remove('border-blue-400', 'bg-blue-50');
  }

  dropZone.addEventListener('drop', handleDrop, false);

  function handleDrop(e) {
    const dt = e.dataTransfer;
    const files = dt.files;

    if (files.length > 0) {
      fileInput.files = files;
      updateFileName(files[0].name);
    }
  }

  fileInput.addEventListener('change', function(e) {
    if (e.target.files.length > 0) {
      updateFileName(e.target.files[0].name);
    }
  });

  function updateFileName(fileName) {
    const textElement = dropZone.querySelector('p.text-xs');
    textElement.textContent = `Selected: ${fileName}`;
    textElement.classList.add('text-green-600', 'font-medium');
  }
});
</script>