class AppearanceReleasesController < ApplicationController
  include ProjectContext, AppearanceReleaseContext

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

  include ProjectLayout

  def index
    @appearance_releases = filtered_appearance_releases.order_by_recent.paginate(page: params[:page])
  end

  def new
    @appearance_release = build_appearance_release
  end

  def create
    @appearance_release = build_appearance_release(appearance_release_params)

    if @appearance_release.save(context: :non_native)
      log_create_analytics
      AddHeadshotCollectionUidToProjectJob.perform_later(@project)
      SetTagsForReleasableJob.perform_later(@appearance_release)
      redirect_to [@project, :appearance_releases], notice: "The release has been imported. #{link_to_import_another}"
    else
      render :new
    end
  end

  def edit
    @project = @appearance_release.project
  end

  def update
    @project = @appearance_release.project

    @appearance_release.attributes = appearance_release_params

    if @appearance_release.save(context: :non_native)
      AddHeadshotCollectionUidToProjectJob.perform_later(@project)
      redirect_to [@project, :appearance_releases], notice: "The release has been updated"
    else
      render :edit
    end
  end

  def destroy
    @project = @appearance_release.project

    if @appearance_release.destroy
      AddHeadshotCollectionUidToProjectJob.perform_later(@appearance_release.project)

      redirect_to [@project, :appearance_releases], alert: "The release has been deleted"
    end
  end

  private

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

  def filtered_appearance_releases
    results = case params[:type_filter]
              when 'complete'
                appearance_releases.complete
              when 'incomplete'
                appearance_releases.incomplete
              else
                appearance_releases
              end
    results = results.search(params[:query]) if params[:query].present?
    results
  end

  def appearance_release_params
    params.require(:appearance_release).permit(:contract, :guardian_address, :guardian_first_name, :guardian_last_name, :guardian_phone, :guardian_photo, :guardian_email, :minor,
                                               :person_address_old, :person_first_name, :person_last_name, :person_phone, :person_email, :person_photo,
                                               :applicable_medium_id, :applicable_medium_text,
                                               :territory_id, :territory_text,
                                               :term_id, :term_text, :person_date_of_birth,
                                               :restriction_id, :restriction_text)
  end

  def build_appearance_release(params = {})
    authorize appearance_releases.build(params)
  end

  def link_to_import_another
    view_context.link_to "Import Another", [:new, @project, :appearance_release]
  end

  def log_create_analytics
    TrackAnalyticsJob.perform_later(Current.user, Current.account, :track_create_non_native_release, release_type: AppearanceRelease.to_s, user_agent: request.user_agent, user_ip: request.remote_ip)
  end
end
