from global_conf import CONFIG
from db_logic import collections

import datetime


def store_new_user(dicks_collection, message, result ):
    
    try:
        novenkiy = {
            'user_id':       message.from_user.id,
            'user_fullname': message.from_user.full_name,
            'dick_size':     result,
            'datetimes':     datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            'attempts':      1,
            'chat_id':       message.chat.id
        }
        metadata = dicks_collection.insert(novenkiy, overwrite_mode='update')

        print(f'Успешно добавлен нового пользователь @{message.from_user.username}') 
    except Exception as e2:
        print(f'ошибка DB в /dick :: добавление нового пользователя @{message.from_user.username}')
        print(e2)


def update_attempts(dicks_collection, message, user ):
    try:
        metadata = dicks_collection.insert(
            {
                '_key':          user['_key'], ### этот нужен чтобы апдейт прилетал тому же юзеру
                'user_id':       user['user_id'] ,
                'user_fullname': message.from_user.full_name,
                'dick_size':     user['dick_size'] ,
                'datetimes':     user['datetimes'] ,
                'attempts':      user['attempts'] + 1,
                'chat_id':       message.chat.id
            },
            overwrite_mode='update'
        )
        print(f'Успешно апдейтнули попытку крутить пипису @{message.from_user.username}') 
    except Exception as e2:
        print(f'ошибка DB в /dick :: обновление попытки крутить пипису @{message.from_user.username}')
        print(e2) 


def update_dick_size(dicks_collection, message, user, updatedDick ):
   
    try:
        metadata = dicks_collection.insert(
            {
                '_key':          user['_key'], ### этот нужен чтобы апдейт прилетал тому же юзеру
                'user_id':       user['user_id'],
                'user_fullname': message.from_user.full_name,
                'dick_size':     updatedDick,
                'datetimes':     datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                'attempts':      user['attempts'] + 1,
                'chat_id':       message.chat.id
            },
            overwrite_mode='update'
        )
        print(f'Успешно апдейтнули пипису @{message.from_user.username}') 
    except Exception as e2:
        print(f'ошибка DB в /dick :: обновление пиписы @{message.from_user.username}')
        print(e2)


def get_user(dicks_collection, message):
    try:
        #### Чекнуть есть ли юзер в базе
        candidate_cursor = dicks_collection.find(
            {
                'user_id': message.from_user.id,
                'chat_id': message.chat.id
            },
            skip  = 0, 
            limit = 1488
        )
        if candidate_cursor.count() > 0:  
            user = candidate_cursor.pop()
        else:
            user = None

        return user
    except Exception as e: 
        print('ошибка DB в /dick :: поиск юзера')
        print(e)