from krrkt.engine.fast_layer import FastLayer


class TestFastLayerMorphology:
    def setup_method(self):
        self.layer = FastLayer()

    def test_finds_lemma_in_different_form(self):
        findings = self.layer.analyze("Уникальные решения для бизнеса.")
        rules = [f["rule"] for f in findings]
        assert "Необъективная оценка" in rules

    def test_finds_verb_lemma(self):
        findings = self.layer.analyze("Компания являлась лидером.")
        words = [f["word"].lower() for f in findings]
        assert any("являлась" in w for w in words)

    def test_finds_amplifier_forms(self):
        # "очень" should match as-is (lemma = очень)
        findings = self.layer.analyze("Это очень важный продукт.")
        rules = [f["rule"] for f in findings]
        assert "Усилитель" in rules

    def test_phrase_takes_priority_over_lemma(self):
        # "широкий спектр" as phrase should not also match "широкий" as individual lemma
        text = "Предлагаем широкий спектр услуг."
        findings = self.layer.analyze(text)
        phrase_findings = [f for f in findings if "спектр" in f["word"].lower()]
        assert len(phrase_findings) == 1  # Only the phrase, not individual words

    def test_detects_participle(self):
        # "сделанный" is a PRTF participle
        findings = self.layer.analyze("Сделанный проект был хорош.")
        rules = [f["rule"] for f in findings]
        assert "Возможно, причастие" in rules
        participles = [f for f in findings if f["rule"] == "Возможно, причастие"]
        assert any("сделанный" in f["word"].lower() for f in participles)

    def test_detects_gerund(self):
        # "принимая" is a GRND gerund
        findings = self.layer.analyze("Принимая решение, мы учли факторы.")
        rules = [f["rule"] for f in findings]
        assert "Возможно, деепричастие" in rules
        gerunds = [f for f in findings if f["rule"] == "Возможно, деепричастие"]
        assert any("принимая" in f["word"].lower() for f in gerunds)

    def test_participle_not_in_phrase_overlap(self):
        # If a word is already matched as a phrase, it should not also
        # be flagged as a participle
        text = "Принимая во внимание факторы."
        findings = self.layer.analyze(text)
        # "Принимая во внимание" is a phrase (Канцеляризм) — "Принимая"
        # should not also appear as a separate gerund
        gerunds = [f for f in findings if f["rule"] == "Возможно, деепричастие"]
        assert not any("принимая" in f["word"].lower() for f in gerunds)

    def test_detects_okazyvat_lemma(self):
        # "оказывают" should be detected via "оказывать" lemma
        findings = self.layer.analyze("Мы оказывают помощь.")
        words = [f["word"].lower() for f in findings]
        assert "оказывают" in words

    def test_mixed_problems_case(self):
        # BAD case #05: should find at least 4 issues
        text = "Высококвалифицированные специалисты нашей динамично развивающейся компании оказывают широкий спектр услуг."
        findings = self.layer.analyze(text)
        rules = [f["rule"] for f in findings]
        assert "Корпоративный штамп" in rules
        assert "Фраза с отглагольным существительным" in rules
        assert len(findings) >= 4
