package sources

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

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

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

const pastebinSearchHTML = `<!doctype html>
<html><body>
  <a href="/Ab12Cd34">paste one</a>
  <a href="/Ef56Gh78">paste two</a>
  <a href="/about">nope</a>
  <a href="/toolong123">nine chars nope</a>
</body></html>`

const pastebinRawContent1 = `some text with sk-proj-AAAA1234 leaked here`
const pastebinRawContent2 = `nothing interesting in this paste`

func TestPastebin_Sweep_ExtractsFindings(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch {
		case r.URL.Path == "/search":
			w.Header().Set("Content-Type", "text/html")
			_, _ = w.Write([]byte(pastebinSearchHTML))
		case r.URL.Path == "/raw/Ab12Cd34":
			_, _ = w.Write([]byte(pastebinRawContent1))
		case r.URL.Path == "/raw/Ef56Gh78":
			_, _ = w.Write([]byte(pastebinRawContent2))
		default:
			http.NotFound(w, r)
		}
	}))
	defer srv.Close()

	src := &PastebinSource{
		BaseURL:  srv.URL,
		Registry: pastebinTestRegistry(),
		Limiters: recon.NewLimiterRegistry(),
		Client:   NewClient(),
	}

	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)
	}
	// Only paste one has "sk-proj-", paste two doesn't match.
	if len(findings) != 1 {
		t.Fatalf("expected 1 finding, got %d", len(findings))
	}
	f := findings[0]
	if f.SourceType != "recon:pastebin" {
		t.Errorf("SourceType=%s, want recon:pastebin", f.SourceType)
	}
	if f.ProviderName != "openai" {
		t.Errorf("ProviderName=%s, want openai", f.ProviderName)
	}
	wantSource := srv.URL + "/Ab12Cd34"
	if f.Source != wantSource {
		t.Errorf("Source=%s, want %s", f.Source, wantSource)
	}
}

func TestPastebin_NameAndRate(t *testing.T) {
	s := &PastebinSource{}
	if s.Name() != "pastebin" {
		t.Errorf("Name=%s", s.Name())
	}
	if s.Burst() != 1 {
		t.Errorf("Burst=%d", s.Burst())
	}
	if !s.RespectsRobots() {
		t.Error("expected RespectsRobots=true")
	}
	if !s.Enabled(recon.Config{}) {
		t.Error("expected Enabled=true")
	}
}

func TestPastebin_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(pastebinSearchHTML))
	}))
	defer srv.Close()

	src := &PastebinSource{
		BaseURL:  srv.URL,
		Registry: pastebinTestRegistry(),
		Limiters: recon.NewLimiterRegistry(),
		Client:   NewClient(),
	}
	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")
	}
}
