package runbook

import (
	"strings"

	"ops-assistant/models"

	"gorm.io/gorm"
)

type ResolvedTarget struct {
	Found bool
	User  string
	Host  string
	Port  int
}

func resolveTarget(db *gorm.DB, name string) ResolvedTarget {
	trim := strings.TrimSpace(name)
	if trim == "" {
		return ResolvedTarget{}
	}
	var t models.OpsTarget
	if err := db.Where("name = ? AND enabled = ?", trim, true).First(&t).Error; err != nil {
		return ResolvedTarget{}
	}
	user := strings.TrimSpace(t.User)
	host := strings.TrimSpace(t.Host)
	port := t.Port
	if user == "" || host == "" {
		return ResolvedTarget{}
	}
	if port <= 0 {
		port = 22
	}
	return ResolvedTarget{Found: true, User: user, Host: host, Port: port}
}
