require 'rails_helper'

describe 'Cart', type: :feature do
  feature 'can be empty' do
    let(:book) { create(:book) }
    before { visit root_path; click_on 'Cart' }

    scenario 'without content' do
      expect(page).to have_content 'Nothing here yet'
      expect(page).to have_link 'Back to shopping'
      expect(page).not_to have_content book.name
    end
  end

  feature 'can add' do
    before { create(:bike); create(:book) }
    before { visit root_path }

    scenario 'single product' do
      first(:button, 'Add to cart').click
      expect(page).to have_content('Cart | 1')
    end

    scenario 'product with quantity' do
      within first('.add_to_carty_cart') do
        fill_in 'order_item_quantity', with: 2
        click_on 'Add to cart'
      end
      expect(page).to have_content('Cart | 2')
    end

    scenario 'different type of products' do
      find('#bikes').click_on 'Add to cart'
      find('#books').click_on 'Add to cart'
      expect(page).to have_content('Cart | 2')
    end
  end

  feature 'cannot add' do
    before { create(:bike) }
    before { visit root_path }

    scenario 'product with nil quantity' do
      within '.add_to_carty_cart' do
        fill_in 'order_item_quantity', with: 0
        click_on 'Add to cart'
      end
      expect(page).to have_content('Cart | EMPTY')
    end

    scenario 'product with string quantity' do
      within '.add_to_carty_cart' do
        fill_in 'order_item_quantity', with: 'string'
        click_on 'Add to cart'
      end
      expect(page).to have_content('Cart | EMPTY')
    end
  end

  feature 'shows products' do
    let(:book) { create(:book) }

    before do
      book; visit root_path

      click_on 'Add to cart'
      click_on 'Cart'
    end

    scenario 'with subtotal' do
      expect(page).to have_css '#subtotal', text: "#{book.price}"
    end

    scenario 'with product content' do
      expect(page).to have_content book.name
    end

    scenario 'with price' do
      expect(page).to have_content book.price
    end

    scenario 'with quantity field' do
      expect(page).to have_field('order_item_quantity', with: 1)
    end
  end

  feature 'can manage product' do
    let(:book) { create(:book) }
    let(:discount) { create(:engine_discount) }

    before do
      book; visit root_path

      click_on 'Add to cart'
      visit carty_cart_engine.shopping_cart_path
    end

    scenario 'by deleting product from cart' do
      click_on 'X'
      expect(page).to have_content 'Nothing here yet'
    end

    scenario 'by changing quantity of product in cart' do
      fill_in 'order_item_quantity', with: 3
      click_on '⟳'

      expect(page).to have_field 'order_item_quantity', with: 3
      expect(page).to have_content "#{book.price}"

      expect(page).not_to have_css '#subtotal', text: "#{book.price}"
      expect(page).to have_css '#subtotal', text: "#{book.price * 3}"
    end

    scenario 'by clear cart' do
      click_on 'Empty cart'
      expect(page).to have_content 'Nothing here yet'
    end

    scenario 'by adding existing discount to cart' do
      fill_in 'Discount Code', with: discount.code
      click_on 'Update'
      expect(page).not_to have_css('#subtotal', text: "#{book.price}")
    end

    scenario 'by adding wrong discount to cart' do
      fill_in 'Discount Code', with: 'somthing'
      expect(page).to have_css('#subtotal', text: "#{book.price}")
    end
  end

  feature 'checkout' do
    before do
      create(:bike)
      visit root_path
      click_on 'Add to cart'
      visit carty_cart_engine.shopping_cart_path
    end

    scenario 'follows to users checkout path' do
      user_checkout = CartyCartEngine.users_checkout_path

      click_on 'Checkout'
      expect(current_url).to include user_checkout
    end

    scenario 'has no content after checkout' do
      click_on 'Checkout'
      visit carty_cart_engine.shopping_cart_path
      expect(page).to have_content 'Nothing here yet'
    end
  end
end
