package ops

import (
	"fmt"
	"strings"

	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"ops-assistant/internal/core/command"
	coremodule "ops-assistant/internal/core/module"
	"ops-assistant/internal/core/registry"
)

type Service struct {
	dbPath   string
	baseDir  string
	registry *registry.Registry
	db       *gorm.DB
}

func NewService(dbPath, baseDir string, reg *registry.Registry) *Service {
	db, _ := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
	return &Service{dbPath: dbPath, baseDir: baseDir, registry: reg, db: db}
}

func (s *Service) Handle(userID int64, text string) (bool, string) {
	if !strings.HasPrefix(strings.TrimSpace(text), "/") {
		return false, ""
	}
	cmd, _, err := command.ParseWithInputs(text)
	if err != nil {
		return false, ""
	}
	// 通用帮助
	if cmd.Module == "help" || cmd.Name == "/help" || cmd.Name == "/start" {
		return true, s.helpText()
	}
	if cmd.Module == "ops" && (len(cmd.Args) == 0 || cmd.Args[0] == "help") {
		return true, s.helpText()
	}
	if cmd.Module == "ops" && len(cmd.Args) > 0 && cmd.Args[0] == "modules" {
		return true, s.modulesStatusText()
	}
	if cmd.Module != "" && cmd.Module != "ops" && s.db != nil {
		if !coremodule.IsEnabled(s.db, cmd.Module) {
			return true, fmt.Sprintf("[ERR_FEATURE_DISABLED] 模块未启用: %s（开关: enable_module_%s）", cmd.Module, cmd.Module)
		}
	}
	out, handled, err := s.registry.Handle(userID, cmd)
	if !handled {
		return false, ""
	}
	if err != nil {
		return true, "❌ OPS 执行失败: " + err.Error()
	}
	return true, out
}

func (s *Service) helpText() string {
	lines := []string{
		"🛠️ OPS 交互命令：",
		"- /ops modules （查看模块启用状态）",
		"- /cpa help",
		"- /cpa status",
		"- /cpa usage backup",
		"- /cpa usage restore <backup_id> [--confirm YES_RESTORE] [--dry-run]",
		"- /cf status （需要 enable_module_cf）",
		"- /cf zones （需要 enable_module_cf）",
		"- /cf dns list <zone_id> （需要 enable_module_cf）",
		"- /cf dns update <zone_id> <record_id> <type> <name> <content> [ttl] [proxied:true|false] （需要 enable_module_cf）",
		"- /cf dnsadd <name> <content> [on|off] [type] （需要 enable_module_cf）",
		"- /cf dnsset <record_id> <content> [true] （需要 enable_module_cf）",
		"- /cf dnsdel <record_id> YES （需要 enable_module_cf）",
		"- /cf dnsproxy <record_id|name> on|off （需要 enable_module_cf）",
		"- /cf workers list （需要 enable_module_cf）",
		"- /mail status （需要 enable_module_mail）",
	}
	return strings.Join(lines, "\n")
}

func (s *Service) modulesStatusText() string {
	mods := s.registry.ListModules()
	if len(mods) == 0 {
		return "暂无已注册模块"
	}
	lines := []string{"🧩 模块状态："}
	for _, m := range mods {
		enabled := false
		if s.db != nil {
			enabled = coremodule.IsEnabled(s.db, m)
		}
		state := "disabled"
		if enabled {
			state = "enabled"
		}
		lines = append(lines, fmt.Sprintf("- %s: %s", m, state))
	}
	lines = append(lines, "\n可用命令：/ops modules")
	return strings.Join(lines, "\n")
}
