require "rails_helper"

feature "Admin managing task requests" do
  let(:current_user) { create(:user, admin: true, email: "user@test.com") }
  let(:project) { create(:project, account: current_user.primary_account, name: "Test Project") }

  before do
    sign_in current_user
  end

  scenario "admin should see View action for task request in Manage dropdown", js: true do
    create(:task_request)
    visit admin_task_requests_path

    click_on 'Manage'
    expect(page).to have_content 'View'
  end

  scenario "admin can open detail view of task request", js: true do
    task_request = create(:task_request, :with_files)
    visit admin_task_requests_path

    click_on 'Manage'
    click_link 'View'
    switch_to_window(windows.last)

    expect(page).to have_content 'Account Name'
    expect(page).to have_content  task_request.project.account.name

    expect(page).to have_content 'Project Name'
    expect(page).to have_content task_request.project.name

    expect(page).to have_content 'Description'
    expect(page).to have_content  task_request.description

    expect(page).to have_content 'Created At'
    expect(page).to have_content task_request.created_at

    expect(page).to have_content 'User Email'
    expect(page).to have_content task_request.user_email

    expect(page).to have_content 'Deadline'
    expect(page).to have_content task_request.deadline

    expect(page).to have_content 'Time Allowed'
    expect(page).to have_content task_request.time_allowed

    expect(page).to have_content 'Additional Notes'
    expect(page).to have_content task_request.additional_notes

    expect(page).to have_content 'Status'
    expect(page).to have_content task_request.status

    expect(page).to have_content 'Files'
    task_request.files.each do |file|
      expect(page).to have_content file.blob.filename
    end
  end

  scenario "no files attached label is shown if there are no files attached to the task request", js:true do
    create(:task_request)
    visit admin_task_requests_path

    click_on 'Manage'
    click_link 'View'
    switch_to_window(windows.last)

    expect(page).to have_content 'Attached files will appear here.'
  end

  scenario "task requests table is visible" do
    visit admin_task_requests_path

    expect(page).to have_content "Task ID"
    expect(page).to have_content "Account Name"
    expect(page).to have_content "Project Name"
    expect(page).to have_content "Created On"
    expect(page).to have_content "Deadline"
    expect(page).to have_content "Time Allowed"
    expect(page).to have_content "Status"
  end

  scenario "sees list of task requests" do
    visit admin_task_requests_path

    expect(page).to have_content no_task_requests_label

    task_request = create(:task_request)

    visit admin_task_requests_path

    expect(page).not_to have_content no_task_requests_label

    expect(page).to have_content task_request.id
    expect(page).to have_content task_request.project.account.name
    expect(page).to have_content task_request.project.name
    expect(page).to have_content task_request.created_at.try(:strftime, '%D')
    expect(page).to have_content task_request.deadline.try(:strftime, '%D')
    expect(page).to have_content task_request.time_allowed
    expect(page).to have_content task_request.status.capitalize
  end

  private

  def no_task_requests_label
    "Task requests will appear here"
  end
end
