import asyncio
import secrets

from db.session import AsyncSessionLocal
from domains.users.models import User
from core.security import hash_password
from core.config import Settings

async def create_admin() -> None:
    username = input("Admin username: ").strip()
    phone_number = input("Phone number (optional, 11 digits): ").strip() or None
    secret = secrets.token_urlsafe(Settings.SECRET_PASS_LENGTH)
    
    async with AsyncSessionLocal() as db:
        try:
            user = User(
                username=username,
                phone_number=phone_number,
                is_admin=True,
                secret_hash=hash_password(secret),
            )

            db.add(user)
            await db.commit()

        except ValueError as exc:
            print(f"\nError: {exc}\n")
            return

    print("\nAdmin created successfully\n")
    print("Username:", user.username)
    print("Phone number:", user.phone_number)
    print("Secret:", secret)
    print("\nSave this secret!\n")


if __name__ == "__main__":
    asyncio.run(create_admin())
