"""vikdata-auth: Standalone JWT validation SDK for vikdata platform.

This SDK provides JWT validation via JWKS with no database dependencies.
It follows the industry-standard approach used by Google, AWS, and Azure.

Core Features:
- JWKS fetching with intelligent caching (1 hour TTL)
- RS256 JWT validation with automatic key rotation support
- Permission matching with wildcard support
- FastAPI middleware and decorators (optional dependency)

Basic Usage:
    from vikdata_auth import JWTValidator, has_permission

    # Create validator (typically once at startup)
    validator = JWTValidator(
        jwks_url="https://api.vikdata.com/.well-known/jwks.json",
    )

    # Validate a token
    claims = await validator.validate(token)
    print(f"User: {claims.user_id}")
    print(f"Tenant: {claims.tenant_id}")

    # Check permissions
    if has_permission(claims.permissions, "vessels:read"):
        print("User can read vessels")

FastAPI Middleware (requires 'fastapi' extra):
    from fastapi import FastAPI
    from vikdata_auth import JWTValidationMiddleware

    app = FastAPI()
    app.add_middleware(
        JWTValidationMiddleware,
        jwks_url="https://api.vikdata.com/.well-known/jwks.json",
    )

FastAPI Decorators:
    from vikdata_auth import require_permission

    @router.get("/vessels")
    async def list_vessels(
        _: None = require_permission("vessels:read"),
    ):
        return {"vessels": [...]}
"""

from .exceptions import (
    AuthenticationError,
    AuthorizationError,
    JWKSError,
    VikdataAuthError,
)
from .jwt import JWKSClient, JWTValidator, TokenClaims
from .permissions import (
    has_all_permissions,
    has_any_permission,
    has_permission,
    is_superadmin,
    match_permission,
    parse_permission,
)

# Core exports (always available)
__all__ = [
    # JWT validation
    "JWKSClient",
    "JWTValidator",
    "TokenClaims",
    # Permission matching (namespaced format: namespace.resource:action)
    "parse_permission",
    "match_permission",
    "has_permission",
    "has_any_permission",
    "has_all_permissions",
    "is_superadmin",
    # Exceptions
    "VikdataAuthError",
    "AuthenticationError",
    "AuthorizationError",
    "JWKSError",
]

# Try to import FastAPI components (optional dependency)
try:
    from .decorators import (
        RateLimiter,
        rate_limit,
        require_all_permissions,
        require_any_permission,
        require_any_role,
        require_permission,
        require_role,
        require_tenant_match,
    )
    from .middleware import (
        AuthDependency,
        JWTValidationMiddleware,
        get_current_user,
        get_tenant_id,
        get_user_id,
    )

    __all__.extend(
        [
            # Middleware
            "JWTValidationMiddleware",
            "AuthDependency",
            "get_current_user",
            "get_tenant_id",
            "get_user_id",
            # Decorators
            "require_permission",
            "require_any_permission",
            "require_all_permissions",
            "require_role",
            "require_any_role",
            "require_tenant_match",
            "rate_limit",
            "RateLimiter",
        ]
    )
except ImportError:
    # FastAPI not installed - middleware/decorators not available
    pass

__version__ = "0.1.0"
