"""Domain.services.ending_repetition_service
Where: Domain service module providing reusable business logic.
What: Houses service routines and helpers implemented in this module.
Why: Makes this service behaviour accessible to application workflows.
"""

from __future__ import annotations

from dataclasses import dataclass


@dataclass(frozen=True)
class EndingRepetitionFinding:
    """Finding representing repeated sentence endings over a threshold window."""

    ending: str
    count: int
    start_sentence_index: int
    end_sentence_index: int


class EndingRepetitionService:
    """Detect clusters of identical sentence endings for editorial feedback."""

    def _ending(self, s: str) -> str:
        """Return the trailing characters used for repetition comparison."""

        s = (s or "").strip()
        if not s:
            return ""
        return s[-2:]

    def analyze(self, sentences: list[str], *, threshold: int) -> list[EndingRepetitionFinding]:
        """Identify sequences where sentence endings repeat at least `threshold` times."""

        out: list[EndingRepetitionFinding] = []
        cur: str | None = None
        count = 0
        start_idx = 0
        for idx, s in enumerate(sentences):
            e = self._ending(s)
            if e == cur and e:
                count += 1
            else:
                if cur and count >= threshold:
                    out.append(
                        EndingRepetitionFinding(ending=cur, count=count, start_sentence_index=start_idx, end_sentence_index=idx - 1)
                    )
                cur = e
                count = 1
                start_idx = idx
        if cur and count >= threshold:
            out.append(EndingRepetitionFinding(ending=cur, count=count, start_sentence_index=start_idx, end_sentence_index=len(sentences) - 1))
        return out
