"""User service — business logic (Tortoise queries).

Fix from slaid098/templates: ``get_users`` returns a plain ``list`` of
User instances (the old implementation returned a tuple while every caller
expected a list).
"""

from __future__ import annotations

from fastapi import HTTPException, status

from {{ cookiecutter.project_name }}.db.models.user import User


class UserService:
    """Stateless user service — all methods are async classmethods."""

    @staticmethod
    async def get_users() -> list[User]:
        """Return all users as a list (NOT a tuple — bug fixed)."""
        return list(await User.all())

    @staticmethod
    async def get_user(user_id: int) -> User:
        """Return a single user by id or raise 404."""
        user = await User.get_or_none(id=user_id)
        if user is None:
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail=f"User {user_id} not found",
            )
        return user

    @staticmethod
    async def create_user(username: str, email: str) -> User:
        """Create a new user."""
        return await User.create(username=username, email=email)