package service

import (
	"context"

	"wucher/internal/domain/user"
)

type UserService struct {
	repo user.Repository
}

func NewUserService(repo user.Repository) *UserService {
	return &UserService{repo: repo}
}

func (s *UserService) Create(ctx context.Context, u *user.User) error {
	return s.repo.Create(ctx, u)
}

func (s *UserService) Update(ctx context.Context, u *user.User) error {
	return s.repo.Update(ctx, u)
}

func (s *UserService) Delete(ctx context.Context, id []byte) error {
	return s.repo.Delete(ctx, id)
}

func (s *UserService) GetByID(ctx context.Context, id []byte) (*user.User, error) {
	return s.repo.GetByID(ctx, id)
}

func (s *UserService) GetByEmail(ctx context.Context, email string) (*user.User, error) {
	return s.repo.GetByEmail(ctx, email)
}

func (s *UserService) GetByUsername(ctx context.Context, username string) (*user.User, error) {
	return s.repo.GetByUsername(ctx, username)
}

func (s *UserService) List(ctx context.Context, filter string, sort string, limit, offset int) ([]user.User, int64, error) {
	return s.repo.List(ctx, filter, normalizeUserSort(sort), limit, offset)
}

func normalizeUserSort(sort string) string {
	return normalizeSortByRules(sort,
		sortField("users.email", "email"),
		sortField("users.first_name", "first_name"),
		sortField("users.last_name", "last_name"),
		sortField("users.created_at", "created_at"),
		sortField("users.updated_at", "updated_at"),
	)
}
