<div class="space-y-4">
  <%= render "settings/providers/setup_steps",
             steps: [
               t("coinstats_items.new.step1_html").html_safe,
               t("coinstats_items.new.step2"),
               t("coinstats_items.new.step3_html", accounts_url: accounts_path).html_safe
             ] %>

  <% error_msg = local_assigns[:error_message] || @error_message %>
  <% if error_msg.present? %>
    <%= render DS::Alert.new(message: error_msg, variant: :error) %>
  <% end %>

  <%
    # Get or initialize a coinstats_item for this family
    # - If family has an item WITH credentials, use it (for updates)
    # - If family has an item WITHOUT credentials, use it (to add credentials)
    # - If family has no items at all, create a new one
    coinstats_item = Current.family.coinstats_items.first_or_initialize(name: t("coinstats_items.new.default_name"))
    is_new_record = coinstats_item.new_record?
  %>

  <%= styled_form_with model: coinstats_item,
                       url: is_new_record ? coinstats_items_path : coinstats_item_path(coinstats_item),
                       scope: :coinstats_item,
                       method: is_new_record ? :post : :patch,
                       data: { turbo: true },
                       class: "space-y-3" do |form| %>
    <%= form.text_field :api_key,
                        label: t("coinstats_items.new.api_key_label"),
                        placeholder: t("coinstats_items.new.api_key_placeholder"),
                        type: :password %>

    <div class="flex justify-end">
      <%= form.submit is_new_record ? t("coinstats_items.new.configure") : t("coinstats_items.new.update_configuration"),
                      class: "inline-flex items-center justify-center rounded-lg px-4 py-2 text-sm font-medium text-inverse bg-inverse hover:bg-inverse-hover focus:outline-none focus:ring-2 focus:ring-gray-900 focus:ring-offset-2 transition-colors" %>
    </div>
  <% end %>

</div>
