from __future__ import annotations

from dataclasses import dataclass, field


@dataclass
class SearchApiPolicy:
    disabled: bool = False
    compliance_approved: bool = False
    allowed_providers: set[str] = field(default_factory=lambda: {"naver"})
    daily_limit: int | None = None
    calls_made: int = 0

    def can_call(self, provider: str, requested_calls: int = 1) -> tuple[bool, str | None]:
        if self.disabled:
            return False, "search API disabled"
        if not self.compliance_approved:
            return False, "search API compliance not approved"
        if provider not in self.allowed_providers:
            return False, "search provider not allowed"
        calls = max(1, int(requested_calls or 1))
        if self.daily_limit is not None and self.calls_made + calls > self.daily_limit:
            return False, "search API usage limit reached"
        return True, None

    def record_call(self, count: int = 1) -> None:
        self.calls_made += max(1, int(count or 1))
