# frozen_string_literal: true

class AppearanceReleaseImportsController < ApplicationController
  include AppearanceReleaseContext
  include ProjectContext

  before_action :set_project, only: [:create]

  include ProjectLayout

  def create
    authorize AppearanceRelease
    attachments = appearance_release_params

    if attachments.nil?
      alert_message = t 'appearance_releases.create.no_attachments'
      redirect_to [@project, :appearance_releases], alert: alert_message
    else
      MatchAppearanceReleasesJob.perform_later(@project, attachments)
      notice_message = t 'appearance_releases.create.matching_started'
      redirect_to [@project, :appearance_releases], notice: notice_message
    end
  end

  private

  def appearance_releases
    if @project
      policy_scope(@project.appearance_releases)
    else
      super
    end
  end

  def appearance_release_params
    params.require(:attachments)
  end

  def acceptable_extensions
    AppearanceRelease.acceptable_import_file_extensions
  end
end
