import asyncio
import sys

sys.path.insert(0, ".")

from app.db.session import async_session_factory
from app.models.user import User
from app.core.security import get_password_hash


async def create_admin():
    async with async_session_factory() as session:
        # 检查用户是否已存在
        from sqlalchemy import select

        result = await session.execute(select(User).where(User.username == "linkong"))
        existing_user = result.scalar_one_or_none()

        if existing_user:
            print(f"用户 linkong 已存在，更新密码...")
            existing_user.set_password("LK12345678")
            existing_user.role = "super_admin"
        else:
            print("创建管理员用户...")
            user = User(
                username="linkong",
                email="linkong@example.com",
                password_hash=get_password_hash("LK12345678"),
                role="super_admin",
                is_active=True,
            )
            session.add(user)

        await session.commit()
        print("完成！")


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