package main

import (
	"flag"
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"

	"go-ts/internal/client"
)

func main() {
	serverAddr := flag.String("server", "127.0.0.1:9987", "TeamSpeak 3 Server Address")
	nickname := flag.String("nickname", "GoCient", "Nickname")
	flag.Parse()

	// Wait for server to start (Docker fix)
	log.Println("Waiting 5 seconds for server to start...")
	time.Sleep(5 * time.Second)

	log.Printf("Starting TS3 Client...")
	log.Printf("Server: %s", *serverAddr)
	log.Printf("Nickname: %s", *nickname)

	c := client.NewClient(*nickname)

	errChan := make(chan error)
	go func() {
		if err := c.Connect(*serverAddr); err != nil {
			errChan <- err
		}
	}()

	// Wait for signals
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)

	select {
	case err := <-errChan:
		log.Fatalf("Client Error: %v", err)
	case <-sigChan:
		log.Println("Shutting down...")
	}
}
