"""Mail API çağrıları için thread-safe sayaçlar."""
from __future__ import annotations

from dataclasses import dataclass
import threading


@dataclass
class MailApiStats:
    """Mail sipariş ve kontrol sonuçlarına ilişkin sayaçlar."""

    order_attempts: int = 0
    order_successes: int = 0
    order_failures: int = 0
    check_attempts: int = 0
    check_successes: int = 0
    check_failures: int = 0

    def copy(self) -> "MailApiStats":
        """Sayaçların kopyasını döndür."""

        return MailApiStats(
            order_attempts=self.order_attempts,
            order_successes=self.order_successes,
            order_failures=self.order_failures,
            check_attempts=self.check_attempts,
            check_successes=self.check_successes,
            check_failures=self.check_failures,
        )


_STATS = MailApiStats()
_LOCK = threading.Lock()


def record_mail_order_result(success: bool) -> None:
    """Bir mail siparişi sonucunu kaydet."""

    with _LOCK:
        _STATS.order_attempts += 1
        if success:
            _STATS.order_successes += 1
        else:
            _STATS.order_failures += 1


def record_mail_check_result(success: bool) -> None:
    """Bir mail kontrolü sonucunu kaydet."""

    with _LOCK:
        _STATS.check_attempts += 1
        if success:
            _STATS.check_successes += 1
        else:
            _STATS.check_failures += 1


def get_mail_stats() -> MailApiStats:
    """Mail API sayaçlarının anlık görüntüsünü döndür."""

    with _LOCK:
        return _STATS.copy()


def reset_mail_stats() -> None:
    """Sayaçları sıfırla (testler için)."""

    with _LOCK:
        global _STATS
        _STATS = MailApiStats()


__all__ = [
    "MailApiStats",
    "get_mail_stats",
    "record_mail_check_result",
    "record_mail_order_result",
    "reset_mail_stats",
]
