from server.backend.schema.pydantic import settings, UserCreate
from server.backend.database.db import create_user, list_users
import uvicorn
import asyncio
import argparse

async def init_admin(user_name: str):
    admin_user = {
        "code": "123456",
        "name": user_name,
        "middlename": user_name,
        "surname": user_name,
        "admin": True
    }

    users = await list_users() or []
    label = any(u.admin for u in users)

    if not label:
        await create_user(UserCreate(**admin_user))

def start(log_level: str):
    uvicorn.run(
        "server.backend.endpoints.endpoints:api",
        host="0.0.0.0",
        port=settings.PORT,
        reload=False,
        log_level=log_level,
        access_log=True
    )

parser = argparse.ArgumentParser(description="logging and admin creation")
parser.add_argument(
    "--mode",
    choices=["debug", "info"],
    default="info",
    help="Режим логирования"
)
parser.add_argument(
    "--user_name",
    type=str,
    required=True,
    help="Создание первого пользователя"
)
args = parser.parse_args()

if __name__ == "__main__":
    asyncio.run(init_admin(args.user_name))

    print("Режим:", args.mode)

    start(args.mode)