class MusicReleasesController < ApplicationController
  include ProjectContext, MusicReleaseContext

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

  include ProjectLayout

  NUMBER_OF_COMPOSERS = 5
  NUMBER_OF_PUBLISHERS = 2

  def index
    @music_releases = filtered_music_releases.order_by_recent.paginate(page: params[:page])
  end

  def new
    @music_release = build_music_release
    prefill_composers_and_publishers
  end

  def create
    @music_release = build_music_release(music_release_params)

    if @music_release.save
      log_create_analytics
      SetTagsForReleasableJob.perform_later(@music_release)
      redirect_to [@project, :music_releases], notice: t(".notice")
    else
      prefill_composers_and_publishers
      render :new
    end
  end

  def edit
    @project = @music_release.project
    prefill_composers_and_publishers
  end

  def update
    @project = @music_release.project

    if @music_release.update(music_release_params)
      redirect_to [@project, :music_releases], notice: t(".notice")
    else
      prefill_composers_and_publishers
      render :edit
    end
  end

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

  private

  def prefill_composers_and_publishers
    (NUMBER_OF_COMPOSERS - @music_release.composers.size).times { @music_release.composers.build }
    (NUMBER_OF_PUBLISHERS - @music_release.publishers.size).times { @music_release.publishers.build }
  end

  def music_releases
    if @project
      policy_scope(@project.music_releases)
    else
      policy_scope(MusicRelease)
    end
  end

  def music_release_params
    params.require(:music_release).permit(
      :name, :person_first_name, :person_last_name, :person_phone, :person_email, :person_company, :person_title,
      :person_address_street1, :person_address_street2, :person_address_city, :person_address_state, :person_address_zip, :person_address_country,
      :contract,
      :applicable_medium_id, :applicable_medium_text,
      :territory_id, :territory_text,
      :term_id, :term_text,
      :restriction_id, :restriction_text,
      file_infos_attributes: [
        :filename,
        :content_type,
        :byte_size
      ],
      composers_attributes: [
        :id,
        :name,
        :affiliation,
        :percentage,
        :cae_number,
      ],
      publishers_attributes: [
        :id,
        :name,
        :affiliation,
        :percentage
      ]
    )
  end

  def build_music_release(attrs = {})
    authorize @project.music_releases.build(attrs)
  end

  def filtered_music_releases
    results = music_releases

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

    results
  end

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