package matrixbot

import (
	"fmt"

	"maunium.net/go/mautrix"
	"maunium.net/go/mautrix/event"
	"maunium.net/go/mautrix/format"
	"maunium.net/go/mautrix/id"
	"roese.dev/matrix-development-lauge/modules/config"
)

func SendMessage(roomID string, topic string, header string, content string, url string) {
	cfg := config.Cfg.Bot

	client, err := mautrix.NewClient(cfg.HomeserverUrl, "", "")

	if err != nil {
		panic(err)
	}

	_, err = client.Login(&mautrix.ReqLogin{
		Type:             "m.login.password",
		Identifier:       mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: cfg.User},
		Password:         cfg.Password,
		StoreCredentials: true,
	})

	if err != nil {
		panic(err)
	}

	msg := format.RenderMarkdown(fmt.Sprintf("Thema: %s <h4>%s</h4>%s<br>%s", topic, header, content, url), false, true)

	client.SendMessageEvent(id.RoomID(roomID), event.EventMessage, &msg)
}
