// kafka — a privacy-respecting metasearch engine
// Copyright (C) 2026-present metamorphosis-dev
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package engines

import (
	"context"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"regexp"
	"strings"

	"github.com/metamorphosis-dev/kafka/internal/contracts"
)

// googleUserAgent is an honest User-Agent identifying the metasearch engine.
// Using a spoofed GSA User-Agent violates Google's Terms of Service and
// risks permanent IP blocking.
var googleUserAgent = "Kafka/0.1 (compatible; +https://github.com/metamorphosis-dev/kafka)"

type GoogleEngine struct {
	client *http.Client
}

func (e *GoogleEngine) Name() string { return "google" }

func (e *GoogleEngine) Search(ctx context.Context, req contracts.SearchRequest) (contracts.SearchResponse, error) {
	if strings.TrimSpace(req.Query) == "" {
		return contracts.SearchResponse{Query: req.Query}, nil
	}

	start := (req.Pageno - 1) * 10
	query := url.QueryEscape(req.Query)

	u := fmt.Sprintf(
		"https://www.google.com/search?q=%s&filter=0&start=%d&hl=%s&lr=%s&safe=%s",
		query,
		start,
		googleHL(req.Language),
		googleUILanguage(req.Language),
		googleSafeSearchLevel(req.Safesearch),
	)

	httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
	if err != nil {
		return contracts.SearchResponse{}, err
	}
	httpReq.Header.Set("User-Agent", googleUserAgent)
	httpReq.Header.Set("Accept", "*/*")
	httpReq.AddCookie(&http.Cookie{Name: "CONSENT", Value: "YES+"})

	resp, err := e.client.Do(httpReq)
	if err != nil {
		return contracts.SearchResponse{}, err
	}
	defer resp.Body.Close()

	// Check for Google block / CAPTCHA page.
	if detectGoogleSorry(resp) {
		return contracts.SearchResponse{
			Query:               req.Query,
			NumberOfResults:     0,
			Results:             nil,
			Answers:             []map[string]any{},
			Corrections:         []string{},
			Infoboxes:           []map[string]any{},
			Suggestions:         []string{},
			UnresponsiveEngines: [][2]string{{"google", "blocked by Google (CAPTCHA/sorry page)"}},
		}, nil
	}

	if resp.StatusCode != http.StatusOK {
		io.Copy(io.Discard, io.LimitReader(resp.Body, 4096))
		return contracts.SearchResponse{}, fmt.Errorf("google error: status %d", resp.StatusCode)
	}

	body, err := io.ReadAll(io.LimitReader(resp.Body, 128*1024))
	if err != nil {
		return contracts.SearchResponse{}, err
	}

	results := parseGoogleResults(string(body), req.Query)
	return contracts.SearchResponse{
		Query:               req.Query,
		NumberOfResults:     len(results),
		Results:             results,
		Answers:             []map[string]any{},
		Corrections:         []string{},
		Infoboxes:           []map[string]any{},
		Suggestions:         extractGoogleSuggestions(string(body)),
		UnresponsiveEngines: [][2]string{},
	}, nil
}

func detectGoogleSorry(resp *http.Response) bool {
	if resp.Request != nil {
		if resp.Request.URL.Host == "sorry.google.com" || strings.HasPrefix(resp.Request.URL.Path, "/sorry") {
			return true
		}
	}
	return false
}

func parseGoogleResults(body, query string) []contracts.MainResult {
	var results []contracts.MainResult

	mjjPattern := regexp.MustCompile(`<div[^>]*class="[^"]*MjjYud[^"]*"[^>]*>(.*?)</div>`)
	matches := mjjPattern.FindAllStringSubmatch(body, -1)

	for i, match := range matches {
		if len(match) < 2 {
			continue
		}
		block := match[1]

		urlPattern := regexp.MustCompile(`<a[^>]+href="(/url\?q=[^"&]+)`)
		urlMatch := urlPattern.FindStringSubmatch(block)
		if len(urlMatch) < 2 {
			continue
		}
		rawURL := urlMatch[1]
		actualURL := strings.TrimPrefix(rawURL, "/url?q=")
		if amp := strings.Index(actualURL, "&amp;"); amp != -1 {
			actualURL = actualURL[:amp]
		}
		if decoded, err := url.QueryUnescape(actualURL); err == nil {
			actualURL = decoded
		}

		if actualURL == "" || !strings.HasPrefix(actualURL, "http") {
			continue
		}

		titlePattern := regexp.MustCompile(`<span[^>]*class="[^"]*qrStP[^"]*"[^>]*>([^<]+)</span>`)
		titleMatch := titlePattern.FindStringSubmatch(block)
		title := query
		if len(titleMatch) >= 2 {
			title = stripTags(titleMatch[1])
		} else {
			linkTitlePattern := regexp.MustCompile(`<a[^>]+role="link"[^>]*>([^<]+)<`)
			ltMatch := linkTitlePattern.FindStringSubmatch(block)
			if len(ltMatch) >= 2 {
				title = stripTags(ltMatch[1])
			}
		}

		snippet := extractGoogleSnippet(block)

		urlPtr := actualURL
		results = append(results, contracts.MainResult{
			Title:    title,
			URL:       &urlPtr,
			Content:   snippet,
			Engine:    "google",
			Score:     float64(len(matches) - i),
			Category:  "general",
			Engines:   []string{"google"},
			Template:  "default.html",
		})
	}

	return results
}

func extractGoogleSnippet(block string) string {
	snippetPattern := regexp.MustCompile(`<div[^>]+data-sncf="1"[^>]*>(.*?)</div>`)
	matches := snippetPattern.FindAllStringSubmatch(block, -1)
	var parts []string
	for _, m := range matches {
		if len(m) < 2 {
			continue
		}
		text := stripTags(m[1])
		if text != "" {
			parts = append(parts, text)
		}
	}
	return strings.Join(parts, " ")
}

func extractGoogleSuggestions(body string) []string {
	var suggestions []string
	suggestionPattern := regexp.MustCompile(`(?s)<div[^>]*class="[^"]*ouy7Mc[^"]*"[^>]*>.*?<a[^>]*>([^<]+)</a>`)
	matches := suggestionPattern.FindAllStringSubmatch(body, -1)
	seen := map[string]bool{}
	for _, m := range matches {
		if len(m) < 2 {
			continue
		}
		s := strings.TrimSpace(stripTags(m[1]))
		if s != "" && !seen[s] {
			seen[s] = true
			suggestions = append(suggestions, s)
		}
	}
	return suggestions
}

func googleHL(lang string) string {
	lang = strings.ToLower(strings.TrimSpace(lang))
	if lang == "" || lang == "auto" {
		return "en"
	}
	return lang
}

func googleUILanguage(lang string) string {
	lang = strings.ToLower(strings.Split(lang, "-")[0])
	if lang == "" || lang == "auto" {
		return ""
	}
	return "lang_" + lang
}

func googleSafeSearchLevel(safesearch int) string {
	switch safesearch {
	case 0:
		return "off"
	case 1:
		return "medium"
	case 2:
		return "high"
	default:
		return "medium"
	}
}

func stripTags(s string) string {
	stripper := regexp.MustCompile(`<[^>]*>`)
	s = stripper.ReplaceAllString(s, "")
	s = strings.ReplaceAll(s, "&amp;", "&")
	s = strings.ReplaceAll(s, "&quot;", `"`)
	s = strings.ReplaceAll(s, "&#39;", "'")
	s = strings.ReplaceAll(s, "&nbsp;", " ")
	return strings.TrimSpace(s)
}
