"""Domain.services.punctuation_style_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 PunctuationFinding:
    """Finding describing punctuation style non-compliance in a line."""

    kind: str  # "ellipsis_style" | "ellipsis_odd_count" | "dash_style"
    line_index: int
    found: str | None = None
    count: int | None = None


class PunctuationStyleService:
    """約物スタイルの統一検出サービス。

    三点リーダ（……）/ダッシュ（――）のスタイル逸脱を検出する。
    """

    def analyze_lines(self, lines: list[str]) -> list[PunctuationFinding]:
        """
        Analyze lines for punctuation style violations.

        Purpose:
            Detect non-standard punctuation marks (ellipsis, dashes) that violate
            Japanese typography conventions.

        Args:
            lines: Text lines to analyze.

        Returns:
            List of PunctuationFinding objects describing violations.

        Side Effects:
            None (read-only analysis).
        """
        findings: list[PunctuationFinding] = []
        for i, raw in enumerate(lines, 1):
            line = (raw or "").strip()
            if not line:
                continue
            # 三点リーダの半端/異スタイル
            if "..." in line or "・・・" in line:
                findings.append(
                    PunctuationFinding(kind="ellipsis_style", line_index=i, found=("..." if "..." in line else "・・・"))
                )
            ellipsis_count = line.count("…")
            if ellipsis_count % 2 == 1:
                findings.append(
                    PunctuationFinding(kind="ellipsis_odd_count", line_index=i, count=ellipsis_count)
                )
            # ダッシュ
            if ("—" in line or "－" in line) and "――" not in line:
                findings.append(PunctuationFinding(kind="dash_style", line_index=i))
        return findings
