"""User service — business logic for user profile reads."""
from __future__ import annotations

import structlog

from repositories.user_repository import UserRepository

log = structlog.get_logger()


class UserService:
    def __init__(self, repo: UserRepository):
        self.repo = repo

    async def get_me(self, token: str) -> dict:
        """Return the authenticated user's profile.

        Returns a dict with id, email, name, credits_balance, free_used.
        Raises httpx.HTTPStatusError if the token is invalid.
        """
        record = await self.repo.get_by_token(token)
        return {
            'id': record.get('id'),
            'email': record.get('email'),
            'name': record.get('name', ''),
            'credits_balance': record.get('credits_balance', 0),
            'free_used': record.get('free_used', False),
        }
