import json
from typing import Any
from models.database import Database

class User:
    def __init__(self, db: Database):
        self.db = db

    async def create(self, user_id: int, username: str, language: str, referred_id: int = None):
        if username is None:
            username = 'None'
        query = """
        INSERT INTO users (user_id, username, language, referral_link, referred_id) 
        VALUES (?, ?, ?, ?, ?)
        """
        await self.db.execute(query, (user_id, username, language, f'?start={user_id}', referred_id))

    async def get_by_user_id(self, user_id: int):
        query = "SELECT * FROM users WHERE user_id = ?"
        return await self.db.fetchone(query, (user_id,))

    async def update_user(self, user_id: int, column: str, value: Any):
        query = f"UPDATE users SET {column} = ? WHERE user_id = ?"
        return await self.db.execute(query, (value, user_id))
