class Public::AcquiredMediaReleasesController < Public::BaseController
  include CreateReleasableJobs
  before_action :set_account, :set_project, :set_contract_template

  def new
    @acquired_media_release = build_acquired_media_release
  end

  def create
    @acquired_media_release = build_acquired_media_release(acquired_media_release_params_with_locale_and_contract_template)

    if @acquired_media_release.save(context: :native)
      after_create(@acquired_media_release)
    else
      render :new
    end
  end

  private

  def set_project
    @project = @account.projects.find(params[:project_id])
  end

  def set_account
    @account = Account.find_by(slug: params[:account_id])
  end

  def set_contract_template
    @contract_template = @project.contract_templates.find(params[:contract_template_id])
  end

  def acquired_media_releases
    policy_scope(@project.acquired_media_releases)
  end

  def build_acquired_media_release(params = {})
    acquired_media_releases.build(params).tap do |acquired_media_release|
      authorize(acquired_media_release)
    end
  end

  def person_params
    %i[
      person_first_name
      person_last_name
      person_phone
      person_fax
      person_email
      person_title
      person_address_street1
      person_address_street2
      person_address_city
      person_address_state
      person_address_zip
      person_address_country
    ]
  end

  def guardian_params
    %i[
      guardian_first_name
      guardian_last_name
      guardian_phone
      guardian_email
      guardian_photo
      guardian_address_street1
      guardian_address_street2
      guardian_address_city
      guardian_address_state
      guardian_address_zip
      guardian_address_country
    ]
  end

  def second_guardian_params
    %i[
      guardian_2_first_name
      guardian_2_last_name
      guardian_2_phone
      guardian_2_email
      guardian_2_photo
      guardian_2_address_street1
      guardian_2_address_street2
      guardian_2_address_city
      guardian_2_address_state
      guardian_2_address_zip
      guardian_2_address_country
    ]
  end

  def acquired_media_release_params
    params.require(:acquired_media_release).permit(person_params,
                                                   guardian_params,
                                                   second_guardian_params,
                                                   :minor,
                                                   :name,
                                                   :description,
                                                   :signature_base64,
                                                   :locale, :contract_template,
                                                   categories: [],
                                                   files: []
                                                  )
  end

  def acquired_media_release_params_with_locale
    acquired_media_release_params.merge(locale: I18n.locale)
  end

  def acquired_media_release_params_with_locale_and_contract_template
    acquired_media_release_params_with_locale.merge(contract_template: @contract_template)
  end
end
