from fastapi import FastAPI
from pydantic import BaseModel
from krrkt.engine.pipeline import proofread
from krrkt.rules.loader import load_rules

app = FastAPI(title="krrkt", description="Russian text quality checker")


class ProofreadRequest(BaseModel):
    text: str
    skip_llm: bool = False
    model: str | None = None


class FindingResponse(BaseModel):
    start: int
    end: int
    word: str
    rule: str
    section: str = ""
    description: str = ""
    weight: int = 100
    style: str = "red"
    tab: str = "red"
    source: str = "fast"


class ProofreadResponse(BaseModel):
    score: float
    score_color: str
    words: int
    sentences: int
    stopwords: int
    findings: list[FindingResponse]
    summary: dict


@app.post("/api/v1/proofread", response_model=ProofreadResponse)
async def api_proofread(request: ProofreadRequest):
    result = await proofread(
        text=request.text,
        skip_llm=request.skip_llm,
        model=request.model,
    )
    return ProofreadResponse(
        score=result.score,
        score_color=result.score_color,
        words=result.words,
        sentences=result.sentences,
        stopwords=result.stopwords,
        findings=[FindingResponse(**f.__dict__) for f in result.findings],
        summary=result.summary,
    )


@app.get("/api/v1/rules")
def api_rules():
    return load_rules()


@app.get("/api/v1/health")
def api_health():
    return {"status": "ok"}
