package sources

import (
	"context"
	"fmt"
	"io"
	"net/http"
	"regexp"
	"time"

	"golang.org/x/time/rate"

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

// WebpackSource probes for Webpack/Vite build artifacts that contain inlined
// environment variables. Bundlers like Webpack and Vite inline process.env.*
// values at build time, frequently shipping API keys to production bundles.
type WebpackSource struct {
	BaseURL  string
	Registry *providers.Registry
	Limiters *recon.LimiterRegistry
	Client   *Client
}

var _ recon.ReconSource = (*WebpackSource)(nil)

func (s *WebpackSource) Name() string              { return "webpack" }
func (s *WebpackSource) RateLimit() rate.Limit      { return rate.Every(3 * time.Second) }
func (s *WebpackSource) Burst() int                 { return 2 }
func (s *WebpackSource) RespectsRobots() bool       { return true }
func (s *WebpackSource) Enabled(_ recon.Config) bool { return true }

// envVarPattern matches inlined environment variable patterns from bundlers.
var envVarPattern = regexp.MustCompile(`(?i)(NEXT_PUBLIC_|REACT_APP_|VITE_|VUE_APP_|NUXT_|GATSBY_)[A-Z_]*(API[_]?KEY|SECRET|TOKEN|PASSWORD)['":\s]*[=:,]\s*['"]([a-zA-Z0-9_\-]{8,})['"]`)

// webpackBundlePaths are common locations for JS bundle artifacts.
var webpackBundlePaths = []string{
	"/static/js/main.js",
	"/static/js/bundle.js",
	"/_next/static/chunks/main.js",
	"/assets/index.js",
	"/dist/bundle.js",
	"/build/static/js/main.js",
}

func (s *WebpackSource) Sweep(ctx context.Context, _ string, out chan<- recon.Finding) error {
	base := s.BaseURL
	if base == "" {
		return nil
	}
	client := s.Client
	if client == nil {
		client = NewClient()
	}

	queries := BuildQueries(s.Registry, "webpack")
	if len(queries) == 0 {
		return nil
	}

	for _, q := range queries {
		if err := ctx.Err(); err != nil {
			return err
		}

		for _, path := range webpackBundlePaths {
			if err := ctx.Err(); err != nil {
				return err
			}

			if s.Limiters != nil {
				if err := s.Limiters.Wait(ctx, s.Name(), s.RateLimit(), s.Burst(), false); err != nil {
					return err
				}
			}

			probeURL := fmt.Sprintf("%s%s", base, path)
			req, err := http.NewRequestWithContext(ctx, http.MethodGet, probeURL, nil)
			if err != nil {
				continue
			}

			resp, err := client.Do(ctx, req)
			if err != nil {
				continue
			}

			body, err := io.ReadAll(io.LimitReader(resp.Body, 512*1024)) // 512KB max
			_ = resp.Body.Close()
			if err != nil {
				continue
			}

			if envVarPattern.Match(body) {
				out <- recon.Finding{
					ProviderName: q,
					Source:       probeURL,
					SourceType:   "recon:webpack",
					Confidence:   "medium",
					DetectedAt:   time.Now(),
				}
				break // one finding per query is sufficient
			}
		}
	}
	return nil
}
