package subscription

import (
	"encoding/json"
	"io"
	"net/http"
	"net/http/httptest"
	"testing"
)

const sampleBase64Sub = `c3M6Ly9ZV1Z6TFRFeU9DMW5ZMjA2Wm1Fd1lqZ3laamN0Tm1VelppMWhZakZoTFdJMk5qZ3RZamhqTlRjeU5ERTJNakpqUUdWNGNHbHlaUzEwYVcxbExtTjVZbVZ5Ym05a1pTNTBiM0E2TVRrNE5ERT0jJUU1JThCJUJGJUU5JTgwJTg5LSVFNSU4OCVCMCVFNiU5QyU5RiVFNiU5NyVCNiVFOSU5NyVCNCVFRiVCQyU5QTIwMjYtMDktMDMKc3M6Ly9ZV1Z6TFRFeU9DMW5ZMjA2Wm1Fd1lqZ3laamN0Tm1VelppMWhZakZoTFdJMk5qZ3RZamhqTlRjeU5ERTJNakpqUUhKbGJXRnBiaTEwY21GbVptbGpMbU41WW1WeWJtOWtaUzUwYjNBNk1UazROREk9IyVFNSU4QiVCRiVFOSU4MCU4OS0lRTUlQkQlOTMlRTYlOUMlODglRTUlODklQTklRTQlQkQlOTklRTYlQjUlODElRTklODclOEYlRUYlQkMlOUEyNi4wOUdCCnNzOi8vWVdWekxURXlPQzFuWTIwNlptRXdZamd5WmpjdE5tVXpaaTFoWWpGaExXSTJOamd0WWpoak5UY3lOREUyTWpKalFERXVNUzR4TGpFNk5UazFNREk9IyVFMyU4MCU5MCVFOSU4MCU5QSVFNyU5RiVBNSVFMyU4MCU5MSVFNSVBNiU4MiVFOSU5QyU4MCVFNiU5QiVCNCVFNSVBNCU5QSVFOCU4QSU4MiVFNyU4MiVCOSVFOCVBRiVCNyVFOSU4NyU4RCVFOCVBMyU4NUFwcApzczovL1lXVnpMVEV5T0MxblkyMDZabUV3WWpneVpqY3RObVV6WmkxaFlqRmhMV0kyTmpndFlqaGpOVGN5TkRFMk1qSmpRREV1TVM0eExqRTZOVGsxTURNPSMlRTglODElOTQlRTclQjMlQkIlRTklODIlQUUlRTclQUUlQjElM0FsaWJjeWJlcjd4MjQlNDBnbWFpbC5jb20Kc3M6Ly9ZV1Z6TFRFeU9DMW5ZMjA2Wm1Fd1lqZ3laamN0Tm1VelppMWhZakZoTFdJMk5qZ3RZamhqTlRjeU5ERTJNakpqUURFd015NHhPREV1TVRZMExqRXdORG8yTVRFd01RPT0jJUU5JUFCJTk4JUU5JTgwJTlGJUU4JUFGJTk1JUU3JTk0JUE4LSVFNiVCMyU5NSVFNSU5QiVCRDEKc3M6Ly9ZV1Z6TFRFeU9DMW5ZMjA2Wm1Fd1lqZ3laamN0Tm1VelppMWhZakZoTFdJMk5qZ3RZamhqTlRjeU5ERTJNakpqUURFd015NHhPREV1TVRZMExqRXdORG8yTURBNE9BPT0jJUU5JUFCJTk4JUU5JTgwJTlGJUU1JUE0JTg3JUU3JTk0JUE4LSVFNCVCQiU4NSVFOSU5OSU5MCVFNCVCOCVCNCVFNiU5NyVCNiVFNCVCRCVCRiVFNyU5NCVBOAo=`

func TestParseXHSCacheBase64Subscription(t *testing.T) {
	svc := NewService(nil)
	parsed, err := svc.parseSubscription([]byte(sampleBase64Sub), 1, "auto")
	if err != nil {
		t.Fatalf("parse failed: %v", err)
	}
	if len(parsed.Nodes) != 6 {
		t.Fatalf("expected 6 nodes, got %d", len(parsed.Nodes))
	}
	if parsed.Nodes[0].Type != "ss" {
		t.Fatalf("expected ss type, got %s", parsed.Nodes[0].Type)
	}
	var cfg map[string]any
	if err := json.Unmarshal([]byte(parsed.Nodes[0].RawConfig), &cfg); err != nil {
		t.Fatal(err)
	}
	if cfg["server"] != "expire-time.cybernode.top" {
		t.Fatalf("unexpected server: %v", cfg["server"])
	}
	if cfg["cipher"] != "aes-128-gcm" {
		t.Fatalf("unexpected cipher: %v", cfg["cipher"])
	}
	if parsed.Nodes[0].Name == "" {
		t.Fatal("expected decoded node name")
	}
}

func TestDetectFormatBase64Links(t *testing.T) {
	if got := detectFormat(string(decodeMaybeBase64([]byte(sampleBase64Sub)))); got != "links" {
		t.Fatalf("expected links, got %s", got)
	}
}

func TestFetchURLFollowsRequest(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Header.Get("User-Agent") == "" {
			t.Fatal("missing user agent")
		}
		_, _ = io.WriteString(w, sampleBase64Sub)
	}))
	defer srv.Close()

	svc := NewService(nil)
	body, err := svc.fetchURL(srv.URL)
	if err != nil {
		t.Fatal(err)
	}
	if len(body) == 0 {
		t.Fatal("empty body")
	}
}
