package engines

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

	"github.com/ashie/gosearch/internal/contracts"
)

func TestQwantEngine_WebLite(t *testing.T) {
	transport := roundTripperFunc(func(r *http.Request) (*http.Response, error) {
		if r.Method != http.MethodGet {
			return httpResponse(http.StatusMethodNotAllowed, "", ""), nil
		}
		if r.URL.Host != "lite.qwant.com" {
			return httpResponse(http.StatusNotFound, "", ""), nil
		}
		if r.URL.Path != "/" {
			// goquery request URL parsing should normalize to "/"
			t.Fatalf("unexpected path: %s", r.URL.Path)
		}

		q := r.URL.Query().Get("q")
		if q != "hugo" {
			t.Fatalf("unexpected q: %q", q)
		}
		if r.URL.Query().Get("locale") != "en_us" {
			t.Fatalf("unexpected locale: %q", r.URL.Query().Get("locale"))
		}
		if r.URL.Query().Get("l") != "en" {
			t.Fatalf("unexpected l: %q", r.URL.Query().Get("l"))
		}
		if r.URL.Query().Get("s") != "0" {
			t.Fatalf("unexpected s: %q", r.URL.Query().Get("s"))
		}
		if r.URL.Query().Get("p") != "1" {
			t.Fatalf("unexpected p: %q", r.URL.Query().Get("p"))
		}

		body := `
<!doctype html>
<html>
  <body>
    <section>
      <article>
        <span class="url partner">https://example.com/q</span>
        <h2><a href="https://example.com/q">Qwant Title</a></h2>
        <p>Qwant description</p>
      </article>
      <article>
        <span class="tooltip">ad</span>
        <span class="url partner">https://example.com/ad</span>
        <h2><a href="https://example.com/ad">Ad Title</a></h2>
        <p>Ad description</p>
      </article>
    </section>
  </body>
</html>`

		return httpResponse(http.StatusOK, body, "text/html"), nil
	})

	client := &http.Client{Transport: transport}
	engine := &QwantEngine{client: client, category: "web-lite", resultsPerPage: 10}

	resp, err := engine.Search(context.Background(), contracts.SearchRequest{
		Query:      "hugo",
		Pageno:     1,
		Safesearch: 0,
		Language:   "en",
	})
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(resp.Results) != 1 {
		t.Fatalf("expected 1 result (non-ad), got %d", len(resp.Results))
	}
	if resp.Results[0].Title != "Qwant Title" {
		t.Fatalf("unexpected title: %q", resp.Results[0].Title)
	}
	if resp.Results[0].Content != "Qwant description" {
		t.Fatalf("unexpected content: %q", resp.Results[0].Content)
	}
	if resp.Results[0].URL == nil || *resp.Results[0].URL != "https://example.com/q" {
		t.Fatalf("unexpected url: %v", resp.Results[0].URL)
	}
}

