package sshd

import (
	"encoding/json"
	"fmt"
	"strings"

	"hnyfkj.com.cn/rtu/linux/utils/shell"
)

func extractShellExecuteParams(params json.RawMessage) (shell.ExecuteParams, error) {
	if len(params) == 0 {
		return shell.ExecuteParams{}, fmt.Errorf("missing params")
	}

	var p shell.ExecuteParams
	if err := json.Unmarshal(params, &p); err != nil {
		return shell.ExecuteParams{}, err
	}

	return p, nil
}

func extractClientID(params json.RawMessage) (string, error) {
	if len(params) == 0 {
		return "", fmt.Errorf("missing params")
	}

	var p struct {
		ClientID string `json:"client_id"`
	}
	if err := json.Unmarshal(params, &p); err != nil {
		return "", err
	}

	clientID := strings.TrimSpace(p.ClientID)
	if clientID == "" {
		return "", fmt.Errorf("clientID is required and cannot be blank")
	}

	return clientID, nil
}
