"""OAuth2 config."""

import os
from pathlib import Path

import msgspec
import msgspec.json


class OAuthConfig(msgspec.Struct):
    """OAuth config."""

    client_id: str
    client_secret: str
    audience: str
    scope: str

    authorization_url: str
    token_endpoint_url: str
    device_authorization_url: str
    jwks_url: str


def load_oauth_config() -> OAuthConfig:
    """Load OAuth config."""

    oauth_config_file_path = Path(__file__).with_name("oauth-config.json")

    if "SLICES_OAUTH_CONFIG_OVERRIDE" in os.environ:
        oauth_config_file_path = Path(os.environ["SLICES_OAUTH_CONFIG_OVERRIDE"])

    with oauth_config_file_path.open() as f:
        oauth_config: OAuthConfig = msgspec.json.decode(f.read(), type=OAuthConfig)

    return oauth_config


settings = load_oauth_config()
