import uuid
import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from config.db_config import DatabaseConfig
import mysql.connector


def generate_and_store_token(user_id):
    token = str(uuid.uuid4())

    conn = mysql.connector.connect(
        host=DatabaseConfig.DB_HOST,
        port=DatabaseConfig.DB_PORT,
        user=DatabaseConfig.DB_USER,
        password=DatabaseConfig.DB_PASSWORD,
        database=DatabaseConfig.DB_NAME
    )

    cursor = conn.cursor()

    cursor.execute(
        "UPDATE users SET access_token=%s WHERE id=%s",
        (token, user_id)
    )

    conn.commit()
    cursor.close()
    conn.close()

    return token