package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"path/filepath"
)

var (
	serverAddr = flag.String("server", "ws://localhost:3001", "Adresse WebSocket du serveur")
	nodeID     = flag.String("node-id", defaultNodeID(), "ID du nœud (défaut: hostname)")
	dataDir    = flag.String("data-dir", "./edubox-data", "Répertoire de données")
	uiEnabled  = flag.Bool("ui", true, "Activer l'interface locale HTMX")
)

func defaultNodeID() string {
	h, err := os.Hostname()
	if err != nil {
		return "unknown"
	}
	return h
}

func main() {
	flag.Parse()

	dd := *dataDir
	if !filepath.IsAbs(dd) {
		ex, err := os.Executable()
		if err == nil {
			dd = filepath.Join(filepath.Dir(ex), dd)
		} else {
			wd, _ := os.Getwd()
			dd = filepath.Join(wd, dd)
		}
	}
	*dataDir = dd

	if err := os.MkdirAll(*dataDir, 0755); err != nil {
		log.Fatalf("Cannot create data-dir: %v", err)
	}

	fmt.Printf("EduBox Agent - node: %s - data-dir: %s\n", *nodeID, *dataDir)

	if *uiEnabled {
		go startUI(*dataDir, *nodeID, *serverAddr)
	}

	startWebSocket(*serverAddr, *nodeID, *dataDir)
}
