package app

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"vrc_osc_go/internal/config"
	"vrc_osc_go/internal/osc"
)

func Run(configPath string) error {
	cfg, err := config.Load(configPath)
	if err != nil {
		return err
	}

	server := osc.NewServer(cfg.OSC.Host, cfg.OSC.Port)
	server.Map("DiscordSend", func(_ string, args []osc.Value) error {
		return fmt.Errorf("discord mute not implemented yet")
	})

	errCh := make(chan error, 1)
	go func() { errCh <- server.Serve() }()

	sigCh := make(chan os.Signal, 1)
	signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
	select {
	case err := <-errCh:
		return err
	case <-sigCh:
		server.Close()
		return nil
	}
}

