<%= form_with model: provider, scope: :provider,
      url: provider.new_record? ? providers_path : provider_path(provider) do |f| %>

  <% if provider.errors.any? %>
    <div class="alert alert-danger">
      <ul class="mb-0">
        <% provider.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.hidden_field :type, value: provider.type_name %>

  <div class="mb-3">
    <span class="badge bg-secondary"><%= provider.type_name %></span>
  </div>

  <div class="mb-3">
    <%= f.label :name, class: 'form-label' %>
    <%= f.text_field :name, class: 'form-control', required: true %>
  </div>

  <% if provider.requires_api_key? %>
    <div class="mb-3">
      <%= f.label :api_key, 'API Key', class: 'form-label' %>
      <%= f.password_field :api_key, class: 'form-control',
          placeholder: provider.api_key.present? ? '(already set)' : nil,
          required: provider.new_record? %>
    </div>
  <% end %>

  <div class="mb-3">
    <%= f.label :model, class: 'form-label' %>
    <%= f.text_field :model, class: 'form-control', required: true %>
  </div>

  <div class="mb-3">
    <%= f.label :address, 'API Base URL', class: 'form-label' %>
    <%= f.text_field :address, class: 'form-control', required: true %>
  </div>

  <div class="form-actions">
    <%= f.submit provider.new_record? ? 'Add Provider' : 'Update Provider',
        class: 'btn btn-primary' %>
    or <%= link_to 'Cancel', providers_path %>
  </div>
<% end %>
