package commands

import (
	"fmt"
	"strings"

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

const (
// ComponentNameInput = 1
)

var ()

func generateEntity() (err error) {
	var meta = flag.GenerateEntityControl

	entity := common.Entity{
		ID:   strings.ToLower(meta.Name),
		Type: meta.Type,
	}

	for _, property := range strings.Split(meta.Properties, ",") {
		property = strings.Trim(property, " ")
		property = common.LcFirst(property)
		if property != "" {
			entity.Properties = append(entity.Properties, &common.Propertie{
				ID: property,
			})
		}
	}

	filename := normalizePath(meta.Name)

	if err = common.WriteToJson(
		basePathWithComplement(fmt.Sprintf("%s/%s.json", EntitiesPath, filename)),
		entity,
	); err != nil {
		return
	}

	return
}

func generateResource() (err error) {
	var meta = flag.GenerateResourceControl

	resource := common.Resource{
		ID:     strings.ToLower(meta.Name),
		Entity: meta.Entity,
	}

	for _, id := range strings.Split(meta.Methods, ",") {
		id = strings.Trim(id, " ")
		id = common.LcFirst(id)
		if id != "" {
			resource.Methods = append(resource.Methods, &common.Method{
				ID: id,
			})
		}
	}

	filename := normalizePath(meta.Name)

	if err = common.WriteToJson(
		basePathWithComplement(fmt.Sprintf("%s/%s.json", ResourcesPath, filename)),
		resource,
	); err != nil {
		return
	}
	return
}
