package config

import (
	"context"

	"velox-bot/internal/commands/level/shared"
	"velox-bot/internal/db/services"

	"github.com/bwmarrin/discordgo"
)

func slvlSetMessageOption() *discordgo.ApplicationCommandOption {
	return &discordgo.ApplicationCommandOption{
		Type:        discordgo.ApplicationCommandOptionSubCommand,
		Name:        "set-message",
		Description: "Set the level up message",
		Options: []*discordgo.ApplicationCommandOption{
			{
				Type:        discordgo.ApplicationCommandOptionString,
				Name:        "message",
				Description: "Level-up message. Variables: {user}, {level}",
				Required:    true,
			},
		},
	}
}

func slvlSetMessageHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
	if !shared.RequireManageGuild(s, i) || !shared.RequireLevelSystemEnabled(s, i) {
		return
	}
	if !shared.RequireLevelSettingsService(s, i) {
		return
	}

	guildID, ok := shared.ParseGuildID(s, i)
	if !ok {
		return
	}

	optMap := shared.SubcommandOptionMap(i)
	messageOpt, ok := optMap["message"]
	if !ok {
		shared.RespondEphemeral(s, i, "Missing message option.")
		return
	}

	message := messageOpt.StringValue()
	if message == "" {
		message = "GG {user}, you reached level {level}!"
	}

	if err := services.Global.LevelSettings.SetLevelUpMessage(context.Background(), guildID, message); err != nil {
		shared.RespondEphemeral(s, i, "Failed to set level up message.")
		return
	}

	shared.RespondEphemeral(s, i, "Level up message set.")
}

