# frozen_string_literal: true

require File.join(File.expand_path('../../../../test', __dir__), 'test_helper')

module QualityApp
  class TestMrlChemicalsRequirementInteractor < MiniTestWithHooks
    include MrlChemicalsRequirementFactory
    include MasterfilesApp::CalendarFactory
    include MasterfilesApp::PartyFactory
    include MasterfilesApp::CultivarFactory
    include MasterfilesApp::CommodityFactory
    include MasterfilesApp::QaStandardTypeFactory
    include MasterfilesApp::QaStandardFactory
    include MasterfilesApp::TargetMarketFactory
    include MasterfilesApp::ChemicalFactory
    include MasterfilesApp::MrlRequirementFactory

    def test_repo
      repo = interactor.send(:repo)
      assert repo.is_a?(QualityApp::MrlChemicalsRequirementRepo)
    end

    def test_mrl_chemicals_requirement
      QualityApp::MrlChemicalsRequirementRepo.any_instance.stubs(:find_mrl_chemicals_requirement).returns(fake_mrl_chemicals_requirement)
      entity = interactor.send(:mrl_chemicals_requirement, 1)
      assert entity.is_a?(MrlChemicalsRequirement)
    end

    def test_create_mrl_chemicals_requirement
      attrs = fake_mrl_chemicals_requirement.to_h.reject { |k, _| k == :id }
      res = interactor.create_mrl_chemicals_requirement(attrs)
      assert res.success, "#{res.message} : #{res.errors.inspect}"
      assert_instance_of(MrlChemicalsRequirement, res.instance)
      assert res.instance.id.nonzero?
    end

    def test_create_mrl_chemicals_requirement_fail
      attrs = fake_mrl_chemicals_requirement(id: nil).to_h
      res = interactor.create_mrl_chemicals_requirement(attrs)
      refute res.success, 'should fail validation'
      assert_equal ['must be filled'], res.errors[:id]
    end

    def test_update_mrl_chemicals_requirement
      id = create_mrl_chemicals_requirement
      attrs = interactor.send(:repo).find_hash(:mrl_chemicals_requirements, id).reject { |k, _| k == :id }
      value = attrs[:maximum_residue_level]
      attrs[:maximum_residue_level] = 1
      res = interactor.update_mrl_chemicals_requirement(id, attrs)
      assert res.success, "#{res.message} : #{res.errors.inspect}"
      assert_instance_of(MrlChemicalsRequirement, res.instance)
      assert_equal 1, res.instance.maximum_residue_level
      refute_equal value, res.instance.maximum_residue_level
    end

    def test_update_mrl_chemicals_requirement_fail
      id = create_mrl_chemicals_requirement
      attrs = interactor.send(:repo).find_hash(:mrl_chemicals_requirements, id).reject { |k, _| %i[id maximum_residue_level].include?(k) }
      res = interactor.update_mrl_chemicals_requirement(id, attrs)
      refute res.success, "#{res.message} : #{res.errors.inspect}"
      assert_equal ['is missing'], res.errors[:maximum_residue_level]
    end

    def test_delete_mrl_chemicals_requirement
      id = create_mrl_chemicals_requirement(force_create: true)
      assert_count_changed(:mrl_chemicals_requirements, -1) do
        res = interactor.delete_mrl_chemicals_requirement(id)
        assert res.success, res.message
      end
    end

    private

    def mrl_chemicals_requirement_attrs
      chemical_id = create_chemical
      mrl_requirement_id = create_mrl_requirement

      {
        id: 1,
        chemical_id: chemical_id,
        mrl_requirement_id: mrl_requirement_id,
        percentage_of_eu_level: 1.0,
        maximum_residue_level: 1.0,
        active: true
      }
    end

    def fake_mrl_chemicals_requirement(overrides = {})
      MrlChemicalsRequirement.new(mrl_chemicals_requirement_attrs.merge(overrides))
    end

    def interactor
      @interactor ||= MrlChemicalsRequirementInteractor.new(current_user, {}, {}, {})
    end
  end
end
