package project

import (
	"context"
	"fmt"
	"os"
	"path"
)

import (
	"github.com/wang1309/cola-go/cmd/cola/v1/internal/base"
)

import (
	"github.com/AlecAivazis/survey/v2"
	"github.com/fatih/color"
)

// Project is a project template.
type Project struct {
	Name string
	Path string
}

func (p *Project) New(ctx context.Context, dir string, layout string, branch string) error {
	to := path.Join(dir, p.Name)
	if _, err := os.Stat(to); !os.IsNotExist(err) {
		fmt.Printf("ğŸš« %s already exists\n", p.Name)
		override := false
		prompt := &survey.Confirm{
			Message: "ğŸ“‚ Do you want to override the folder ?",
			Help:    "Delete the existing folder and create the project.",
		}
		e := survey.AskOne(prompt, &override)
		if e != nil {
			return e
		}
		if !override {
			return err
		}
		os.RemoveAll(to)
	}
	fmt.Printf("ğŸš€ Creating service %s, layout repo is %s, please wait a moment.\n\n", p.Name, layout)
	repo := base.NewRepo(layout, branch)
	if err := repo.CopyTo(ctx, to, p.Path, []string{".git", ".github"}); err != nil {
		return err
	}
	e := os.Rename(
		path.Join(to, "cmd", "server"),
		path.Join(to, "cmd", p.Name),
	)
	if e != nil {
		return e
	}
	base.Tree(to, dir)

	fmt.Printf("\nğŸ�º Project creation succeeded %s\n", color.GreenString(p.Name))
	fmt.Print("ğŸ’» Use the following command to start the project ğŸ‘‡:\n\n")

	fmt.Println(color.WhiteString("$ cd %s", p.Name))
	fmt.Println(color.WhiteString("$ go generate ./..."))
	fmt.Println(color.WhiteString("$ go build -o ./bin/ ./... "))
	fmt.Println(color.WhiteString("$ ./bin/%s -conf ./configs\n", p.Name))
	fmt.Println("			ğŸ¤� Thanks for using Cola")
	fmt.Println("	ğŸ“š Tutorial: https://github.com/wang1309/cola-go#readme")
	return nil
}
