from fastapi import APIRouter, BackgroundTasks
import backend_fastapi.schemas as schemas
from backend_fastapi.repositories import get_stored_roles, add_role, add_user, get_role_all, update_role, delete_role, update_user,delete_user,get_users
from typing import List
router = APIRouter()

@router.post("/role")
async def create_role(role: schemas.RoleCreate) -> schemas.Role:
    return await add_role(role)

@router.get("/stored_role")
async def get_stored_role() -> List[schemas.Role]:
    return await get_stored_roles()

@router.get("/role")
async def get_role(limit:int=10, page:int=0) -> List[schemas.Role]:
    return await get_role_all(limit, page)

@router.patch("/role")
async def change_role(role: schemas.Role, id: int) -> None:
    return await update_role(role, id)

@router.delete("/role")
async def remove_role(id: int) -> schemas.Role:
    return await delete_role(id)

@router.post("/user")
async def create_user(user: schemas.UserCreate) -> schemas.User:
    import hashlib
    user.hashed_password = hashlib.sha256(user.hashed_password.encode('utf-8')).hexdigest()
    return await add_user(user)


@router.get('/user')
async def show_users(limit:int=10, page:int=0):
    return await get_users(limit, page)

@router.patch('/user')
async def change_users(user: schemas.User, id: int):
    import hashlib
    if user.hashed_password:
        user.hashed_password = hashlib.sha256(user.hashed_password.encode('utf-8')).hexdigest()
    return await update_user(user, id)

@router.delete('/user')
async def remove_users(id: int):
    return await delete_user(id)