import pytest
from fastapi.testclient import TestClient
from krrkt.api.app import app


client = TestClient(app)


class TestHealthEndpoint:
    def test_health(self):
        response = client.get("/api/v1/health")
        assert response.status_code == 200
        assert response.json()["status"] == "ok"


class TestRulesEndpoint:
    def test_returns_rules(self):
        response = client.get("/api/v1/rules")
        assert response.status_code == 200
        data = response.json()
        assert "categories" in data


class TestProofreadEndpoint:
    def test_proofread_clean_text(self):
        response = client.post("/api/v1/proofread", json={
            "text": "Завод выпускает подшипники.",
            "skip_llm": True,
        })
        assert response.status_code == 200
        data = response.json()
        assert "score" in data
        assert "findings" in data
        assert data["score"] >= 8

    def test_proofread_dirty_text(self):
        response = client.post("/api/v1/proofread", json={
            "text": "Наша компания является лидером рынка и предлагает широкий спектр услуг.",
            "skip_llm": True,
        })
        assert response.status_code == 200
        data = response.json()
        assert data["score"] < 8
        assert len(data["findings"]) > 0

    def test_proofread_empty_text(self):
        response = client.post("/api/v1/proofread", json={
            "text": "",
            "skip_llm": True,
        })
        assert response.status_code == 200
        assert response.json()["score"] == 0

    def test_findings_structure(self):
        response = client.post("/api/v1/proofread", json={
            "text": "Широкий спектр услуг высочайшего качества.",
            "skip_llm": True,
        })
        data = response.json()
        for f in data["findings"]:
            assert "start" in f
            assert "end" in f
            assert "word" in f
            assert "rule" in f
            assert "weight" in f
            assert "source" in f
