require "rails_helper"

RSpec.feature "User manages project custom folders", type: :feature do
  let(:user) { create(:user, :manager) }
  let(:project) { create(:project, members: user, account: user.primary_account) }

  before do
    sign_in(user)
  end

  scenario "creating a new folder" do
    visit new_project_directory_path(project)

    fill_in "Name", with: "Actors"
    select "Videos", from: "Category"

    click_on "Create Folder"

    expect(page).to have_content("The folder has been created")
  end
  
  scenario "updating a folder" do
    dir = create(:directory)
    visit edit_project_directory_path(project, dir)

    fill_in "Name", with: "New Actors"
    select "Videos", from: "Category"

    click_on "Update Folder"

    expect(page).to have_content("The folder has been updated")
  end

  scenario "deleting an existing folder", js: true do
    create(:directory, project: project)
    visit project_path(project)

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

    click_on "button"
    accept_alert do
      click_on "Delete Folder"
    end

    expect(page).to have_content("The folder has been deleted")
  end

  scenario "adding files to a folder", js: true do
    dir = create(:directory)
    visit new_file_project_directory_path(project, dir)

    expect(page).to have_content("UPLOAD NEW FILES")

    drop_file Rails.root.join(file_fixture("person_photo.png")), type: :dropzone

    click_button "Upload Files"
    expect(page).to have_content("The folder has been updated")
    expect(page).to have_content("person_photo.png")
  end

  scenario "folder's show page should show uploaded files", js: true do
    dir_with_files = create(:directory, :with_files)
    visit project_directory_path(project, dir_with_files)

    expect(page).to have_content("location_photo.png")
  end

  scenario "Listed file should have a manage button", js: true do
    dir_with_files = create(:directory, :with_files)
    visit project_directory_path(project, dir_with_files)

    expect(page).to have_content("location_photo.png")

    click_on "Manage"
    expect(page).to have_link("Download File")
    expect(page).to have_link("Delete File")
  end

  scenario "searching for a file", js: true do
    dir_with_many_files = create(:directory, :many_files, project: project)

    visit project_directory_path(project, dir_with_many_files)

    within "form" do
      fill_in "Search Files", with: "location"
      click_on "button"
    end

    expect(page).to have_content("location_photo.png")
    expect(page).not_to have_content("material_photo.png")
    expect(page).not_to have_content("person_photo.png")
    expect(page).to have_field("Search Files", with: "location")
  end

  scenario "User can delete a file", js: true do
    dir_with_files = create(:directory, :with_files)
    visit project_directory_path(project, dir_with_files)

    click_on "Manage"
    
    accept_alert do
      click_on "Delete File"
    end

    expect(page).to have_content("File deleted successfully")
  end
end
