require "rails_helper"

feature "User managing projects" do
  let(:user) { create(:user, :account_manager) }

  before do
    sign_in(user)
  end

  scenario "creating a new project", js: true do
    visit new_project_path

    it_also "requires a project name" do
      click_on "Create Project"

      expect(page).to have_content "can't be blank"
    end

    fill_in "Name", with: "Avengers"
    select "Other", from: "Client"
    fill_in "Client name", with: "My Client"
    uncheck "Appearance Release"
    check "Location Release"
    click_on "Create Project"

    expect(page).to have_content "The project has been created"
    expect(page).to have_content "Avengers"
    expect(page).to have_content /My Client/i
    expect(page).not_to have_content "Appearance Releases"
    expect(page).to have_content "Location Releases"
  end

  scenario "editing a project", js: true do
    project = create(:project, members: user, account: user.primary_account, name: "Avengers", client_name: "Marvel")

    visit projects_path
    click_on "button"
    click_on "Edit"

    it_also "requires a project name" do
      fill_in "Name", with: ""
      click_on "Update Project"

      expect(page).to have_content "can't be blank"
    end

    fill_in "Name", with: "Justice League"
    select "Other", from: "Client"
    fill_in "Client name", with: "DC"
    uncheck "Appearance Release"
    check "Location Release"
    click_on "Update Project"

    expect(page).to have_content "The project has been updated"
    expect(page).to have_content "Justice League"
    expect(page).to have_content /DC/i
    expect(page).not_to have_content "Appearance Releases"
    expect(page).to have_content "Location Releases"
  end

  scenario "deleting a project", js: true do
    skip "This functionality is no longer available"

    project = create(:project, members: user, name: "Avengers", account: user.primary_account)
    create(:appearance_release, project: project)

    visit projects_path

    expect(page).to have_content("Avengers")

    click_on "button"
    accept_alert do
      click_on "Delete"
    end

    expect(page).to have_content "The project has been deleted"
    expect(page).not_to have_content "Avengers"
  end

  scenario "all features are enabled" do
    project = create(:project, members: user, account: user.primary_account)
    enable_all_project_features(project)

    visit project_path(project)

    expect(page).to have_content("Talent Releases")
    expect(page).to have_content("Appearance Releases")
    expect(page).to have_content("Acquired Media Releases")
    expect(page).to have_content("Material Releases")
    expect(page).to have_content("Music Releases")
  end

  scenario "some features are disabled" do
    project = create(:project, members: user, account: user.primary_account)
    disable_project_features(project, :material_release, :music_release)

    visit project_path(project)

    expect(page).to have_content("Talent Releases")
    expect(page).to have_content("Appearance Releases")
    expect(page).to have_content("Acquired Media Releases")
    expect(page).not_to have_content("Material Releases")
    expect(page).not_to have_content("Music Releases")
  end

  # TODO: What about the welcome page when there are no existing projects?

  context "for a manager" do
    let(:user) { create(:user, :manager) }
    let!(:project) { create(:project, members: user, account: user.primary_account) }

    scenario "can manage projects" do
      visit projects_path

      expect(page).not_to have_link("Create New Project")
      expect(page).to have_link("Edit", href: edit_project_path(project))
      expect(page).not_to have_link("Delete", href: project_path(project))
      # expect(page).to have_link("Archive", href: project_archivals_path(project))
    end
  end

  context "for an associate" do
    let(:user) { create(:user, :associate) }
    let!(:project) { create(:project, members: user, account: user.primary_account) }
    scenario "cannot manage projects" do
      visit projects_path

      expect(page).not_to have_link("Create New Project")
      expect(page).not_to have_link("Edit", href: edit_project_path(project))
      expect(page).not_to have_link("Delete", href: project_path(project))
      # expect(page).not_to have_link("Archive", href: project_archivals_path(project))
    end
  end

  private

  def enable_all_project_features(project)
    disable_project_features(project)
  end

  def disable_project_features(project, *features)
    defaults = {
      appearance_release: true,
      location_release: true,
      material_release: true,
      acquired_media_release: true,
      talent_release: true,
      music_release: true,
      video_analysis: true,
    }
    disabled_features = Array.wrap(features).each_with_object({}) { |feature, hash| hash[feature] = false }

    project.settings(:features).update(defaults.merge(disabled_features))
  end
end
