package service

import (
	"log"
	"x-agents/server/internal/model"
	"x-agents/server/internal/repository"
)

type ToolService struct {
	toolRepo *repository.ToolRepository
}

func NewToolService(toolRepo *repository.ToolRepository) *ToolService {
	return &ToolService{toolRepo: toolRepo}
}

func (s *ToolService) GetAllTools() ([]model.Tool, error) {
	return s.toolRepo.FindAll()
}

func (s *ToolService) GetToolsByCategory(category string) ([]model.Tool, error) {
	return s.toolRepo.FindByCategory(category)
}

func (s *ToolService) GetToolByID(id string) (*model.Tool, error) {
	return s.toolRepo.FindByID(id)
}

// GetTools 根据条件获取工具列表
func (s *ToolService) GetTools(category string, status string) ([]model.Tool, error) {
	var tools []model.Tool
	query := s.toolRepo.DB()

	if category != "" {
		query = query.Where("category = ?", category)
	}
	if status != "" {
		query = query.Where("status = ?", status)
	}

	err := query.Find(&tools).Error
	return tools, err
}

func (s *ToolService) CreateTool(tool *model.Tool) error {
	return s.toolRepo.Create(tool)
}

func (s *ToolService) UpdateTool(tool *model.Tool) error {
	return s.toolRepo.Update(tool)
}

func (s *ToolService) DeleteTool(id string) error {
	return s.toolRepo.Delete(id)
}

// InitDefaultTools 初始化默认工具到数据库
func (s *ToolService) InitDefaultTools() error {
	// 删除现有的系统工具，重新插入
	s.toolRepo.DB().Where("provider = ?", "system").Delete(&model.Tool{})

	// 插入默认工具
	tools := s.getDefaultTools()
	for _, tool := range tools {
		if err := s.toolRepo.Create(&tool); err != nil {
			return err
		}
	}

	log.Printf("[ToolService] Loaded %d default tools", len(tools))
	return nil
}

// getDefaultTools 获取默认工具列表
func (s *ToolService) getDefaultTools() []model.Tool {
	return []model.Tool{
		// 文件操作
		{
			Name:            "read_file",
			Description:     "Read the contents of a file from the filesystem.",
			DescriptionCN:   "读取文件系统的文件内容。",
			Category:        "file",
			SecurityLevel:   "safe",
			RequireApproval: false,
			Provider:        "system",
			Status:          "active",
			Parameters:      `{"type":"object","properties":{"file_path":{"type":"string","description":"The path to the file to read"},"encoding":{"type":"string","description":"File encoding (default: utf-8)","default":"utf-8"}},"required":["file_path"]}`,
		},
		{
			Name:            "write_file",
			Description:     "Write content to a file. Creates the file if it doesn't exist, overwrites if it does.",
			DescriptionCN:   "写入内容到文件。如果文件不存在则创建，存在则覆盖。",
			Category:        "file",
			SecurityLevel:   "review",
			RequireApproval: true,
			Provider:        "system",
			Status:          "active",
			Parameters:      `{"type":"object","properties":{"file_path":{"type":"string","description":"The path to the file to write"},"content":{"type":"string","description":"The content to write to the file"}},"required":["file_path","content"]}`,
		},
		{
			Name:            "list_dir",
			Description:     "List the contents of a directory.",
			DescriptionCN:   "列出目录的内容。",
			Category:        "file",
			SecurityLevel:   "safe",
			RequireApproval: false,
			Provider:        "system",
			Status:          "active",
			Parameters:      `{"type":"object","properties":{"dir_path":{"type":"string","description":"The path to the directory to list","default":"."}}}`,
		},
		{
			Name:            "delete_file",
			Description:     "Delete a file or directory.",
			DescriptionCN:   "删除文件或目录。",
			Category:        "file",
			SecurityLevel:   "danger",
			RequireApproval: true,
			Provider:        "system",
			Status:          "active",
			Parameters:      `{"type":"object","properties":{"file_path":{"type":"string","description":"The path to the file or directory to delete"}},"required":["file_path"]}`,
		},
		{
			Name:            "search_files",
			Description:     "Search for files by name pattern or content.",
			DescriptionCN:   "按文件名模式或内容搜索文件。",
			Category:        "file",
			SecurityLevel:   "safe",
			RequireApproval: false,
			Provider:        "system",
			Status:          "active",
			Parameters:      `{"type":"object","properties":{"directory":{"type":"string","description":"The directory to search in"},"pattern":{"type":"string","description":"Glob pattern for file names","default":"*"},"content_pattern":{"type":"string","description":"Search for files containing this text"}},"required":["directory"]}`,
		},
		// 代码执行
		{
			Name:            "execute_python",
			Description:     "Execute Python code in a sandboxed environment.",
			DescriptionCN:   "在沙盒环境中执行Python代码。",
			Category:        "executor",
			SecurityLevel:   "review",
			RequireApproval: true,
			Provider:        "system",
			Status:          "active",
			Parameters:      `{"type":"object","properties":{"code":{"type":"string","description":"The Python code to execute"},"timeout":{"type":"integer","description":"Execution timeout in seconds","default":30}},"required":["code"]}`,
		},
		{
			Name:            "execute_javascript",
			Description:     "Execute JavaScript code in a sandboxed environment.",
			DescriptionCN:   "在沙盒环境中执行JavaScript代码。",
			Category:        "executor",
			SecurityLevel:   "review",
			RequireApproval: true,
			Provider:        "system",
			Status:          "active",
			Parameters:      `{"type":"object","properties":{"code":{"type":"string","description":"The JavaScript code to execute"},"timeout":{"type":"integer","description":"Execution timeout in seconds","default":30}},"required":["code"]}`,
		},
		{
			Name:            "execute_bash",
			Description:     "Execute a bash command in a sandboxed environment.",
			DescriptionCN:   "在沙盒环境中执行Bash命令。",
			Category:        "executor",
			SecurityLevel:   "danger",
			RequireApproval: true,
			Provider:        "system",
			Status:          "active",
			Parameters:      `{"type":"object","properties":{"command":{"type":"string","description":"The bash command to execute"},"timeout":{"type":"integer","description":"Execution timeout in seconds","default":30}},"required":["command"]}`,
		},
		// 网页
		{
			Name:            "web_fetch",
			Description:     "Fetch content from a web URL.",
			DescriptionCN:   "从网页URL获取内容。",
			Category:        "web",
			SecurityLevel:   "safe",
			RequireApproval: false,
			Provider:        "system",
			Status:          "active",
			Parameters:      `{"type":"object","properties":{"url":{"type":"string","description":"The URL to fetch"},"method":{"type":"string","description":"HTTP method","default":"GET"},"timeout":{"type":"integer","description":"Request timeout in seconds","default":30}},"required":["url"]}`,
		},
		{
			Name:            "web_search",
			Description:     "Search the web for information.",
			DescriptionCN:   "在网络上搜索信息。",
			Category:        "web",
			SecurityLevel:   "safe",
			RequireApproval: false,
			Provider:        "system",
			Status:          "active",
			Parameters:      `{"type":"object","properties":{"query":{"type":"string","description":"The search query"},"max_results":{"type":"integer","description":"Maximum number of results","default":5}},"required":["query"]}`,
		},
		// HTTP
		{
			Name:            "http_request",
			Description:     "Make HTTP requests to APIs.",
			DescriptionCN:   "向API发送HTTP请求。",
			Category:        "http",
			SecurityLevel:   "safe",
			RequireApproval: false,
			Provider:        "system",
			Status:          "active",
			Parameters:      `{"type":"object","properties":{"url":{"type":"string","description":"The URL to request"},"method":{"type":"string","description":"HTTP method","default":"GET"},"params":{"type":"object","description":"Query parameters"},"json_data":{"type":"object","description":"JSON body"},"timeout":{"type":"integer","description":"Request timeout","default":30}},"required":["url"]}`,
		},
		// 通知
		{
			Name:            "send_notification",
			Description:     "Send notifications via email, webhook, dingtalk, or slack.",
			DescriptionCN:   "通过邮件、webhook、钉钉或Slack发送通知。",
			Category:        "notification",
			SecurityLevel:   "safe",
			RequireApproval: false,
			Provider:        "system",
			Status:          "active",
			Parameters:      `{"type":"object","properties":{"type":{"type":"string","description":"Notification type: email, webhook, dingtalk, slack"},"message":{"type":"string","description":"The notification message"}},"required":["type","message"]}`,
		},
		// 系统
		{
			Name:            "get_current_time",
			Description:     "Get the current date and time.",
			DescriptionCN:   "获取当前日期和时间。",
			Category:        "system",
			SecurityLevel:   "safe",
			RequireApproval: false,
			Provider:        "system",
			Status:          "active",
			Parameters:      `{"type":"object","properties":{"timezone":{"type":"string","description":"Optional timezone (e.g., UTC, Asia/Shanghai)"}}}`,
		},
	}
}
