package bot

import (
	"log"
	"velox-bot/internal/commands"
	"velox-bot/internal/db/services"
	"velox-bot/internal/events"

	"github.com/bwmarrin/discordgo"
)

type Bot struct {
	Session            *discordgo.Session
	AppID              string
	GuildID            string
	Commands           []*discordgo.ApplicationCommand
	registeredCommands []*discordgo.ApplicationCommand
	Services           *services.Services
}

func NewBot(token, appID, guildID string, cmds []*discordgo.ApplicationCommand, services *services.Services) (*Bot, error) {
	session, err := discordgo.New("Bot " + token)
	if err != nil {
		return nil, err
	}

	return &Bot{
		Session:  session,
		AppID:    appID,
		GuildID:  guildID,
		Commands: cmds,
		Services: services,
	}, nil
}

func (b *Bot) Start() error {
	if err := b.Session.Open(); err != nil {
		return err
	}

	b.registeredCommands = make([]*discordgo.ApplicationCommand, len(b.Commands))
	for _, cmd := range b.Commands {
		created, err := b.Session.ApplicationCommandCreate(b.AppID, b.GuildID, cmd)
		if err != nil {
			log.Printf("Cannot create '%v' command: %v", cmd.Name, err)
			continue
		}
		b.registeredCommands = append(b.registeredCommands, created)
	}

	b.Session.AddHandler(commands.HandleInteraction)

	b.Session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
		events.HandleMessageCreate(s, m, b.Services)
	})

	return nil
}

func (b *Bot) Close() error {
	for _, cmd := range b.registeredCommands {
		if err := b.Session.ApplicationCommandDelete(b.AppID, b.GuildID, cmd.ID); err != nil {
			log.Printf("cannot delete '%s' command: %v", cmd.Name, err)
		}
	}
	return b.Session.Close()
}
