class AcquiredMediaReleasesController < ApplicationController
  include ProjectContext, AcquiredMediaReleaseContext

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

  include ProjectLayout

  def index
    @acquired_media_releases = filtered_acquired_media_releases.order_by_recent.paginate(page: params[:page])
  end

  def new
    @acquired_media_release = build_acquired_media_release
  end

  def create
    @acquired_media_release = build_acquired_media_release(acquired_media_release_params)

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

  def edit
    @project = @acquired_media_release.project
  end

  def update
    @project = @acquired_media_release.project

    if @acquired_media_release.update(acquired_media_release_params)
      redirect_to [@project, :acquired_media_releases], notice: t(".notice")
    else
      render :edit
    end
  end

  def destroy
    @project = @acquired_media_release.project
    @acquired_media_release.destroy
    redirect_to [@project, :acquired_media_releases], alert: t(".alert")
  end

  private

  def acquired_media_releases
    if @project
      policy_scope(@project.acquired_media_releases)
    else
      policy_scope(AcquiredMediaRelease)
    end
  end

  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 acquired_media_release_params
    params.require(:acquired_media_release).permit(person_params,
                                                   guardian_params,
                                                   second_guardian_params,
                                                   :minor,
                                                   :name,
                                                   :territory,
                                                   :term,
                                                   :contract,
                                                   :applicable_medium_id, :applicable_medium_text,
                                                   :territory_id, :territory_text,
                                                   :term_id, :term_text,
                                                   :restriction_id, :restriction_text,
                                                   categories: [],
                                                   files: []
                                                  )
  end

  def build_acquired_media_release(attrs = {})
    authorize @project.acquired_media_releases.build(attrs)
  end

  def filtered_acquired_media_releases
    results = acquired_media_releases

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

    results
  end
end
