package telegram_handler

import (
	"context"
	"errors"
	"fmt"
	"strings"
	"ticket-pimp/internal/domain"

	"github.com/mr-linch/go-tg"
	"github.com/mr-linch/go-tg/tgb"
)

type git struct {
	name string
	url  string

	git string
	ssh string
}

func newGit(g *domain.Git) *git {
	return &git{
		name: g.Name,
		url:  g.HtmlUrl,
		git:  g.CloneUrl,
		ssh:  fmt.Sprintf("ssh://%s/%s.git", g.SshUrl, g.FullName),
	}
}

// FYI: Telegram doesn't renders this hyperlink, if the url is localhost 🤷‍♂️
func (g *git) PrepareAnswer() string {
	return tg.HTML.Text(
		tg.HTML.Line(
			"Repo ",
			tg.HTML.Link(g.name, g.url),
			"has been created!",
		),
	)
}

func (h *Handler) NewRepoHandler(ctx context.Context, mu *tgb.MessageUpdate) error {

	str := strings.Replace(mu.Text, "/repo", "", 1)

	if str == "" {
		return errors.New("empty command provided")
	}

	var g *domain.Git

	g, err := h.git.CreateRepo(str)

	if err != nil {
		return mu.Answer(errorAnswer(err.Error())).ParseMode(tg.HTML).DoVoid(ctx)
	}

	resp := newGit(g).PrepareAnswer()
	h.LogMessage(ctx, mu, resp)

	return mu.Answer(resp).ParseMode(tg.HTML).DoVoid(ctx)
}
