package cli

import (
	"bytes"
	"strings"
	"testing"

	"banger/internal/system"
)

func TestPrintDoctorReport_BriefAllPass(t *testing.T) {
	report := system.Report{}
	report.AddPass("first", "detail one")
	report.AddPass("second", "detail two")
	report.AddPass("third")

	var buf bytes.Buffer
	if err := printDoctorReport(&buf, report, false); err != nil {
		t.Fatalf("printDoctorReport: %v", err)
	}

	got := buf.String()
	want := "all 3 checks passed\n"
	if got != want {
		t.Fatalf("brief all-pass output\n got: %q\nwant: %q", got, want)
	}
}

func TestPrintDoctorReport_BriefHidesPassDetails(t *testing.T) {
	report := system.Report{}
	report.AddPass("first", "detail one")
	report.AddWarn("second", "warn detail")
	report.AddPass("third", "detail three")
	report.AddFail("fourth", "fail detail")

	var buf bytes.Buffer
	if err := printDoctorReport(&buf, report, false); err != nil {
		t.Fatalf("printDoctorReport: %v", err)
	}

	got := buf.String()
	if strings.Contains(got, "PASS") || strings.Contains(got, "first") || strings.Contains(got, "third") {
		t.Fatalf("brief mode leaked PASS rows: %q", got)
	}
	for _, want := range []string{"WARN\tsecond", "warn detail", "FAIL\tfourth", "fail detail"} {
		if !strings.Contains(got, want) {
			t.Fatalf("missing %q in brief output: %q", want, got)
		}
	}
	if !strings.Contains(got, "2 passed, 1 warning, 1 failure") {
		t.Fatalf("missing summary footer in: %q", got)
	}
}

func TestPrintDoctorReport_BriefSummaryPlurals(t *testing.T) {
	report := system.Report{}
	report.AddPass("a")
	report.AddWarn("b")
	report.AddWarn("c")

	var buf bytes.Buffer
	if err := printDoctorReport(&buf, report, false); err != nil {
		t.Fatalf("printDoctorReport: %v", err)
	}
	if !strings.Contains(buf.String(), "1 passed, 2 warnings, 0 failures") {
		t.Fatalf("plural counts wrong: %q", buf.String())
	}
}

func TestPrintDoctorReport_VerboseShowsEverything(t *testing.T) {
	report := system.Report{}
	report.AddPass("first", "detail one")
	report.AddWarn("second", "warn detail")

	var buf bytes.Buffer
	if err := printDoctorReport(&buf, report, true); err != nil {
		t.Fatalf("printDoctorReport: %v", err)
	}
	got := buf.String()
	for _, want := range []string{"PASS\tfirst", "detail one", "WARN\tsecond", "warn detail"} {
		if !strings.Contains(got, want) {
			t.Fatalf("verbose mode missing %q: %q", want, got)
		}
	}
	if strings.Contains(got, "passed,") {
		t.Fatalf("verbose mode should not print summary footer: %q", got)
	}
}
