import base64

from oauthlib.common import urlencode
from requests_oauthlib import OAuth2Session

from configs.firebase import SANDBOX


def auth(data):
    hostname = data.get('hostname')
    """
    Generates the url for the user to authenticate with ebay.
    """
    # uthorisation = f"Basic {base64.b64encode('AwearLLC-Riverr-PRD-3a5abeb3b-390d6768:PRD-a5abeb3b96df-7cc5-426b-b8a2-25f3'.encode('utf-8'))}"
    oauth = OAuth2Session(
        client_id="AwearLLC-Riverr-SBX-27ca6ee8c-fa6ba91b",
        scope="https://api.ebay.com/oauth/api_scope/sell.account.readonly",
        redirect_uri=f"app.riverr.app/shops",
        # prompt='login'
     )
    res = oauth.authorization_url(
        "https://auth.sandbox.ebay.com/oauth2/authorize",
    )
    print(res)
    return res


# def token(data):
#     hostname = data.get('hostname')
#     currentUser = data.get('currentUser')
#     uid, enterpriseId = currentUser.get('uid'), currentUser.get('enterpriseId')
#     code = data.get('code')
#     oauth = OAuth2Session(
#         client_id="AwearLLC-Riverr-SBX-27ca6ee8c-fa6ba91b" if SANDBOX else "AwearLLC-Riverr-PRD-3a5abeb3b-390d6768",
        
import base64
import hashlib


def merchantDelete(data):
    enterpriseId = data.get("id")
    challengeCode = data.get("challenge_code")
    endpoint = f"https://riverr-enterprise-integrations-dot-riverr-enterprise-user.uc.r.appspot.com/ebay/merchant-delete/{enterpriseId}"
    verificationToken = enterpriseId+enterpriseId
    m = hashlib.sha256((challengeCode+verificationToken+endpoint).encode('utf-8'))
    return dict(
        challengeResponse=m.hexdigest()
    )