package sources

import (
	"context"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"

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

func pypiTestRegistry() *providers.Registry {
	return providers.NewRegistryFromProviders([]providers.Provider{
		{Name: "openai", Keywords: []string{"sk-proj-"}},
	})
}

const pypiFixtureHTML = `<!doctype html>
<html><body>
  <a href="/project/openai-leaked/">openai-leaked</a>
  <a href="/project/sk-proj-helper/">sk helper</a>
  <a href="/simple/">nope</a>
  <a href="https://external.example.com/project/nope/">external</a>
  <a href="/project/openai-leaked/">duplicate</a>
</body></html>`

func newPyPITestSource(srvURL string) *PyPISource {
	return &PyPISource{
		BaseURL:  srvURL,
		Registry: pypiTestRegistry(),
		Limiters: recon.NewLimiterRegistry(),
		Client:   NewClient(),
	}
}

func TestPyPI_Sweep_ExtractsFindings(t *testing.T) {
	var hits int
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/search/" {
			t.Errorf("unexpected path: %s", r.URL.Path)
		}
		if r.URL.Query().Get("q") == "" {
			t.Errorf("missing q param")
		}
		hits++
		w.Header().Set("Content-Type", "text/html")
		_, _ = w.Write([]byte(pypiFixtureHTML))
	}))
	defer srv.Close()

	src := newPyPITestSource(srv.URL)
	out := make(chan recon.Finding, 16)
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	if err := src.Sweep(ctx, "", out); err != nil {
		t.Fatalf("Sweep err: %v", err)
	}
	close(out)

	var findings []recon.Finding
	for f := range out {
		findings = append(findings, f)
	}
	// 2 unique /project/ links (duplicate is deduped by extractAnchorHrefs)
	if len(findings) != 2 {
		t.Fatalf("expected 2 findings, got %d", len(findings))
	}

	got := map[string]bool{}
	for _, f := range findings {
		got[f.Source] = true
		if f.SourceType != "recon:pypi" {
			t.Errorf("unexpected SourceType: %s", f.SourceType)
		}
		if f.Confidence != "low" {
			t.Errorf("unexpected Confidence: %s", f.Confidence)
		}
	}
	if !got[srv.URL+"/project/openai-leaked/"] {
		t.Error("missing openai-leaked finding")
	}
	if !got[srv.URL+"/project/sk-proj-helper/"] {
		t.Error("missing sk-proj-helper finding")
	}
	if hits == 0 {
		t.Fatal("server was never hit")
	}
}

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

func TestPyPI_Sweep_CtxCancelled(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(500 * time.Millisecond)
		_, _ = w.Write([]byte(pypiFixtureHTML))
	}))
	defer srv.Close()

	src := newPyPITestSource(srv.URL)
	ctx, cancel := context.WithCancel(context.Background())
	cancel()

	out := make(chan recon.Finding, 4)
	if err := src.Sweep(ctx, "", out); err == nil {
		t.Fatal("expected ctx error")
	}
}

func TestPyPI_NameAndRate(t *testing.T) {
	s := &PyPISource{}
	if s.Name() != "pypi" {
		t.Errorf("unexpected name: %s", s.Name())
	}
	if s.Burst() != 2 {
		t.Errorf("burst: %d", s.Burst())
	}
	if s.RespectsRobots() {
		t.Error("expected RespectsRobots=false")
	}
	want := float64(1) / 2
	got := float64(s.RateLimit())
	if got < want-0.01 || got > want+0.01 {
		t.Errorf("rate limit=%v want~%v", got, want)
	}
}
