package sources

import (
	"context"
	"encoding/json"
	"errors"
	"net/http"
	"net/http/httptest"
	"sync/atomic"
	"testing"
	"time"

	"github.com/salvacybersec/keyhunter/pkg/recon"
)

func k8sStubHandler(t *testing.T, calls *int32) http.HandlerFunc {
	t.Helper()
	return func(w http.ResponseWriter, r *http.Request) {
		atomic.AddInt32(calls, 1)
		if r.URL.Path != "/api/v1/packages/search" {
			t.Errorf("unexpected path: %s", r.URL.Path)
		}
		if r.URL.Query().Get("ts_query_web") == "" {
			t.Errorf("missing ts_query_web param")
		}
		body := k8sSearchResponse{
			Packages: []k8sPackage{
				{
					PackageID:      "pkg-1",
					Name:           "openai-operator",
					NormalizedName: "openai-operator",
					Repository:     k8sRepo{Name: "community", Kind: 6},
				},
				{
					PackageID:      "pkg-2",
					Name:           "llm-secrets",
					NormalizedName: "llm-secrets",
					Repository:     k8sRepo{Name: "stable", Kind: 0},
				},
			},
		}
		w.Header().Set("Content-Type", "application/json")
		_ = json.NewEncoder(w).Encode(body)
	}
}

func TestKubernetes_SweepEmitsFindings(t *testing.T) {
	reg := syntheticRegistry()
	lim := recon.NewLimiterRegistry()
	_ = lim.For("k8s", 1000, 100)

	var calls int32
	srv := httptest.NewServer(k8sStubHandler(t, &calls))
	defer srv.Close()

	src := &KubernetesSource{
		BaseURL:  srv.URL,
		Registry: reg,
		Limiters: lim,
		Client:   NewClient(),
	}

	out := make(chan recon.Finding, 32)
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	done := make(chan error, 1)
	go func() { done <- src.Sweep(ctx, "", out); close(out) }()

	var findings []recon.Finding
	for f := range out {
		findings = append(findings, f)
	}
	if err := <-done; err != nil {
		t.Fatalf("Sweep error: %v", err)
	}

	// 2 keywords * 2 results = 4 findings
	if len(findings) != 4 {
		t.Fatalf("expected 4 findings, got %d", len(findings))
	}
	for _, f := range findings {
		if f.SourceType != "recon:k8s" {
			t.Errorf("SourceType=%q want recon:k8s", f.SourceType)
		}
	}
	if got := atomic.LoadInt32(&calls); got != 2 {
		t.Errorf("expected 2 server calls, got %d", got)
	}
}

func TestKubernetes_KindPaths(t *testing.T) {
	reg := syntheticRegistry()
	lim := recon.NewLimiterRegistry()
	_ = lim.For("k8s", 1000, 100)

	var calls int32
	srv := httptest.NewServer(k8sStubHandler(t, &calls))
	defer srv.Close()

	src := &KubernetesSource{
		BaseURL:  srv.URL,
		Registry: reg,
		Limiters: lim,
		Client:   NewClient(),
	}

	out := make(chan recon.Finding, 32)
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	done := make(chan error, 1)
	go func() { done <- src.Sweep(ctx, "", out); close(out) }()

	var findings []recon.Finding
	for f := range out {
		findings = append(findings, f)
	}
	if err := <-done; err != nil {
		t.Fatalf("Sweep error: %v", err)
	}

	// Check that kind=6 maps to kube-operator and kind=0 maps to helm in URLs.
	hasOperator := false
	hasHelm := false
	for _, f := range findings {
		if contains(f.Source, "/kube-operator/") {
			hasOperator = true
		}
		if contains(f.Source, "/helm/") {
			hasHelm = true
		}
	}
	if !hasOperator {
		t.Error("expected at least one finding with kube-operator path")
	}
	if !hasHelm {
		t.Error("expected at least one finding with helm path")
	}
}

func TestKubernetes_EnabledAlwaysTrue(t *testing.T) {
	s := &KubernetesSource{}
	if !s.Enabled(recon.Config{}) {
		t.Fatal("expected Enabled=true")
	}
}

func TestKubernetes_NameAndRate(t *testing.T) {
	s := &KubernetesSource{}
	if s.Name() != "k8s" {
		t.Errorf("unexpected name: %s", s.Name())
	}
	if s.Burst() != 1 {
		t.Errorf("burst: %d", s.Burst())
	}
	if !s.RespectsRobots() {
		t.Error("expected RespectsRobots=true")
	}
}

func TestKubernetes_CtxCancelled(t *testing.T) {
	reg := syntheticRegistry()
	lim := recon.NewLimiterRegistry()
	_ = lim.For("k8s", 1000, 100)

	src := &KubernetesSource{
		BaseURL:  "http://127.0.0.1:1",
		Registry: reg,
		Limiters: lim,
		Client:   NewClient(),
	}

	ctx, cancel := context.WithCancel(context.Background())
	cancel()

	out := make(chan recon.Finding, 1)
	err := src.Sweep(ctx, "", out)
	if !errors.Is(err, context.Canceled) {
		t.Fatalf("expected context.Canceled, got %v", err)
	}
}

func TestKubernetes_NilRegistryNoError(t *testing.T) {
	src := &KubernetesSource{Client: NewClient()}
	out := make(chan recon.Finding, 1)
	if err := src.Sweep(context.Background(), "", out); err != nil {
		t.Fatalf("expected nil, got %v", err)
	}
}

// contains checks if substr is in s. Avoids importing strings in test.
func contains(s, substr string) bool {
	for i := 0; i+len(substr) <= len(s); i++ {
		if s[i:i+len(substr)] == substr {
			return true
		}
	}
	return false
}
