"""Seed admin user for YakPanel"""
import asyncio
import sys
import os

# Run from backend directory
backend_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.chdir(backend_dir)
sys.path.insert(0, backend_dir)

from sqlalchemy import select
from app.core.database import AsyncSessionLocal, init_db
from app.core.security import get_password_hash
from app.models.user import User


async def seed():
    await init_db()
    async with AsyncSessionLocal() as db:
        result = await db.execute(select(User).where(User.username == "admin"))
        if result.scalar_one_or_none():
            print("Admin user already exists")
            return
        admin = User(
            username="admin",
            password=get_password_hash("admin"),
            is_superuser=True,
        )
        db.add(admin)
        await db.commit()
        print("Admin user created: username=admin, password=admin")


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