from msal import ConfidentialClientApplication
import os
from azure.identity.aio import ClientSecretCredential
from msgraph import GraphServiceClient

def get_client():

    credentials = ClientSecretCredential(
        os.getenv("TENANT_ID"),
        os.getenv("CLIENT_ID"),
        os.getenv("CLIENT_SECRET"),
    )
    scopes = ['https://graph.microsoft.com/.default']
    client = GraphServiceClient(credentials=credentials, scopes=scopes)

    return client

async def get_access_token():
    credentials = ClientSecretCredential(
        os.getenv("TENANT_ID"),
        os.getenv("CLIENT_ID"),
        os.getenv("CLIENT_SECRET"),
    )
    scopes = ['https://graph.microsoft.com/.default']
    token = await credentials.get_token('https://graph.microsoft.com/.default')
    return token.token

def get_sp_client():
    from office365.sharepoint.client_context import ClientContext
    from office365.runtime.auth.client_credential import ClientCredential

    # Dati dell'App Registrata
    client_id = os.getenv("SP_CLIENT_ID")
    client_secret = os.getenv("SP_CLIENT_SECRET")

    site_url = "https://webearitsrl.sharepoint.com/sites/cdg"

    # Autenticazione con Client Credentials
    ctx = ClientContext(site_url).with_credentials(ClientCredential(client_id, client_secret))

    return ctx
