from configs.firebase import db


def getShops(credentials=False, platformId=None):
    ref = db.collection("shopsCredentials" if credentials else "shops").where("platformId", "==", platformId).get() if platformId else db.collection("shops").get()
    return [shop.to_dict() for shop in ref]

def saveShop(uid, platformId, enterpriseId, platformShopId, platformName, appId , name, createdAt, updatedAt, url,password = None,**kwargs) -> str:
    shop = getShop(uid, platformId, platformShopId)
    if shop:
        id = shop.get('id')
        shop = dict(
            id=id,
            uid=uid,
            enterpriseId=enterpriseId,
            platformShopId=platformShopId,
            platformId=platformId,
            name=name,
            platformName=platformName,
            createdAt=createdAt,
            updatedAt=updatedAt,
            url=url,
            appId=appId,
            password=password,
            disabled=False,
            **kwargs
        )
        ref = db.collection("shops").document(id).set(shop, merge=True)
    else:
        shop = dict(
            uid=uid,
            enterpriseId=enterpriseId,
            name=name,
            platformName=platformName,
            platformId=platformId,
            platformShopId=platformShopId,
            createdAt=createdAt,
            updated=updatedAt,
            url=url,
            appId=appId,password=password,
            disabled=False,
            **kwargs
        )
        _ , ref = db.collection("shops").add(shop)
        id = ref.id
        shop['id'] = id
        ref.update(dict(id=id))
    saveShopCredentials(shop)
    return shop


def getShop(uid, platformId, platformShopId) -> dict:
    ref = db.collection("shopsCredentials").where("platformShopId", "==", platformShopId).where("platformId", "==", platformId).where("uid", "==", uid).get()
    if ref:
        return ref[0].to_dict()
    return None

def getShopById(id:str, credentials = False):
    ref = db.collection("shops").document(id).get() if not credentials else db.collection("shopsCredentials").document(id).get()
    if ref.exists:
        return ref.to_dict()
    else:
        ref = db.collection("shops").document(id).get()
        if ref.exists:
            shop =  ref.to_dict()
            if shop.get("platformId") == "13":return shop
    return None

def saveShopCredentials(shop):
    db.collection("shopsCredentials").document(shop.get('id')).set(shop, merge=True)
    return shop.get('id')

def getUserShops(uid, credentials=False):
    ref = db.collection('shopsCredentials' if credentials else 'shops').where("uid", '==', uid).get()
    return [r.to_dict() for r in ref]

def getShopByPlatformShopId(platformId,id, credentials=False):
    ref = db.collection('shops' if not credentials else "shopsCredentials").where('platformId', "==", platformId).where("platformShopId", "==", id).get()
    if len(ref)>0:
        return ref[0].to_dict()
    return None

def updateShop(shopId, **kwargs):
    try:
        db.document(f"shopsCredentials/{shopId}").update(dict(
            **kwargs
        ))
    except Exception as e:
        print("Error while updating shop", e)