package hostsetup

import (
	"fmt"
	"strings"
)

func renderUnit(spec AppSpec, opts SetupOptions) string {
	var b strings.Builder
	fmt.Fprintf(&b, "[Unit]\n")
	fmt.Fprintf(&b, "Description=%s\n", spec.Description)
	fmt.Fprintf(&b, "After=network-online.target\n")
	fmt.Fprintf(&b, "Wants=network-online.target\n\n")

	fmt.Fprintf(&b, "[Service]\n")
	fmt.Fprintf(&b, "Type=simple\n")
	fmt.Fprintf(&b, "User=%s\n", opts.User)
	fmt.Fprintf(&b, "Group=%s\n", opts.Group)
	fmt.Fprintf(&b, "ExecStart=%s serve --config %s\n", opts.BinaryPath, opts.ConfigPath)
	fmt.Fprintf(&b, "WorkingDirectory=%s\n", opts.DataDir)
	fmt.Fprintf(&b, "Restart=on-failure\n")
	fmt.Fprintf(&b, "RestartSec=5\n\n")

	fmt.Fprintf(&b, "[Install]\n")
	fmt.Fprintf(&b, "WantedBy=multi-user.target\n")
	return b.String()
}
