<% record = local_assigns[:record] %>
<% show_inherit_hint = local_assigns[:show_inherit_hint] %>
<% param_key = record.model_name.param_key %>
<% primary = record.primary_color.presence || record.effective_primary_color %>
<% secondary = record.secondary_color.presence || record.effective_secondary_color %>
<% logo_src = record.effective_logo_url.presence %>

<fieldset class="branding-fields">
  <legend><%= local_assigns[:legend] || "Identità visiva" %></legend>

  <% if show_inherit_hint %>
    <p class="branding-hint">Lascia vuoto per usare logo e colori della società.</p>
  <% end %>

  <div class="branding-logo-block">
    <%= label_tag "branding[logo_file]", "Logo (immagine)" %>
    <div class="branding-logo-preview" data-branding-logo-preview>
      <%= image_tag logo_src.presence || "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
            alt: "Anteprima logo",
            class: "branding-logo-preview__img",
            data: { branding_logo_img: true },
            width: 96,
            height: 96,
            hidden: logo_src.blank? %>
      <span class="branding-logo-preview__placeholder" data-branding-logo-placeholder <%= "hidden" if logo_src.present? %>>
        Nessun logo — scegli un file per l’anteprima
      </span>
    </div>
    <%= file_field_tag "branding[logo_file]", accept: "image/png,image/jpeg,image/webp", data: { branding_logo_file: true }, class: "branding-file-input" %>
  </div>

  <%= label_tag "#{param_key}[logo_url]", "Logo (URL alternativo)" %>
  <%= text_field_tag "#{param_key}[logo_url]", record.logo_url, placeholder: "https://…", class: "branding-url-input" %>

  <%= label_tag "#{param_key}[primary_color]", "Colore principale" %>
  <div class="branding-color-row" data-branding-color-row="primary">
    <%= color_field_tag "#{param_key}[primary_color]", primary, data: { color_picker: true }, class: "branding-color-picker", title: "Scegli colore" %>
    <%= text_field_tag nil, primary, data: { color_hex: true }, class: "branding-hex-input", placeholder: "#e53935", autocomplete: "off", spellcheck: false %>
    <span class="color-swatch branding-color-swatch" data-color-swatch aria-hidden="true"></span>
    <div class="branding-color-presets" aria-label="Colori rapidi">
      <% %w[#e53935 #1565c0 #2e7d32 #ff9800 #212121 #ffffff].each do |hex| %>
        <button type="button" class="branding-preset" data-color-preset="<%= hex %>" style="background:<%= hex %>" title="<%= hex %>"></button>
      <% end %>
    </div>
  </div>

  <%= label_tag "#{param_key}[secondary_color]", "Colore secondario" %>
  <div class="branding-color-row" data-branding-color-row="secondary">
    <%= color_field_tag "#{param_key}[secondary_color]", secondary, data: { color_picker: true }, class: "branding-color-picker", title: "Scegli colore" %>
    <%= text_field_tag nil, secondary, data: { color_hex: true }, class: "branding-hex-input", placeholder: "#ffffff", autocomplete: "off", spellcheck: false %>
    <span class="color-swatch branding-color-swatch" data-color-swatch aria-hidden="true"></span>
    <div class="branding-color-presets" aria-label="Colori rapidi">
      <% %w[#ffffff #f5f5f5 #212121 #ffd54f #90caf9 #e53935].each do |hex| %>
        <button type="button" class="branding-preset" data-color-preset="<%= hex %>" style="background:<%= hex %>" title="<%= hex %>"></button>
      <% end %>
    </div>
  </div>

  <div class="branding-live-preview" data-branding-live-preview>
    <span class="branding-live-preview__label">Anteprima intestazione</span>
    <div class="branding-live-preview__header" data-preview-header style="--preview-primary:<%= primary %>;--preview-secondary:<%= secondary %>">
      <span class="branding-live-preview__logo" data-preview-logo>
        <% if logo_src %>
          <%= image_tag logo_src, alt: "", width: 40, height: 40, style: "border-radius:8px;object-fit:contain" %>
        <% end %>
      </span>
      <span class="branding-live-preview__name"><%= record.try(:name).presence || "Nome società / squadra" %></span>
    </div>
  </div>
</fieldset>
