from typing import Optional

from keycloak import KeycloakOpenID, KeycloakAdmin, KeycloakOpenIDConnection

from core.environment import Environment


class Keycloak:
    client: Optional[KeycloakOpenID] = None
    admin: Optional[KeycloakAdmin] = None

    @classmethod
    def init(cls):
        cls.client = KeycloakOpenID(
            server_url=Environment.get("KEYCLOAK_URL", str),
            client_id=Environment.get("KEYCLOAK_CLIENT_ID", str),
            realm_name=Environment.get("KEYCLOAK_REALM", str),
            client_secret_key=Environment.get("KEYCLOAK_CLIENT_SECRET", str),
        )
        connection = KeycloakOpenIDConnection(
            server_url=Environment.get("KEYCLOAK_URL", str),
            client_id=Environment.get("KEYCLOAK_CLIENT_ID", str),
            realm_name=Environment.get("KEYCLOAK_REALM", str),
            client_secret_key=Environment.get("KEYCLOAK_CLIENT_SECRET", str),
        )
        cls.admin = KeycloakAdmin(connection=connection)
