require "rails_helper"

feature "User creates notes" do
  let(:current_user) { create(:user) }
  let(:project) { create(:project, account: current_user.primary_account) }

  shared_examples "a notable collection UI", js: true do
    specify do
      sign_in current_user
      visit polymorphic_path [subject.project, subject.model_name.plural]

      by "not adding content to the note" do
        open_notes_modal(subject)
        submit_notes_modal(subject, notes: "")
        expect(page).to have_css(".invalid-feedback", text: "can't be blank")
      end

      by "adding content to the note" do
        submit_notes_modal(subject, notes: "Test note!")
        expect(page).to have_note_for(subject, "Test note!")
      end

      by "adding another note" do
        add_notes_to(subject, notes: "Another note")
        expect(page).to have_note_for(subject, "Another note")
        expect(page).to have_more_notes_for(subject)
      end

      by "viewing all the notes" do
        open_notes_list_for(subject)
        expect(page).to have_css(".list-group-item", text: "Test note!")
        expect(page).to have_css(".list-group-item", text: "Another note")
      end
    end

    def open_notes_modal(release)
      click_button "Manage"
      click_link "Notes"
    end

    def submit_notes_modal(release, notes:)
      action = url_for([release, :notes, only_path: true])
      notes_form = "form[action='#{action}']"

      within notes_form do
        fill_in "Content", with: notes
        click_button "Create Note"
      end
    end

    def add_notes_to(release, notes:)
      open_notes_modal(release)
      submit_notes_modal(release, notes: notes)
    end

    def have_note_for(release, notes)
      have_selector("td", text: notes)
    end

    def have_more_notes_for(release)
      notes_path = url_for [release, :notes, only_path: true]
      have_link("more note", href: notes_path) # , visible: :all)
    end

    def open_notes_list_for(release)
      notes_path = url_for [release, :notes, only_path: true]
      click_link "more note", href: notes_path
    end
  end

  context "for appearance releases" do
    subject! { create(:appearance_release, project: project, notes: []) }

    it_behaves_like "a notable collection UI"
  end

  context "for talent releases" do
    subject! { create(:talent_release, project: project, notes: []) }

    it_behaves_like "a notable collection UI"
  end

  context "for location releases" do
    subject! { create(:location_release, project: project, notes: []) }

    it_behaves_like "a notable collection UI"
  end

  context "for material releases" do
    subject! { create(:material_release, project: project, notes: []) }

    it_behaves_like "a notable collection UI"
  end

  context "for acquired media releases" do
    subject! { create(:acquired_media_release, project: project, notes: []) }

    it_behaves_like "a notable collection UI"
  end

  context "for music releases" do
    subject! { create(:music_release, project: project, notes: []) }

    it_behaves_like "a notable collection UI"
  end

  context "for medical releases" do
    subject! { create(:medical_release, project: project, notes: []) }

    it_behaves_like "a notable collection UI"
  end
end
