<%
  # Parameters:
  # - configuration: Provider::Configurable::Configuration object
%>

<div class="space-y-4">
  <div>
    <% if configuration.provider_description.present? %>
      <div class="text-sm text-secondary mb-4 prose prose-sm">
        <%= markdown(configuration.provider_description).html_safe %>
      </div>
    <% end %>

    <% env_configured = configuration.fields.any? { |f| f.env_key && ENV[f.env_key].present? } %>
    <% if env_configured %>
      <p class="text-sm text-secondary">
        Configuration can be set via environment variables or overridden below.
      </p>
    <% end %>

    <% if configuration.fields.any? { |f| f.description.present? } %>
      <p class="text-secondary text-sm mb-4">Field descriptions:</p>
      <ul class="text-sm text-secondary mb-4 list-disc ml-6 space-y-2">
        <% configuration.fields.each do |field| %>
          <% if field.description.present? %>
            <li><strong><%= field.label %>:</strong> <%= field.description %></li>
          <% end %>
        <% end %>
      </ul>
    <% end %>
  </div>

  <%= styled_form_with model: Setting.new,
                       url: settings_providers_path,
                       method: :patch do |form| %>
    <div class="space-y-4">
      <% configuration.fields.each do |field| %>
        <%
          env_value = ENV[field.env_key] if field.env_key
          # Use dynamic hash-style access - works without explicit field declaration
          setting_value = Setting[field.setting_key]

          # Show the setting value if it exists, otherwise show ENV value
          # This allows users to see what they've overridden
          current_value = setting_value.presence || env_value

          # Mask secret values if they exist
          display_value = if field.secret && current_value.present?
            "********"
          else
            current_value
          end

          # Determine input type
          input_type = field.secret ? "password" : "text"

          # Don't disable fields - allow overriding ENV variables
          disabled = false
        %>

        <%= form.text_field field.setting_key,
                            label: field.label,
                            type: input_type,
                            placeholder: field.default || (field.required ? "" : "Optional"),
                            value: display_value,
                            disabled: disabled %>
      <% end %>

      <div class="flex justify-end">
        <%= form.submit "Save Configuration",
                        class: "inline-flex items-center justify-center rounded-lg px-4 py-2 text-sm font-medium text-white bg-gray-900 hover:bg-gray-800 focus:outline-none focus:ring-2 focus:ring-gray-900 focus:ring-offset-2 transition-colors" %>
      </div>
    </div>
  <% end %>

  <%# Show configuration status %>
  <div class="flex items-center gap-2 mt-4">
    <% if configuration.configured? %>
      <div class="w-2 h-2 bg-success rounded-full"></div>
      <p class="text-sm text-secondary">Configured and ready to use</p>
    <% else %>
      <div class="w-2 h-2 bg-gray-400 rounded-full"></div>
      <p class="text-sm text-secondary">Not configured</p>
    <% end %>
  </div>
</div>
