require "rails_helper"

feature "User managing music releases" do
  let(:current_user) { create(:user, :manager) }
  let(:project) { create(:project, members: current_user, account: current_user.primary_account) }

  scenario "can perform crud actions" do
    release_data = attributes_for(:music_release)

    sign_in current_user
    visit project_music_releases_path(project)

    by "creating a release" do
      click_link *import_music_release_link(project)
      expect(page).to have_content "For optimal accuracy, please ensure music file names match the source file name in the editing sequence."
      fill_in_release_fields release_data
      fill_in_exploitable_rights
      click_button create_release_button
      expect(page).to have_content(create_release_notice)
      expect(page).to have_content(release_data[:name])
    end

    and_by "updating an existing release" do
      click_link *update_music_release_link(MusicRelease.last)

      fill_in_release_fields name: "New release name"
      select "Other", from: "Applicable Media"
      fill_in "Describe other applicable media", with: "New Test"

      click_button update_release_button

      expect(page).to have_content(update_release_notice)
      expect(page).to have_content("New release name")
    end

    and_by "deleting an existing release" do
      click_link *destroy_music_release_link(MusicRelease.last)

      expect(page).to have_content(destroy_release_alert)
      expect(page).not_to have_content("New release name")
    end
  end

  scenario "creating a music release with validation errors", js: true do
    sign_in current_user
    visit new_project_music_release_path(project)

    by "attaching only a contract" do
      attach_file "music_release[contract]", Rails.root.join(file_fixture("contract.pdf")), visible: false
      click_button create_release_button

      expect(page).to have_invalid_field(music_release_name_field)
    end

    and_by "attaching files" do
      drop_file Rails.root.join(file_fixture("audio.mp3")), type: "file-info-dropzone"
      click_button create_release_button

      expect(page).to have_invalid_field(music_release_name_field)
    end

    and_by "filling out the remaining information" do
      fill_in_release_fields name: "Big Media OST"
      click_button create_release_button

      expect(page).to have_content(create_release_notice)
      click_on "Manage"
      expect(page).to have_link("Download")
    end
  end

  scenario "searching for a release", js: true do
    collection1 = create(:music_release, name: "EDM Music", project: project)
    collection2 = create(:music_release, name: "Classical Music", project: project)

    sign_in current_user
    visit project_music_releases_path(project)

    within "form#search" do
      fill_in "Search", with: "EDM"
      click_on "button"
    end

    expect(page).to have_content("EDM Music")
    expect(page).not_to have_content("Classical Music")
    expect(page).to have_field("Search", with: "EDM")
  end

  context "when the user is associate" do
    let(:current_user) { create(:user, :associate) }
  
    scenario "should not show download" do
      collection1 = create(:music_release, name: "EDM Music", project: project)
  
      sign_in current_user
      visit project_music_releases_path(project)
  
      click_on "Manage"
      expect(page).not_to have_link("Download", exact: true)
    end
  end

  private

  def music_release_name_field
    "music_release[name]"
  end

  def import_music_release_link(project)
    ["Import Release", href: new_project_music_release_path(project)]
  end

  def update_music_release_link(music_release)
    ["Edit", href: edit_music_release_path(music_release)]
  end

  def destroy_music_release_link(music_release)
    ["Delete", href: music_release_path(music_release)]
  end

  def fill_in_release_fields(data)
    fill_in "music_release[name]", with: data[:name]
    fill_in "music_release[person_first_name]", with: data[:person_first_name]
    fill_in "music_release[person_last_name]", with: data[:person_last_name]

    fill_in "music_release_composers_attributes_0_name", with: "composer name"
    fill_in "music_release_composers_attributes_0_affiliation", with: "composer affiliation"
    fill_in "music_release_composers_attributes_0_percentage", with: 100
    fill_in "music_release_composers_attributes_0_cae_number", with: "CAE123456789"

    fill_in "music_release_publishers_attributes_0_name", with: "publisher name"
    fill_in "music_release_publishers_attributes_0_affiliation", with: "publisher affiliation"
    fill_in "music_release_publishers_attributes_0_percentage", with: 100
  end

  def create_release_button
    t "helpers.submit.music_release.create"
  end

  def create_release_notice
    t "music_releases.create.notice"
  end

  def update_release_button
    t "helpers.submit.music_release.update"
  end

  def update_release_notice
    t "music_releases.update.notice"
  end

  def destroy_release_alert
    t "music_releases.destroy.alert"
  end

  def fill_in_exploitable_rights
    select "Other", from: "Applicable Media"
    fill_in "Describe other applicable media", with: "Test"
    select "Other", from: "Territory"
    fill_in "Describe other territory", with: "Test"
    select "Other", from: "Term"
    fill_in "Describe other term", with: "Test"
    select "Other", from: "Restriction"
    fill_in "Describe other restrictions", with: "Test"
  end
end
