package localapi

import (
	"encoding/json"
	"net/http"
	"testing"

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

// handleDiskCandidates splits discovery into initialize (all unclaimed) + attach (mountable-FS subset).
func TestDiskCandidates_Split(t *testing.T) {
	d := &fakeDiskOps{candidates: []storage.CandidateDisk{
		{Device: "/dev/sdd", SizeBytes: 64 << 30, DataBearing: false},                                      // blank → initialize only
		{Device: "/dev/sde", FSType: "ext4", DataBearing: true, Mountable: true, MountSource: "/dev/sde1"}, // FS → init + attach
		{Device: "/dev/sdf", FSType: "ntfs", DataBearing: true, Mountable: false},                          // ntfs → initialize only
	}}
	h := newDiskServer(t, d, &fakeGate{}, nil, nil)

	w := do(t, h, "GET", "/disks/candidates", "A", "")
	if w.Code != http.StatusOK {
		t.Fatalf("candidates: got %d want 200 (%s)", w.Code, w.Body.String())
	}
	var env struct {
		Data struct {
			Initialize []storage.CandidateDisk `json:"initialize"`
			Attach     []storage.CandidateDisk `json:"attach"`
		} `json:"data"`
	}
	if err := json.Unmarshal(w.Body.Bytes(), &env); err != nil {
		t.Fatalf("parse: %v", err)
	}
	resp := env.Data
	if len(resp.Initialize) != 3 {
		t.Fatalf("initialize must list all 3 unclaimed disks, got %d", len(resp.Initialize))
	}
	if len(resp.Attach) != 1 || resp.Attach[0].Device != "/dev/sde" {
		t.Fatalf("attach must list only the mountable-FS disk, got %+v", resp.Attach)
	}
}
