package controller

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

func (wc *WorkflowController) CreateCoda(guildID string, chanID string) (string, error) {

	p, err := wc.GetProjectByChannelID(context.TODO(), chanID)
	if err != nil {
		return "", err
	}

	if p == nil {
		return "", errors.New("project wasn't found in the db")
	}

	requestResult, err := wc.ICoda.CreateApp(domain.CodaApplication{
		ID:       p.Key,
		Summary:  p.Name,
		URL:      fmt.Sprintf("https://discord.com/channels/%s/%s", guildID, chanID),
		Git:      p.ProjectGit,
		GitBuild: p.BuildGit,
		Folder:   p.Folder,
	})

	return requestResult, err
}
