package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"net/url"
	"time"
)

const LIBREX_DOMAIN = "librex.antopie.org"

type LibreXResult struct {
	Title       string `json:"title"`
	URL         string `json:"url"`
	Description string `json:"description"`
}

type LibreXResponse []LibreXResult

func PerformLibreXTextSearch(query, safe, lang string, page int) ([]TextSearchResult, time.Duration, error) {
	startTime := time.Now() // Start the timer

	// LibreX/Y uses offset instead of page that starts at 0
	page--
	page = page * 10

	searchURL := fmt.Sprintf("https://%s/api.php?q=%s&p=%d&t=0", LIBREX_DOMAIN, url.QueryEscape(query), page)

	// User Agent generation
	userAgent, err := GetUserAgent("librex-text-search")
	if err != nil {
		return nil, 0, err
	}

	req, err := http.NewRequest("GET", searchURL, nil)
	if err != nil {
		return nil, 0, err
	}
	req.Header.Set("User-Agent", userAgent)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return nil, 0, logError("error making request to LibreX", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, 0, logError("unexpected status code", fmt.Errorf("%d", resp.StatusCode))
	}

	var librexResp LibreXResponse
	if err := json.NewDecoder(resp.Body).Decode(&librexResp); err != nil {
		return nil, 0, logError("error decoding LibreX response", err)
	}

	var results []TextSearchResult
	for _, item := range librexResp {
		result := TextSearchResult{
			URL:         item.URL,
			Header:      item.Title,
			Description: item.Description,
			Source:      "LibreX",
		}

		results = append(results, result)
	}

	duration := time.Since(startTime) // Calculate the duration

	if len(results) == 0 {
		return nil, duration, fmt.Errorf("no results found")
	}

	return results, duration, nil
}

// This is just stupid it will probbably lead to printing error twice
func logError(message string, err error) error {
	log.Printf("%s: %v", message, err)
	return fmt.Errorf("%s: %w", message, err)
}