# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks for proper shared_context and shared_examples usage.
      #
      # If there are no examples defined, use shared_context.
      # If there is no setup defined, use shared_examples.
      #
      # With `Strict: true`, `shared_context` is flagged whenever it contains
      # any examples, even if it also contains setup code.
      #
      # @example
      #   # bad
      #   RSpec.shared_context 'only examples here' do
      #     it 'does x' do
      #     end
      #
      #     it 'does y' do
      #     end
      #   end
      #
      #   # good
      #   RSpec.shared_examples 'only examples here' do
      #     it 'does x' do
      #     end
      #
      #     it 'does y' do
      #     end
      #   end
      #
      # @example
      #   # bad
      #   RSpec.shared_examples 'only setup here' do
      #     subject(:foo) { :bar }
      #
      #     let(:baz) { :bazz }
      #
      #     before do
      #       something
      #     end
      #   end
      #
      #   # good
      #   RSpec.shared_context 'only setup here' do
      #     subject(:foo) { :bar }
      #
      #     let(:baz) { :bazz }
      #
      #     before do
      #       something
      #     end
      #   end
      #
      # @example Strict: true
      #   # bad - shared_context with examples is flagged
      #   RSpec.shared_context 'setup and examples' do
      #     let(:foo) { :bar }
      #
      #     it 'does x' do
      #     end
      #   end
      #
      #   # good - split into separate shared_context and shared_examples
      #   RSpec.shared_context 'setup' do
      #     let(:foo) { :bar }
      #   end
      #
      #   RSpec.shared_examples 'examples' do
      #     it 'does x' do
      #     end
      #   end
      #
      class SharedContext < Base
        extend AutoCorrector

        MSG_EXAMPLES = "Use `shared_examples` when you don't define context."
        MSG_EXAMPLES_STRICT = 'Use `shared_examples` when you define examples.'
        MSG_CONTEXT = "Use `shared_context` when you don't define examples."

        # @!method examples?(node)
        def_node_search :examples?, <<~PATTERN
          (send nil? {#Includes.examples #Examples.all} ...)
        PATTERN

        # @!method context?(node)
        def_node_search :context?, <<~PATTERN
          (send nil?
            {#Subjects.all #Helpers.all #Includes.context #Hooks.all} ...
          )
        PATTERN

        # @!method shared_context(node)
        def_node_matcher :shared_context, <<~PATTERN
          (block (send #rspec? #SharedGroups.context ...) ...)
        PATTERN

        # @!method shared_example(node)
        def_node_matcher :shared_example, <<~PATTERN
          (block (send #rspec? #SharedGroups.examples ...) ...)
        PATTERN

        def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
          offending_node(node) do
            add_offense(node.send_node, message: message) do |corrector|
              if can_correct?(node)
                corrector.replace(node.send_node.loc.selector,
                                  'shared_examples')
              end
            end
          end

          examples_with_only_context(node) do
            add_offense(node.send_node, message: MSG_CONTEXT) do |corrector|
              corrector.replace(node.send_node.loc.selector, 'shared_context')
            end
          end
        end

        private

        def strict?
          cop_config.fetch('Strict', false)
        end

        def message
          strict? ? MSG_EXAMPLES_STRICT : MSG_EXAMPLES
        end

        def can_correct?(node)
          !strict? || !context?(node)
        end

        def offending_node(node)
          shared_context(node) do
            yield if examples?(node) && (strict? || !context?(node))
          end
        end

        def examples_with_only_context(node)
          shared_example(node) { yield if context?(node) && !examples?(node) }
        end
      end
    end
  end
end
