// tools/genplugins/main.go
package main

import (
	"fmt"
	"io/fs"
	"os"
	"path/filepath"
	"strings"
)

func main() {
	pluginsDir := "internal/service/agent/plugins"

	// 扫描子目录（只扫描直接子目录，不包括 all）
	dirs := []string{}
	filepath.WalkDir(pluginsDir, func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		// 只处理直接子目录
		if d.IsDir() && path != pluginsDir {
			relPath, err := filepath.Rel(pluginsDir, path)
			if err != nil {
				return err
			}
			// 只获取直接子目录（不包含路径分隔符）且不是 all
			if !strings.Contains(relPath, string(filepath.Separator)) && relPath != "all" {
				dirs = append(dirs, relPath)
			}
		}
		return nil
	})

	// 生成导入代码
	imports := ""
	for _, dir := range dirs {
		imports += fmt.Sprintf("\t_ \"github.com/anthropic/isdp/internal/service/agent/plugins/%s\"\n", dir)
	}

	content := fmt.Sprintf(`// Code generated by tools/genplugins. DO NOT EDIT.
package all

import (
%s)
`, imports)

	// 写入文件
	allPath := filepath.Join(pluginsDir, "all", "all.go")
	err := os.WriteFile(allPath, []byte(content), 0644)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error writing file: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("Generated %s with %d plugins\n", allPath, len(dirs))
}