package skills

import (
	"context"
	"fkteams/agentcore"
	einoruntime "fkteams/agentcore/eino"
	"fkteams/agentcore/eino/middlewares/fkfs"
	"fkteams/common"
	"fmt"
	"os"
	"path/filepath"

	"github.com/cloudwego/eino/adk/middlewares/skill"
)

func ensureDir(path string) error {
	_, err := os.Stat(path)
	if err == nil {
		return nil
	}
	if os.IsNotExist(err) {
		return os.MkdirAll(path, 0755)
	}
	return err
}

func New(ctx context.Context) (agentcore.AgentMiddleware, error) {
	skillsDirPath := filepath.Join(common.AppDir(), "skills")

	if err := ensureDir(skillsDirPath); err != nil {
		return nil, fmt.Errorf("无法创建或访问目录 %s: %w", skillsDirPath, err)
	}

	fkBackend, err := fkfs.NewLocalBackend(skillsDirPath)
	if err != nil {
		return nil, fmt.Errorf("无法创建本地后端: %w", err)
	}

	localBackend, err := skill.NewBackendFromFilesystem(ctx, &skill.BackendFromFilesystemConfig{
		Backend: fkBackend,
		BaseDir: skillsDirPath,
	})
	if err != nil {
		return nil, err
	}
	skillsMiddleware, err := skill.NewMiddleware(ctx, &skill.Config{
		Backend:    localBackend,
		UseChinese: true,
	})
	if err != nil {
		return nil, err
	}
	return einoruntime.WrapAgentMiddleware("skills", skillsMiddleware), nil
}
