package cli

import (
	"bytes"
	"encoding/json"
	"strings"
	"testing"
)

func TestStatusText(t *testing.T) {
	resetRootFlags(t)
	var buf bytes.Buffer
	rootCmd.SetOut(&buf)
	rootCmd.SetErr(&buf)
	rootCmd.SetArgs([]string{"status"})
	if err := rootCmd.Execute(); err != nil {
		t.Fatalf("status: %v", err)
	}
	out := buf.String()
	if !strings.Contains(out, "orca daemon status") {
		t.Errorf("status text output unexpected: %s", out)
	}
	if !strings.Contains(out, "version") {
		t.Errorf("status output missing version: %s", out)
	}
}

func TestStatusJSON(t *testing.T) {
	resetRootFlags(t)
	var buf bytes.Buffer
	rootCmd.SetOut(&buf)
	rootCmd.SetErr(&buf)
	rootCmd.SetArgs([]string{"status", "--json"})
	if err := rootCmd.Execute(); err != nil {
		t.Fatalf("status --json: %v", err)
	}
	var info map[string]any
	if err := json.Unmarshal(bytes.TrimSpace(buf.Bytes()), &info); err != nil {
		t.Fatalf("unmarshal status json: %v\n%s", err, buf.String())
	}
	if info["daemon"] != "stopped" {
		t.Errorf("status json daemon = %v, want stopped", info["daemon"])
	}
	if info["api_addr"] != "https://localhost:8443" {
		t.Errorf("status json api_addr = %v, want https://localhost:8443", info["api_addr"])
	}
}
