"""Authorization value objects used by handlers."""

from __future__ import annotations

from dataclasses import dataclass
from enum import Enum

from .exceptions import DomainValidationError


class Role(str, Enum):
    GUEST = "GUEST"
    ADMIN = "ADMIN"
    READER = "READER"


@dataclass(slots=True, frozen=True)
class Principal:
    name: str
    role: Role
    tenant_id: str

    @classmethod
    def of(cls, name: str, role: Role, tenant_id: str) -> "Principal":
        if not isinstance(name, str) or not name.strip():
            raise DomainValidationError("Principal.name must be a non-empty string")
        if not isinstance(tenant_id, str) or not tenant_id.strip():
            raise DomainValidationError("Principal.tenant_id must be a non-empty string")
        return cls(name=name.strip(), role=role, tenant_id=tenant_id.strip())

    @classmethod
    def guest(cls) -> "Principal":
        return cls(name="guest", role=Role.GUEST, tenant_id="guest")

    @classmethod
    def system(cls) -> "Principal":
        return cls(name="system", role=Role.ADMIN, tenant_id="system")

    def to_dict(self) -> dict[str, str]:
        return {
            "name": self.name,
            "role": self.role.value,
            "tenant_id": self.tenant_id,
        }


__all__ = ["Principal", "Role"]

