package adminuser

import (
	"context"
	"errors"
)

var (
	ErrDependencyUnavailable = errors.New("dependency unavailable")
	ErrInvalidUser           = errors.New("invalid user")
	ErrRealnameNotRevocable  = errors.New("realname not revocable")
)

type Service struct {
	repo *Repository
}

func NewService(repo *Repository) *Service {
	return &Service{repo: repo}
}

func (s *Service) List(ctx context.Context, page, pageSize int, query ListQuery) (*PaginatedResult, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	return s.repo.List(ctx, page, pageSize, query)
}

func (s *Service) Freeze(ctx context.Context, adminID uint64, userID uint64, req FreezeRequest, meta AuditMeta) (*UserDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	if userID == 0 {
		return nil, ErrInvalidUser
	}
	return s.repo.Freeze(ctx, adminID, userID, req, meta)
}

func (s *Service) Unfreeze(ctx context.Context, adminID uint64, userID uint64, meta AuditMeta) (*UserDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	if userID == 0 {
		return nil, ErrInvalidUser
	}
	return s.repo.Unfreeze(ctx, adminID, userID, meta)
}

func (s *Service) SetDepositFreeQuota(ctx context.Context, adminID uint64, userID uint64, req DepositFreeQuotaRequest, meta AuditMeta) (*UserDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	if userID == 0 || req.AmountCent < 0 {
		return nil, ErrInvalidUser
	}
	return s.repo.SetDepositFreeQuota(ctx, adminID, userID, req, meta)
}

func (s *Service) RevokeRealname(ctx context.Context, adminID uint64, userID uint64, req RevokeRealnameRequest, meta AuditMeta) (*UserDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	if userID == 0 {
		return nil, ErrInvalidUser
	}
	return s.repo.RevokeRealname(ctx, adminID, userID, req, meta)
}
