class MaterialReleasesController < ApplicationController
  include ProjectContext

  before_action :set_project, only: [:index, :new, :create]
  before_action :set_material_release, only: [:edit, :update, :destroy]

  include ProjectLayout

  def index
    @material_releases = filtered_material_releases.order_by_recent.paginate(page: params[:page])
  end

  def new
    @material_release = build_material_release
  end

  def create
    @material_release = build_material_release(material_release_params)

    if @material_release.save
      SetTagsForReleasableJob.perform_later(@material_release)
      redirect_to [@project, :material_releases], notice: t(".notice")
    else
      render :new
    end
  end

  def edit
    @project = @material_release.project
  end

  def update
    @project = @material_release.project

    if @material_release.update(material_release_params)
      redirect_to [@project, :material_releases], notice: t(".notice")
    else
      render :edit
    end
  end

  def destroy
    @project = @material_release.project

    @material_release.destroy

    redirect_to [@project, :material_releases], alert: t(".alert")
  end

  private

  def person_params
    %i[
      person_first_name
      person_last_name
      person_phone
      person_company
      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 material_release_params
    params.require(:material_release).permit(person_params, guardian_params, second_guardian_params, :minor,
      :name, :address_street1, :address_street2, :address_city, :address_state, :address_zip, :address_country,
      :applicable_medium_id, :applicable_medium_text,
      :territory_id, :territory_text,
      :term_id, :term_text,
      :restriction_id, :restriction_text,
      :description,
      :contract, files: []
    )
  end

  def material_releases
    if @project
      policy_scope(@project.material_releases)
    else
      policy_scope(MaterialRelease)
    end
  end

  def build_material_release(attrs = {})
    authorize @project.material_releases.build(attrs)
  end

  def set_material_release
    @material_release = authorize material_releases.find(params[:id])
  end

  def filtered_material_releases
    results = material_releases

    if params[:query].present?
      results = results.search(params[:query])
    end

    results
  end
end
