from .database import *
from .s0_base import *

class User(object):
    def __init__(self, input: dict[str, Any]) -> None:
        self.type = 'user'
        self.id = str(input['user_id'])
        self.name = str(input['username'])
        self.password = str(input['password'])

    def as_dict(self) -> dict[str, Any]:
        return { 'type': self.type, 'id': self.id, 'name': self.name, 'password': self.password }

    def as_id_dict(self) -> dict[str, Any]:
        return { 'type': self.type, 'id': self.id }


def get_user_schema(name: str) -> dict[str, dict[Any, Any]]:
    return { 'id'         : {'type': 'str'      , 'optional': False , 'readonly': True },
             'name'       : {'type': 'str'      , 'optional': False , 'readonly': False},
             'password'   : {'type': 'str'      , 'optional': False , 'readonly': False} }

def get_user(name: str, user_name: str) -> dict[Any, Any]:
    t = try_read(name, f"select * from users where username = '{user_name}'")
    if t == None:
        return {}

    d = {}
    d['id'] = str(t['user_id'])
    d['name'] = str(t['username'])
    # d['password'] = str(t['password'])

    return d

def get_all_users(name: str) -> list[dict[Any, Any]]:
    return read_all(name, "select * from users")

