package api

import (
	"testing"

	"github.com/alexei/tinyforge/internal/store"
)

func TestAggregateWorkloadStats_SumsPerTimestamp(t *testing.T) {
	// Two containers reporting at the same two ticks → summed per ts.
	samples := []store.ContainerStatsSample{
		{TS: 100, CPUPercent: 10, MemoryUsage: 1000, MemoryLimit: 4000},
		{TS: 100, CPUPercent: 5, MemoryUsage: 500, MemoryLimit: 8000},
		{TS: 200, CPUPercent: 20, MemoryUsage: 2000, MemoryLimit: 4000},
	}
	pts := aggregateWorkloadStats(samples)
	if len(pts) != 2 {
		t.Fatalf("expected 2 buckets, got %d", len(pts))
	}
	if pts[0].TS != 100 || pts[0].CPUPercent != 15 || pts[0].MemoryUsage != 1500 {
		t.Fatalf("ts=100 bucket wrong: %+v", pts[0])
	}
	// Memory limit takes the max across containers.
	if pts[0].MemoryLimit != 8000 {
		t.Fatalf("expected max memory limit 8000, got %d", pts[0].MemoryLimit)
	}
	if pts[1].TS != 200 || pts[1].CPUPercent != 20 {
		t.Fatalf("ts=200 bucket wrong: %+v", pts[1])
	}
}

func TestAggregateWorkloadStats_Empty(t *testing.T) {
	pts := aggregateWorkloadStats(nil)
	if pts == nil {
		t.Fatal("expected non-nil empty slice for clean JSON []")
	}
	if len(pts) != 0 {
		t.Fatalf("expected 0 points, got %d", len(pts))
	}
}

func TestWorkloadStatsHistory_UnknownWorkload404(t *testing.T) {
	e := newAPITestEnv(t)
	resp := e.do(t, "GET", "/api/workloads/nope/stats/history", nil)
	if resp.StatusCode != 404 {
		t.Fatalf("expected 404 for unknown workload, got %d", resp.StatusCode)
	}
}

func TestWorkloadStatsHistory_KnownWorkloadEmpty(t *testing.T) {
	e := newAPITestEnv(t)
	id := createImageWorkload(t, e, "metrics-app")
	resp := e.do(t, "GET", "/api/workloads/"+id+"/stats/history", nil)
	if resp.StatusCode != 200 {
		t.Fatalf("expected 200, got %d", resp.StatusCode)
	}
	var pts []workloadStatsPoint
	if errMsg := decodeEnvelope(t, resp, &pts); errMsg != "" {
		t.Fatalf("envelope error: %q", errMsg)
	}
	if len(pts) != 0 {
		t.Fatalf("expected empty series for app with no samples, got %d", len(pts))
	}
}
