ActiveAdmin.register Store do
  permit_params :name, :catalog_url, :store_repository_url

  menu priority: 5, label: "🛒 Stores"

  index do
    selectable_column
    id_column
    column :name
    column :catalog_url do |store|
      link_to store.catalog_url, store.catalog_url, target: "_blank", rel: "noopener"
    end
    column :store_repository_url do |store|
      link_to store.store_repository_url, store.store_repository_url, target: "_blank", rel: "noopener"
    end
    column :updated_at
    actions
  end

  filter :name
  filter :catalog_url
  filter :store_repository_url

  show do
    attributes_table do
      row :id
      row :name
      row :catalog_url do |store|
        link_to store.catalog_url, store.catalog_url, target: "_blank", rel: "noopener"
      end
      row :store_repository_url do |store|
        link_to store.store_repository_url, store.store_repository_url, target: "_blank", rel: "noopener"
      end
      row :created_at
      row :updated_at
    end
    para do
      "Listed by GET /api/stores, which the desktop client reads on first run. With a " \
        "store repository the client takes that config and points it at the catalog " \
        "above; without one it uses the store engine's defaults, which need nothing " \
        "but the name and the catalog."
    end
  end

  form do |f|
    f.inputs do
      f.input :name, hint: "What the client shows in its store picker"
      f.input :catalog_url, hint: "Base URL of the WarpEngine catalog, e.g. https://teletypegames.org"
      f.input :store_repository_url,
              hint: "Optional. A repository holding the store's config.json, e.g. " \
                    "https://git.teletypegames.org/stores/ttg-desktop-store. Leave it " \
                    "empty and the client uses the engine's defaults with the name and " \
                    "catalog above"
    end
    f.actions
  end
end
