"""Tests for JWT claims."""

import pytest

from vikdata_auth.jwt import TokenClaims


class TestTokenClaims:
    """Tests for TokenClaims dataclass."""

    def test_from_payload_basic(self):
        """Create TokenClaims from basic payload."""
        payload = {
            "sub": "user-123",
            "tenant_id": "tenant-456",
            "email": "test@example.com",
            "name": "Test User",
            "permissions": ["vessels:read", "vessels:write"],
            "roles": ["viewer", "editor"],
            "exp": 1700000000,
            "iat": 1699996400,
            "iss": "https://api.vikdata.com",
            "aud": "vikdata",
            "jti": "token-789",
            "entity_type": "user",
        }

        claims = TokenClaims.from_payload(payload)

        assert claims.sub == "user-123"
        assert claims.tenant_id == "tenant-456"
        assert claims.email == "test@example.com"
        assert claims.name == "Test User"
        assert claims.permissions == ["vessels:read", "vessels:write"]
        assert claims.roles == ["viewer", "editor"]
        assert claims.exp == 1700000000
        assert claims.iat == 1699996400
        assert claims.iss == "https://api.vikdata.com"
        assert claims.aud == "vikdata"
        assert claims.jti == "token-789"
        assert claims.entity_type == "user"

    def test_from_payload_minimal(self):
        """Create TokenClaims from minimal payload."""
        payload = {
            "sub": "user-123",
            "tenant_id": "tenant-456",
            "exp": 1700000000,
            "iat": 1699996400,
            "iss": "https://api.vikdata.com",
            "aud": "vikdata",
        }

        claims = TokenClaims.from_payload(payload)

        assert claims.sub == "user-123"
        assert claims.tenant_id == "tenant-456"
        assert claims.email == ""  # Default
        assert claims.name is None  # Default
        assert claims.permissions == []  # Default
        assert claims.roles == []  # Default
        assert claims.jti is None  # Default
        assert claims.entity_type == "user"  # Default

    def test_user_id_alias(self):
        """user_id property is alias for sub."""
        payload = {
            "sub": "user-123",
            "tenant_id": "tenant-456",
            "exp": 1700000000,
            "iat": 1699996400,
            "iss": "https://api.vikdata.com",
            "aud": "vikdata",
        }

        claims = TokenClaims.from_payload(payload)
        assert claims.user_id == claims.sub == "user-123"

    def test_is_user(self):
        """is_user property returns True for user entity type."""
        payload = {
            "sub": "user-123",
            "tenant_id": "tenant-456",
            "exp": 1700000000,
            "iat": 1699996400,
            "iss": "https://api.vikdata.com",
            "aud": "vikdata",
            "entity_type": "user",
        }

        claims = TokenClaims.from_payload(payload)
        assert claims.is_user is True
        assert claims.is_api_key is False

    def test_is_api_key(self):
        """is_api_key property returns True for api_key entity type."""
        payload = {
            "sub": "api-key-123",
            "tenant_id": "tenant-456",
            "exp": 1700000000,
            "iat": 1699996400,
            "iss": "https://api.vikdata.com",
            "aud": "vikdata",
            "entity_type": "api_key",
        }

        claims = TokenClaims.from_payload(payload)
        assert claims.is_api_key is True
        assert claims.is_user is False

    def test_audience_can_be_list(self):
        """Audience can be a list of strings."""
        payload = {
            "sub": "user-123",
            "tenant_id": "tenant-456",
            "exp": 1700000000,
            "iat": 1699996400,
            "iss": "https://api.vikdata.com",
            "aud": ["vikdata", "fishinfo"],
        }

        claims = TokenClaims.from_payload(payload)
        assert claims.aud == ["vikdata", "fishinfo"]
