from __future__ import annotations

from collections.abc import Mapping

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from src.database import SessionLocal
from src.models.user import User


def build_seed_user() -> Mapping[str, str | None]:
    return {
        "email": "dev@headquarter.local",
        "name": "Development User",
        "authentik_id": "dev-authentik-user",
        "avatar_url": None,
    }


async def seed_database(session: AsyncSession) -> User:
    payload = build_seed_user()
    existing_user = await session.scalar(select(User).where(User.email == payload["email"]))

    if existing_user is not None:
        return existing_user

    user = User(**payload)
    session.add(user)
    await session.commit()
    await session.refresh(user)
    return user


async def run() -> None:
    async with SessionLocal() as session:
        user = await seed_database(session)
        print({"user_id": str(user.id), "email": user.email})


def main() -> None:
    import asyncio

    asyncio.run(run())


if __name__ == "__main__":
    main()
