package usecase

import (
	"context"
	"fmt"

	"gitea.computernetthings.ru/yash/crypto_alert_bot/internal/entities"
)

func (uc *Usecase) RegisterNewUser(ctx context.Context, user *entities.User) error {
	_, err := uc.storage.SaveUser(ctx, user)
	if err != nil {
		uc.log.Error("failed to register new user", "user", user, "err", err)
		return fmt.Errorf("failed to register new user: %w", err)
	}

	return nil
}

func (uc *Usecase) UserByID(ctx context.Context, userID entities.UserID) (*entities.User, error) {
	user, err := uc.storage.UserByID(ctx, userID)
	if err != nil {
		uc.log.Error("failed to get user by id", "user_id", userID, "err", err)
		return nil, fmt.Errorf("failed to get user by id: %w", err)
	}

	return user, nil
}

func (uc *Usecase) UserByTgID(ctx context.Context, telegramID entities.TelegramID) (*entities.User, error) {
	user, err := uc.storage.UserByTelegramID(ctx, telegramID)
	if err != nil {
		uc.log.Error("failed to get user by telegram id", "tg_id", telegramID, "err", err)
		return nil, fmt.Errorf("failed to get user by telegram id: %w", err)
	}

	return user, nil
}
