class FileInfosController < ApplicationController
  before_action :set_releasable

  layout "project"

  breadcrumb -> { t("shared.files") }, -> { @releasable.project }, match: :exact
  breadcrumb -> { @releasable.model_name.plural.titleize }, -> { [@releasable.project, @releasable.model_name.plural] }, match: :exact
  breadcrumb -> { t(".heading") }, :url_for

  def edit
    @project = @releasable.project
  end

  def update
    @project = @releasable.project
    
    if @releasable.update(releasable_params)
      SetTagsForReleasableJob.perform_later(@releasable)
      redirect_to [@project, @releasable.model_name.plural], notice: t(".notice")
    else
      render :edit
    end
  end

  private

  def releasable_param
    @releasable_param ||= ReleasableParam.new(params.to_unsafe_h)
  end

  def set_releasable
    @releasable = authorize policy_scope(releasable_param.type).find(releasable_param.id), :"#{action_name}_file_infos?"
  end

  def releasable_params
    params.fetch(releasable_param.name, {}).permit(
      file_infos_attributes: [
        :filename,
        :content_type,
        :byte_size
      ],
    )
  end
end
