package levelsettings

import (
	"context"

	"velox-bot/internal/db/repos/settingsrepo"
)

type Service struct {
	settings *settingsrepo.Repo
}

func New(settings *settingsrepo.Repo) *Service {
	return &Service{settings: settings}
}

func (s *Service) ToggleLevelSystem(ctx context.Context, guildID int64) (bool, error) {
	enabled, err := s.settings.IsLevelSystemEnabled(ctx, guildID)
	if err != nil {
		return false, err
	}

	newEnabled := !enabled
	if err := s.settings.SetLevelSystemEnabled(ctx, guildID, newEnabled); err != nil {
		return false, err
	}

	return newEnabled, nil
}

func (s *Service) SetLevelSystemEnabled(ctx context.Context, guildID int64, enabled bool) error {
	return s.settings.SetLevelSystemEnabled(ctx, guildID, enabled)
}

func (s *Service) SetLevelUpChannel(ctx context.Context, guildID int64, channelID int64) error {
	return s.settings.SetLevelUpChannel(ctx, guildID, channelID)
}

func (s *Service) SetLevelUpMessage(ctx context.Context, guildID int64, message string) error {
	return s.settings.SetLevelUpMessage(ctx, guildID, message)
}

func (s *Service) GetLevelUpMessage(ctx context.Context, guildID int64) (string, error) {
	return s.settings.GetLevelUpMessage(ctx, guildID)
}

func (s *Service) SetRoleRewardForLevel(ctx context.Context, guildID int64, level int, roleID int64) error {
	return s.settings.SetRoleRewardForLevel(ctx, guildID, level, roleID)
}

func (s *Service) ListRoleRewards(ctx context.Context, guildID int64, limit int) ([]*settingsrepo.LevelReward, error) {
	return s.settings.ListRoleRewards(ctx, guildID, limit)
}

func (s *Service) IsLevelSystemEnabled(ctx context.Context, guildID int64) (bool, error) {
	return s.settings.IsLevelSystemEnabled(ctx, guildID)
}
