package main

import (
	"log"
	"os"

	"github.com/sdaduanbilei/agent-team/internal/api"
	"github.com/sdaduanbilei/agent-team/internal/store"
)

func main() {
	agentsDir := env("AGENTS_DIR", "agents")
	skillsDir := env("SKILLS_DIR", "skills")
	roomsDir := env("ROOMS_DIR", "rooms")
	usersDir := env("USERS_DIR", "users")
	teamsDir := env("TEAMS_DIR", "teams")
	addr := env("ADDR", ":8080")

	for _, dir := range []string{agentsDir, skillsDir, roomsDir, usersDir, teamsDir} {
		os.MkdirAll(dir, 0755)
	}

	dbPath := env("DB_PATH", "data.db")
	st, err := store.New(dbPath)
	if err != nil {
		log.Fatalf("初始化数据库失败: %v", err)
	}
	defer st.Close()

	s := api.New(agentsDir, skillsDir, roomsDir, usersDir, teamsDir, st)
	log.Printf("agent-team server starting on %s", addr)
	log.Fatal(s.Start(addr))
}

func env(key, fallback string) string {
	if v := os.Getenv(key); v != "" {
		return v
	}
	return fallback
}
