package sources

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

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

func googleStubHandler(t *testing.T, calls *int32) http.HandlerFunc {
	t.Helper()
	return func(w http.ResponseWriter, r *http.Request) {
		atomic.AddInt32(calls, 1)
		if !strings.HasPrefix(r.URL.Path, "/customsearch/v1") {
			t.Errorf("unexpected path: %s", r.URL.Path)
		}
		if r.URL.Query().Get("key") != "testkey" {
			t.Errorf("missing api key in query")
		}
		if r.URL.Query().Get("cx") != "testcx" {
			t.Errorf("missing cx in query")
		}
		body := map[string]any{
			"items": []map[string]any{
				{"title": "result1", "link": "https://pastebin.com/abc123", "snippet": "found key"},
				{"title": "result2", "link": "https://github.com/org/repo/blob/main/env", "snippet": "another"},
			},
		}
		w.Header().Set("Content-Type", "application/json")
		_ = json.NewEncoder(w).Encode(body)
	}
}

func TestGoogleDorkSource_EnabledRequiresBothKeys(t *testing.T) {
	reg := syntheticRegistry()
	lim := recon.NewLimiterRegistry()

	tests := []struct {
		apiKey, cx string
		want       bool
	}{
		{"", "", false},
		{"key", "", false},
		{"", "cx", false},
		{"key", "cx", true},
	}
	for _, tt := range tests {
		s := NewGoogleDorkSource(tt.apiKey, tt.cx, reg, lim)
		if got := s.Enabled(recon.Config{}); got != tt.want {
			t.Errorf("Enabled(apiKey=%q, cx=%q) = %v, want %v", tt.apiKey, tt.cx, got, tt.want)
		}
	}
}

func TestGoogleDorkSource_SweepEmptyCredsReturnsNil(t *testing.T) {
	reg := syntheticRegistry()
	lim := recon.NewLimiterRegistry()
	s := NewGoogleDorkSource("", "", reg, lim)

	out := make(chan recon.Finding, 10)
	if err := s.Sweep(context.Background(), "", out); err != nil {
		t.Fatalf("expected nil err, got %v", err)
	}
	close(out)
	if n := countFindings(out); n != 0 {
		t.Fatalf("expected 0 findings, got %d", n)
	}
}

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

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

	s := NewGoogleDorkSource("testkey", "testcx", reg, lim)
	s.BaseURL = srv.URL

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

	done := make(chan error, 1)
	go func() { done <- s.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 items = 4 findings
	if len(findings) != 4 {
		t.Fatalf("expected 4 findings, got %d", len(findings))
	}
	for _, f := range findings {
		if f.SourceType != "recon:google" {
			t.Errorf("SourceType=%q want recon:google", f.SourceType)
		}
		if f.Confidence != "low" {
			t.Errorf("Confidence=%q want low", f.Confidence)
		}
	}
	if got := atomic.LoadInt32(&calls); got != 2 {
		t.Errorf("expected 2 API calls, got %d", got)
	}
}

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

	s := NewGoogleDorkSource("key", "cx", reg, lim)
	s.BaseURL = "http://127.0.0.1:1"

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

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

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

	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusUnauthorized)
		_, _ = w.Write([]byte("bad key"))
	}))
	defer srv.Close()

	s := NewGoogleDorkSource("key", "cx", reg, lim)
	s.BaseURL = srv.URL

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