<% content_for :title, "Settings" %>

<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">Settings</h1>
        <p class="mt-2 text-gray-600">Manage system configuration and API keys</p>
      </div>
    </div>
  </div>

  <!-- Settings Form -->
  <div class="bg-white shadow sm:rounded-lg">
    <div class="px-4 py-5 sm:p-6">
      <h3 class="text-lg font-medium leading-6 text-gray-900 mb-4">API Configuration</h3>

      <!-- ipapi.is API Key -->
      <div class="mb-6">
        <%= form_with url: settings_path, method: :patch, class: "space-y-4" do |f| %>
          <%= hidden_field_tag :key, 'ipapi_key' %>

          <div>
            <label for="ipapi_key" class="block text-sm font-medium text-gray-700">
              ipapi.is API Key
            </label>
            <div class="mt-1 flex rounded-md shadow-sm">
              <%= text_field_tag :value,
                  @settings['ipapi_key']&.value || ENV['IPAPI_KEY'],
                  class: "flex-1 min-w-0 block w-full px-3 py-2 rounded-md border-gray-300 focus:ring-blue-500 focus:border-blue-500 sm:text-sm",
                  placeholder: "Enter your ipapi.is API key" %>
              <%= f.submit "Update", class: "ml-3 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 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500" %>
            </div>
            <p class="mt-2 text-sm text-gray-500">
              <% if @settings['ipapi_key']&.value.present? %>
                <span class="text-green-600">✓ Configured in database</span>
              <% elsif ENV['IPAPI_KEY'].present? %>
                <span class="text-yellow-600">Using environment variable (IPAPI_KEY)</span>
              <% else %>
                <span class="text-red-600">ipapi.is not active</span>
              <% end %>
            </p>
            <p class="mt-1 text-xs text-gray-400">
              Get your API key from <a href="https://ipapi.is/" target="_blank" class="text-blue-600 hover:text-blue-800">ipapi.is</a>
            </p>
          </div>
        <% end %>
      </div>
    </div>
  </div>

  <!-- Data Retention Settings -->
  <div class="mt-6 bg-white shadow sm:rounded-lg">
    <div class="px-4 py-5 sm:p-6">
      <h3 class="text-lg font-medium leading-6 text-gray-900 mb-4">Data Retention</h3>

      <div class="mb-6">
        <%= form_with url: settings_path, method: :patch, class: "space-y-4" do |f| %>
          <%= hidden_field_tag :key, 'event_retention_days' %>

          <div>
            <label for="event_retention_days" class="block text-sm font-medium text-gray-700">
              Event Retention Period (days)
            </label>
            <div class="mt-1 flex rounded-md shadow-sm">
              <%= number_field_tag :value,
                  @settings['event_retention_days']&.value || 90,
                  class: "flex-1 min-w-0 block w-full px-3 py-2 rounded-md border-gray-300 focus:ring-blue-500 focus:border-blue-500 sm:text-sm",
                  placeholder: "90",
                  min: 0 %>
              <%= f.submit "Update", class: "ml-3 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 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500" %>
            </div>
            <p class="mt-2 text-sm text-gray-500">
              Events older than this many days will be automatically deleted by the cleanup job (runs hourly).
              Set to 0 to disable automatic cleanup. Default: 90 days.
            </p>
            <p class="mt-1 text-xs text-gray-400">
              Current setting: <strong><%= Setting.event_retention_days %> days</strong>
            </p>
          </div>
        <% end %>
      </div>
    </div>
  </div>
</div>
