from __future__ import annotations

from app.core.agent.processes.v2.intent_router.models import QueryFeatures


class V2ConfidenceAdjuster:
    def adjust(self, confidence: float, features: QueryFeatures) -> float:
        adjusted = confidence
        if not self._has_strong_anchor(features):
            adjusted -= 0.1
        if self._is_short_or_vague(features):
            adjusted -= 0.1
        if self._has_explicit_signal(features):
            adjusted += 0.05
        return min(max(adjusted, 0.0), 1.0)

    def _has_strong_anchor(self, features: QueryFeatures) -> bool:
        return any((features.file_markers, features.endpoint_paths, features.target_doc_hints, features.matched_aliases))

    def _is_short_or_vague(self, features: QueryFeatures) -> bool:
        token_count = len([token for token in features.normalized_query.split() if token.strip()])
        return token_count <= 3 or len(features.target_terms) <= 1

    def _has_explicit_signal(self, features: QueryFeatures) -> bool:
        return bool(features.file_markers or features.endpoint_paths or features.endpoint_markers)
