package migration

import (
	"fmt"
	"io"
	"os"

	"ariga.io/atlas-provider-gorm/gormschema"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

func loader(input string) {
	gormCfg := &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
			NoLowerCase:   true,
		},
	}
	stmts, err := gormschema.New("postgres", gormschema.WithConfig(gormCfg)).Load(getEntities(input)...)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to load gorm schema: %v\n", err)
		os.Exit(1)
	}
	_, _ = io.WriteString(os.Stdout, stmts)
}

func getEntities(input string) []any {
	switch input {
	case "main":
		return getMainEntities()
	case "satusehat":
		return getSatuSehatEntities()
	case "simgossync":
		return getSyncEntities()
	}
	return nil
}

func Migrate(input string) {
	loader(input)

}
