package main

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

type Instance struct {
	URL      string `json:"url"`
	Status   int    `json:"status"`
	SSLGrade string `json:"ssl_grade"`
}

const searxInstancesURL = "https://searx.space/data/instances.json"

func fetchInstances() ([]Instance, error) {
	client := &http.Client{Timeout: 10 * time.Second}
	req, err := http.NewRequest("GET", searxInstancesURL, nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")

	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

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

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	var instances []Instance
	err = json.Unmarshal(body, &instances)
	if err != nil {
		return nil, err
	}

	return instances, nil
}

func validateInstance(instance Instance) bool {
	client := &http.Client{Timeout: 10 * time.Second}
	req, err := http.NewRequest("GET", fmt.Sprintf("%s/search?q=test&categories=general&language=en&safe_search=1&page=1&format=json", instance.URL), nil)
	if err != nil {
		log.Printf("Error creating request for URL: %s, Error: %v", instance.URL, err)
		return false
	}
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")

	resp, err := client.Do(req)
	if err != nil {
		log.Printf("Error performing request for URL: %s, Error: %v", instance.URL, err)
		return false
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		log.Printf("Instance validation failed for URL: %s, StatusCode: %d", instance.URL, resp.StatusCode)
		return false
	}

	return true
}

func getValidInstance() (*Instance, error) {
	instances, err := fetchInstances()
	if err != nil {
		return nil, fmt.Errorf("failed to fetch instances: %w", err)
	}

	for _, instance := range instances {
		if validateInstance(instance) {
			return &instance, nil
		}
	}

	return nil, fmt.Errorf("no valid SearX instances found")
}

// func main() {
// 	instance, err := getValidInstance()
// 	if err != nil {
// 		log.Fatalf("Failed to get a valid SearX instance: %v", err)
// 	}

// 	log.Printf("Selected SearX instance: %s", instance.URL)
// }