package migrations

import (
	"encoding/json"
	"log/slog"

	"git.sr.ht/~ashkeel/strimertul/twitch/eventsub"

	"git.sr.ht/~ashkeel/strimertul/database"
	"git.sr.ht/~ashkeel/strimertul/twitch/alerts"
	"git.sr.ht/~ashkeel/strimertul/twitch/chat"
	"git.sr.ht/~ashkeel/strimertul/twitch/timers"
	"github.com/nicklaw5/helix/v2"
)

func migrateToV4(db database.Database, logger *slog.Logger) error {
	logger.Info("Migrating database from v3 to v4")

	// Rename keys that have no schema changes
	for oldKey, newKey := range map[string]string{
		"twitch/bot-modules/timers/config": timers.ConfigKey,
		"twitch/bot-modules/alerts/config": alerts.ConfigKey,
		"twitch/bot-custom-commands":       chat.CustomCommandsKey,
	} {
		if err := renameKey(db, oldKey, newKey, true); err != nil {
			return err
		}
	}

	// Clear old event keys and IRC-related keys
	for _, key := range []string{
		"twitch/chat-activity",
		"twitch/chat-history",
		"twitch/@send-chat-message",
		"twitch/bot/@send-message",
		"twitch/ev/chat-message",
		"twitch/ev/eventsub-event",
	} {
		if err := db.RemoveKey(key); err != nil {
			return err
		}
	}

	// Migrate bot config to chat config
	var botConfig struct {
		CommandCooldown int `json:"command_cooldown"`
	}
	if err := db.GetJSON("twitch/bot-config", &botConfig); err != nil {
		return err
	}
	if err := db.PutJSON(chat.ConfigKey, chat.Config{
		CommandCooldown: botConfig.CommandCooldown,
	}); err != nil {
		return err
	}

	// Migrate eventsub history to their new keys
	type eventSubNotification struct {
		Subscription helix.EventSubSubscription `json:"subscription"`
		Event        json.RawMessage            `json:"event"`
	}
	var eventsubHistory []eventSubNotification
	if err := db.GetJSON("twitch/eventsub-history", &eventsubHistory); err != nil {
		return err
	}
	eventsubHistoryMap := make(map[string][]eventSubNotification)
	for _, notification := range eventsubHistory {
		key := eventsub.HistoryKeyPrefix + notification.Subscription.Type
		eventsubHistoryMap[key] = append(eventsubHistoryMap[key], notification)
	}
	for key, notifications := range eventsubHistoryMap {
		if err := db.PutJSON(key, notifications); err != nil {
			return err
		}
	}
	// Clear old eventsub history key
	if err := db.RemoveKey("twitch/eventsub-history"); err != nil {
		return err
	}

	return nil
}
