package got

import (
	"fmt"
	"os"

	. "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
)

var Build = func(project *Project, buildOptions *BuildOptions) (fn *BuildSet, err error) {
	var (
		files  []os.FileInfo
		before = []*Command{
			{
				Cmd:         "rm -f go.*",
				Description: "Remove os arquivos do módulo do GO",
			},
			{
				Cmd:         "go mod init {project.custom.go.package.repository}/build",
				Description: "Inicializa o módulo do GO",
			},
		}
	)

	path := fmt.Sprintf("%s/include/go", project.CurrentDirectory)
	if files, err = GetFiles(path); err == nil {
		for _, file := range files {
			if file.Name()[0] == '*' {
				continue
			}
			if file.IsDir() {
				name := file.Name()
				before = append(before, &Command{
					Cmd:         fmt.Sprintf("go mod edit -replace {project.custom.go.package.repository}/build/v1/%s=./v1/%s", name, name),
					Description: "Inicializa o módulo do GO",
				})
			}
		}
	}

	fn = &BuildSet{
		Before: before,
		After: []*Command{
			{
				Id:          "build",
				Cmd:         "GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o server",
				Description: "Compila a aplicação server da api",
			},
		},
	}
	return
}
