package proxmox

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

// TestDestroyLXC_DeleteContract locks the DELETE wire contract: no request body (a form
// body → PVE HTTP 501 "Unexpected content for method 'DELETE'"), flags in the query string,
// force=1 so a still-running scratch can be torn down. Regression guard for the slice-6
// teardown bug.
func TestDestroyLXC_DeleteContract(t *testing.T) {
	var gotMethod, gotURL string
	var gotBody string
	m := &mockDoer{fn: func(r *http.Request) (*http.Response, error) {
		gotMethod = r.Method
		gotURL = r.URL.String()
		if r.Body != nil {
			b, _ := io.ReadAll(r.Body)
			gotBody = string(b)
		}
		return jsonResp(200, `{"data":"UPID:demo:0000:vzdestroy::root@pam:"}`), nil
	}}
	c := newTestClient(m)
	upid, err := c.DestroyLXC(context.Background(), 990000)
	if err != nil {
		t.Fatalf("DestroyLXC: %v", err)
	}
	if upid == "" {
		t.Error("expected a UPID")
	}
	if gotMethod != http.MethodDelete {
		t.Errorf("method = %s, want DELETE", gotMethod)
	}
	if gotBody != "" {
		t.Errorf("DELETE must send NO body, got %q", gotBody)
	}
	for _, want := range []string{"/nodes/demo-felhom/lxc/990000", "purge=1", "destroy-unreferenced-disks=1", "force=1"} {
		if !strings.Contains(gotURL, want) {
			t.Errorf("URL %q missing %q", gotURL, want)
		}
	}
}

// TestVzdump_NotesTemplate confirms the notes-template param is sent when set.
func TestVzdump_NotesTemplate(t *testing.T) {
	var gotBody string
	m := &mockDoer{fn: func(r *http.Request) (*http.Response, error) {
		b, _ := io.ReadAll(r.Body)
		gotBody = string(b)
		return jsonResp(200, `{"data":"UPID:demo:0:vzdump::root@pam:"}`), nil
	}}
	c := newTestClient(m)
	if _, err := c.Vzdump(context.Background(), VzdumpOptions{VMID: 9001, Storage: "local", Mode: ModeSnapshot, Notes: "felhom {{guestname}}"}); err != nil {
		t.Fatal(err)
	}
	if !strings.Contains(gotBody, "notes-template=") {
		t.Errorf("vzdump body missing notes-template: %q", gotBody)
	}
}
