package codegen

import "github.com/dave/jennifer/jen"

type ModuleBuilder struct {
	Name        string           // Name of the module.
	PackageName string           // Name of the package.
	Structs     []*StructBuilder // Structs of the module.
	Funcs       []*FuncBuilder   // Functions of the module.

	// TODO: add structs, funcs, etc. here
}

func Module(name string) *ModuleBuilder {
	return &ModuleBuilder{Name: name, PackageName: "main"}
}

func (mb *ModuleBuilder) WithPackageName(packageName string) *ModuleBuilder {
	mb.PackageName = packageName
	return mb
}

func (mb *ModuleBuilder) WithStructs(structs ...*StructBuilder) *ModuleBuilder {
	mb.Structs = structs
	return mb
}

func (mb *ModuleBuilder) WithFuncs(funcs ...*FuncBuilder) *ModuleBuilder {
	mb.Funcs = funcs
	return mb
}

func (mb *ModuleBuilder) Build() *jen.File {
	// initialize a new file
	f := jen.NewFile(mb.PackageName)

	// header comment
	f.HeaderComment("Code generated by actions-generator. DO NOT EDIT.")

	// add main struct and constructor
	f.Add(Func("New", "*"+mb.Name).WithBody(jen.Return(jen.Op("&").Id(mb.Name).Values())).Build()).Line()

	f.Type().Id(mb.Name).Struct().Line()

	// add structs
	for _, sb := range mb.Structs {
		f.Add(sb.Build())
	}

	// add funcs
	for _, fb := range mb.Funcs {
		f.Add(fb.Build())
	}

	// ensure file ends with a newline
	f.Add(jen.Line())

	return f
}
