package proxmox

import (
	"context"
	"io"
	"net/http"
	"strings"
)

// mockDoer is an injectable HTTP transport for the API client. It records call
// count and routes each request to fn.
type mockDoer struct {
	calls int
	fn    func(*http.Request) (*http.Response, error)
}

func (m *mockDoer) Do(r *http.Request) (*http.Response, error) {
	m.calls++
	return m.fn(r)
}

// jsonResp builds an HTTP response with a JSON body.
func jsonResp(code int, body string) *http.Response {
	return &http.Response{
		StatusCode: code,
		Body:       io.NopCloser(strings.NewReader(body)),
		Header:     http.Header{"Content-Type": []string{"application/json"}},
	}
}

// newTestClient wraps a mockDoer in a Client (bypassing NewClient's real transport).
func newTestClient(d doer) *Client {
	return &Client{base: "https://host:8006/api2/json", node: "demo-felhom", token: "u@pve!t=secret", http: d}
}

// mockRunner records privileged command invocations and returns canned output.
type mockRunner struct {
	calls   int
	lastCmd string
	lastArg []string
	out     []byte
	err     error
}

func (m *mockRunner) Run(_ context.Context, name string, args ...string) ([]byte, []byte, error) {
	m.calls++
	m.lastCmd = name
	m.lastArg = args
	return m.out, nil, m.err
}
