package status

import (
	"context"
	"strings"
	"testing"

	"iop/packages/go/config"
)

func TestNewChecker_CodexUsesProfileCommand(t *testing.T) {
	checker, err := NewChecker("codex", config.CLIProfileConf{
		Command: "/tmp/mycodex",
	})
	if err != nil {
		t.Fatalf("NewChecker failed: %v", err)
	}

	codex, ok := checker.(*CodexChecker)
	if !ok {
		t.Fatalf("expected *CodexChecker, got %T", checker)
	}

	if codex.command != "/tmp/mycodex" {
		t.Errorf("expected command /tmp/mycodex, got %q", codex.command)
	}
}

func TestNewChecker_FallbackToCmdBase(t *testing.T) {
	checker, err := NewChecker("some-other", config.CLIProfileConf{
		Command: "/usr/bin/codex",
	})
	if err != nil {
		t.Fatalf("NewChecker failed: %v", err)
	}

	if _, ok := checker.(*CodexChecker); !ok {
		t.Errorf("expected *CodexChecker for command ending in codex, got %T", checker)
	}
}

func TestUnsupportedTargetReturnsError(t *testing.T) {
	_, err := CheckUsage(context.Background(), "unknown", config.CLIProfileConf{Command: "unknown"})
	if err == nil || !strings.Contains(err.Error(), "status check not supported") {
		t.Errorf("expected not supported error, got %v", err)
	}
}

func TestNewChecker_ClaudeUsesProfileCommand(t *testing.T) {
	checker, err := NewChecker("claude", config.CLIProfileConf{
		Command: "/tmp/myclaude",
	})
	if err != nil {
		t.Fatalf("NewChecker failed: %v", err)
	}

	claude, ok := checker.(*ClaudeChecker)
	if !ok {
		t.Fatalf("expected *ClaudeChecker, got %T", checker)
	}

	if claude.command != "/tmp/myclaude" {
		t.Errorf("expected command /tmp/myclaude, got %q", claude.command)
	}
}

func TestNewChecker_ClaudeTUIUsesClaudeChecker(t *testing.T) {
	checker, err := NewChecker("claude-tui", config.CLIProfileConf{
		Command: "/tmp/claude-wrapper",
	})
	if err != nil {
		t.Fatalf("NewChecker failed: %v", err)
	}

	claude, ok := checker.(*ClaudeChecker)
	if !ok {
		t.Fatalf("expected *ClaudeChecker, got %T", checker)
	}

	if claude.command != "/tmp/claude-wrapper" {
		t.Errorf("expected command /tmp/claude-wrapper, got %q", claude.command)
	}
}

func TestNewChecker_AntigravityUsesProfileCommand(t *testing.T) {
	checker, err := NewChecker("antigravity", config.CLIProfileConf{
		Command: "/tmp/myantigravity",
	})
	if err != nil {
		t.Fatalf("NewChecker failed: %v", err)
	}

	antigravity, ok := checker.(*AntigravityChecker)
	if !ok {
		t.Fatalf("expected *AntigravityChecker, got %T", checker)
	}

	if antigravity.command != "/tmp/myantigravity" {
		t.Errorf("expected command /tmp/myantigravity, got %q", antigravity.command)
	}
}

func TestNewChecker_AntigravityEmptyCommandDefaultsToAgy(t *testing.T) {
	checker, err := NewChecker("antigravity", config.CLIProfileConf{
		Command: "",
	})
	if err != nil {
		t.Fatalf("NewChecker failed: %v", err)
	}

	antigravity, ok := checker.(*AntigravityChecker)
	if !ok {
		t.Fatalf("expected *AntigravityChecker, got %T", checker)
	}

	if antigravity.command != "agy" {
		t.Errorf("expected default command agy, got %q", antigravity.command)
	}
}

func TestNewChecker_AgyCommandBaseUsesAntigravityChecker(t *testing.T) {
	checker, err := NewChecker("some-other", config.CLIProfileConf{
		Command: "/usr/bin/agy",
	})
	if err != nil {
		t.Fatalf("NewChecker failed: %v", err)
	}

	if _, ok := checker.(*AntigravityChecker); !ok {
		t.Errorf("expected *AntigravityChecker for command ending in agy, got %T", checker)
	}
}

func TestNewChecker_ClaudeEmptyCommandDefaultsToClaude(t *testing.T) {
	checker, err := NewChecker("claude", config.CLIProfileConf{
		Command: "",
	})
	if err != nil {
		t.Fatalf("NewChecker failed: %v", err)
	}

	claude, ok := checker.(*ClaudeChecker)
	if !ok {
		t.Fatalf("expected *ClaudeChecker, got %T", checker)
	}

	if claude.command != "claude" {
		t.Errorf("expected default command claude, got %q", claude.command)
	}
}
