# frozen_string_literal: true

require 'rails_helper'

RSpec.feature 'User creates task request', type: :feature do
  let(:current_user) { create(:user, :manager) }
  let(:project) { create(:project, members: current_user, account: current_user.primary_account) }

  before do
    sign_in(current_user)
  end

  scenario 'creating a new task request' do
    visit new_project_task_request_path(project)

    fill_in description_field, with: 'Description of the task'
    fill_in deadline_field, with: '2020-06-24'
    fill_in time_allowed_field, with: '30'
    fill_in additional_notes_field, with: 'Additional note about the task'

    click_on 'Create Task request'

    expect(page).to have_content task_created_message
    expect(page).to have_link("Back")
  end

  scenario 'user can view task request details' do
    create(:task_request, project: project, description: 'Description of the task', deadline: '2020-07-23', time_allowed: '10', additional_notes: 'Additional note about the task')
    
    visit project_task_request_path(project, TaskRequest.first)

    expect(page).to have_content('Description of the task')
    expect(page).to have_content('2020-07-23')
    expect(page).to have_content('10')
    expect(page).to have_content('Additional note about the task')
  end

  scenario 'user can update existing task request' do
    create(:task_request, project: project, description: 'Description of the task', deadline: '2020-08-23', time_allowed: '10', additional_notes: 'Additional note about the task')

    visit edit_project_task_request_path(project, TaskRequest.first)

    fill_in deadline_field, with: '2020-07-01'
    fill_in time_allowed_field, with: '13'

    click_on 'Update Task request'
    expect(page).to have_content('13')
    expect(page).to have_content('07/01/20')
  end

  scenario 'user can cancel a task request' do
    create(:task_request, project: project, description: 'Description of the task', deadline: '2020-08-23', time_allowed: '10', additional_notes: 'Additional note about the task')

    visit project_task_requests_path(project)

    click_on 'Manage'
    click_link 'Cancel'

    expect(page).to have_content('Cancelled')
  end

  scenario 'user can view completed tasks' do
    create_list(:task_request, 5, project: project, status: 'completed')
    create_list(:task_request, 5, project: project, status: 'pending')

    visit project_tasks_path(project)

    expect(page).to have_content('Completed')
    expect(page).not_to have_content('Pending')
  end

  private

  def description_field
    t "task_requests.form.labels.description"
  end

  def deadline_field
    t "task_requests.form.labels.deadline"
  end

  def time_allowed_field
    t "task_requests.form.labels.time_allowed"
  end

  def additional_notes_field
    t "task_requests.form.labels.additional_notes"
  end

  def task_created_message
    t 'task_requests.create.success_message'
  end
end
