package hub

import (
	"context"
	"io"
	"log/slog"
	"net/http"
	"strings"

	"gitea.dooplex.hu/admin/felhom-agent/internal/proxmox"
)

func quietLogger() *slog.Logger { return slog.New(slog.NewTextHandler(io.Discard, nil)) }

// roundTripFunc is a mock http.RoundTripper.
type roundTripFunc func(*http.Request) (*http.Response, error)

func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }

// testClient builds a hub Client over a mock transport (no network).
func testClient(rt roundTripFunc) *Client {
	return newClient("https://hub.example.test", "super-secret-bearer-key", "demo-host-01", &http.Client{Transport: rt}, quietLogger())
}

func httpResp(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"}},
	}
}

// fakePx is a fake proxmoxReader.
type fakePx struct {
	node   string
	ns     proxmox.NodeStatus
	nsErr  error
	lxc    []proxmox.Guest
	lxcErr error
	cfg    map[int]proxmox.GuestConfig
	cfgErr map[int]error
}

func (f *fakePx) Node() string { return f.node }
func (f *fakePx) NodeStatus(ctx context.Context) (proxmox.NodeStatus, error) {
	return f.ns, f.nsErr
}
func (f *fakePx) ListLXC(ctx context.Context) ([]proxmox.Guest, error) { return f.lxc, f.lxcErr }
func (f *fakePx) GuestConfig(ctx context.Context, vmid int) (proxmox.GuestConfig, error) {
	if e := f.cfgErr[vmid]; e != nil {
		return proxmox.GuestConfig{}, e
	}
	return f.cfg[vmid], nil
}

// fakeProber is a fake CloudflaredProber.
type fakeProber struct {
	status string
	err    error
}

func (p fakeProber) Status(ctx context.Context) (string, error) { return p.status, p.err }
