package main

import (
	"embed"
	"fmt"
	"io/fs"
	"log"
	"opencatd-open/internal/cli"
	"opencatd-open/internal/consts"
	"opencatd-open/pkg/config"
	"opencatd-open/pkg/store"
	"opencatd-open/router"

	"github.com/spf13/cobra"
)

//go:embed dist/*
var web embed.FS

func main() {
	cfg, err := config.LoadConfig()
	if err != nil {
		panic(err)
	}

	db, err := store.InitDB(cfg)
	if err != nil {
		panic(err)
	}

	rootCmd := &cobra.Command{
		Use:   "openteam",
		Short: "openteam cli",
		Long:  consts.Logo,
		Run: func(cmd *cobra.Command, args []string) {
			router.SetRouter(cfg, db, &web)
		},
	}

	rootCmd.AddCommand(cli.LoadCmd)
	if err := rootCmd.Execute(); err != nil {
		log.Fatal(err)
	}
}

func printFilesAndDirs(fsys fs.FS, prefix string) error {
	return fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}

		if d.IsDir() {
			fmt.Printf("%s[DIR] %s\n", prefix, p)
		} else {
			info, err := d.Info()
			if err != nil {
				return err
			}
			fmt.Printf("%s[FILE] %s (%d bytes)\n", prefix, p, info.Size())
		}
		return nil
	})
}
