ActiveAdmin.register Member do
  permit_params :nick, :real_nick, :motto, :avatar_filename, :image_id

  menu priority: 4, label: "👥 Members"

  actions :all, except: [ :edit ]

  index do
    selectable_column
    id_column
    column :nick do |m|
      link_to m.nick, admin_member_path(m)
    end
    column :real_nick
    column :motto do |m|
      truncate m.motto, length: 60
    end
    column(:image) do |m|
      if m.image && File.exist?(m.image.file_path)
        image_tag "/api/image/#{m.image.id}", style: "max-height:40px;max-width:80px;object-fit:contain;"
      end
    end
    column(:admin_user) { |m| m.admin_user ? link_to(m.admin_user.email, admin_admin_user_path(m.admin_user)) : "–" }
    column :created_at
    actions
  end

  filter :nick
  filter :real_nick

  show title: proc { |m| m.nick } do
    active_admin_form_for [ :admin, resource ], url: admin_member_path(resource), html: { method: :put } do |f|
      f.inputs "Edit Member" do
        f.input :nick
        f.input :real_nick
        f.input :motto
        f.input :avatar_filename,
                hint: "Fallback image filename (e.g. mr.zero.png) — only used when no image is selected."

        img_hint = if resource.image_id.present? && resource.image
          f.template.image_tag("/api/image/#{resource.image_id}", style: "max-height:80px;max-width:160px;object-fit:contain;margin-top:6px;").html_safe
        end
        f.input :image_id, as: :select, label: "Image",
                collection: WarpEngine::Image.order(:original_filename).map { |img| [ img.original_filename, img.id ] },
                include_blank: "— no image (use avatar_filename) —",
                hint: img_hint
      end
      f.actions
    end
  end

  controller do
    def scoped_collection
      super.includes(:image, :admin_user)
    end

    def find_resource
      scoped_collection.find(params[:id])
    end
  end
end
