"""
Strategy Domain Service — Scorecard Calculator.
"""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import List, Optional
from uuid import UUID


@dataclass
class PerspectiveScore:
    """نتیجه محاسبه امتیاز یک دیدگاه."""
    perspective_id: UUID
    name: str
    weight: float
    score: float
    weighted_score: float
    measure_count: int


@dataclass
class ScorecardReport:
    """گزارش کامل کارت امتیازی."""
    scorecard_id: UUID
    overall_score: float
    perspectives: List[PerspectiveScore] = field(default_factory=list)


class ScorecardCalculator:
    """محاسبه‌گر امتیاز کارت امتیازی متوازن."""

    @staticmethod
    def calculate_perspective_score(
        measures: list,
    ) -> float:
        """محاسبه امتیاز وزنی دیدگاه بر اساس سنجه‌ها.

        Args:
            measures: لیست ScorecardMeasure entities
        Returns:
            امتیاز وزنی (0-100)
        """
        if not measures:
            return 0.0

        total_weight = sum(m.weight for m in measures)
        if total_weight == 0:
            return 0.0

        weighted_sum = 0.0
        for m in measures:
            m.calculate_score()
            weighted_sum += m.score * m.weight

        return weighted_sum / total_weight

    @staticmethod
    def calculate_overall_score(
        perspective_scores: List[PerspectiveScore],
    ) -> float:
        """محاسبه امتیاز کلی کارت امتیازی بر اساس دیدگاه‌ها.

        Args:
            perspective_scores: لیست امتیازهای دیدگاه‌ها
        Returns:
            امتیاز کلی وزنی (0-100)
        """
        if not perspective_scores:
            return 0.0

        total_weight = sum(ps.weight for ps in perspective_scores)
        if total_weight == 0:
            return 0.0

        weighted_sum = sum(ps.score * ps.weight for ps in perspective_scores)
        return weighted_sum / total_weight

    @staticmethod
    def generate_report(
        scorecard,
        perspectives: list,
        measures_by_perspective: dict,
    ) -> ScorecardReport:
        """تولید گزارش کامل کارت امتیازی.

        Args:
            scorecard: Scorecard entity
            perspectives: لیست ScorecardPerspective entities
            measures_by_perspective: dict[perspective_id → list[ScorecardMeasure]]
        Returns:
            ScorecardReport
        """
        p_scores = []
        for p in perspectives:
            measures = measures_by_perspective.get(p.id, [])
            score = ScorecardCalculator.calculate_perspective_score(measures)
            weighted = score * p.weight
            p_scores.append(PerspectiveScore(
                perspective_id=p.id,
                name=p.name,
                weight=p.weight,
                score=score,
                weighted_score=weighted,
                measure_count=len(measures),
            ))

        overall = ScorecardCalculator.calculate_overall_score(p_scores)

        return ScorecardReport(
            scorecard_id=scorecard.id,
            overall_score=overall,
            perspectives=p_scores,
        )
