package sources

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"time"

	"golang.org/x/time/rate"

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

// DevToSource searches the dev.to public API for articles containing leaked
// API keys. Developers write tutorials and guides on dev.to that sometimes
// include real credentials in code examples.
type DevToSource struct {
	BaseURL  string
	Registry *providers.Registry
	Limiters *recon.LimiterRegistry
	Client   *Client
}

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

func (s *DevToSource) Name() string               { return "devto" }
func (s *DevToSource) RateLimit() rate.Limit       { return rate.Every(1 * time.Second) }
func (s *DevToSource) Burst() int                  { return 5 }
func (s *DevToSource) RespectsRobots() bool        { return false }
func (s *DevToSource) Enabled(_ recon.Config) bool { return true }

// devtoArticleSummary represents an article in the dev.to /api/articles list response.
type devtoArticleSummary struct {
	ID  int    `json:"id"`
	URL string `json:"url"`
}

// devtoArticleDetail represents the full article from /api/articles/{id}.
type devtoArticleDetail struct {
	BodyMarkdown string `json:"body_markdown"`
	URL          string `json:"url"`
}

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

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

	for _, q := range queries {
		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
			}
		}

		// Search for articles by tag keyword.
		listURL := fmt.Sprintf("%s/api/articles?tag=%s&per_page=10&state=rising",
			base, url.QueryEscape(q))
		req, err := http.NewRequestWithContext(ctx, http.MethodGet, listURL, nil)
		if err != nil {
			continue
		}
		req.Header.Set("Accept", "application/json")

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

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

		var articles []devtoArticleSummary
		if err := json.Unmarshal(body, &articles); err != nil {
			continue
		}

		// Limit to first 5 articles to stay within rate limits.
		limit := 5
		if len(articles) < limit {
			limit = len(articles)
		}

		for _, article := range articles[:limit] {
			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
				}
			}

			// Fetch full article to get body_markdown.
			detailURL := fmt.Sprintf("%s/api/articles/%d", base, article.ID)
			detailReq, err := http.NewRequestWithContext(ctx, http.MethodGet, detailURL, nil)
			if err != nil {
				continue
			}
			detailReq.Header.Set("Accept", "application/json")

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

			detailBody, err := io.ReadAll(io.LimitReader(detailResp.Body, 256*1024))
			_ = detailResp.Body.Close()
			if err != nil {
				continue
			}

			var detail devtoArticleDetail
			if err := json.Unmarshal(detailBody, &detail); err != nil {
				continue
			}

			if ciLogKeyPattern.MatchString(detail.BodyMarkdown) {
				articleURL := detail.URL
				if articleURL == "" {
					articleURL = fmt.Sprintf("%s/api/articles/%d", base, article.ID)
				}
				out <- recon.Finding{
					ProviderName: q,
					Source:       articleURL,
					SourceType:   "recon:devto",
					Confidence:   "medium",
					DetectedAt:   time.Now(),
				}
			}
		}
	}
	return nil
}
