package gitea

import (
	"context"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

// ListVersions filters to the requested package name and returns versions newest-semver first.
func TestListVersions_FilterAndSort(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if !strings.HasPrefix(r.URL.Path, "/api/v1/packages/admin") {
			http.NotFound(w, r)
			return
		}
		// Intentionally unsorted + a foreign package that must be filtered out.
		w.Header().Set("Content-Type", "application/json")
		w.Write([]byte(`[
			{"name":"felhom-agent","version":"0.43.0","type":"generic"},
			{"name":"felhom-agent","version":"0.52.0","type":"generic"},
			{"name":"felhom-golden","version":"0.85.1","type":"generic"},
			{"name":"felhom-agent","version":"0.9.0","type":"generic"}
		]`))
	}))
	defer srv.Close()

	c := New(srv.URL, "admin", "u", "t")
	vers, err := c.ListVersions(context.Background(), "felhom-agent")
	if err != nil {
		t.Fatalf("ListVersions: %v", err)
	}
	// Only felhom-agent, newest semver first (0.52.0 > 0.43.0 > 0.9.0 — numeric, not lexical).
	want := []string{"0.52.0", "0.43.0", "0.9.0"}
	if len(vers) != len(want) {
		t.Fatalf("got %v, want %v", vers, want)
	}
	for i := range want {
		if vers[i] != want[i] {
			t.Fatalf("got %v, want %v", vers, want)
		}
	}
}

// FileSHA256 prefers an exact filename match; otherwise it falls back to the first file.
func TestFileSHA256_PreferredAndFallback(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		switch {
		case strings.Contains(r.URL.Path, "/felhom-golden/0.85.1/files"):
			w.Write([]byte(`[
				{"name":"stray.txt","sha256":"aaaa"},
				{"name":"golden.tar.zst","sha256":"f87031cc"}
			]`))
		case strings.Contains(r.URL.Path, "/felhom-agent/0.52.0/files"):
			w.Write([]byte(`[{"name":"felhom-agent","sha256":"5bfc690c"}]`))
		default:
			http.NotFound(w, r)
		}
	}))
	defer srv.Close()

	c := New(srv.URL, "admin", "u", "t")

	// preferred-file match among multiple files
	sha, err := c.FileSHA256(context.Background(), "felhom-golden", "0.85.1", "golden.tar.zst")
	if err != nil || sha != "f87031cc" {
		t.Fatalf("golden sha: got %q err %v, want f87031cc", sha, err)
	}
	// single-file version resolves regardless of preferred name (fallback to first)
	sha, err = c.FileSHA256(context.Background(), "felhom-agent", "0.52.0", "felhom-agent")
	if err != nil || sha != "5bfc690c" {
		t.Fatalf("agent sha: got %q err %v, want 5bfc690c", sha, err)
	}
}

// A non-200 from Gitea surfaces as an error (so a save refuses rather than storing a blank sha).
func TestGetJSON_ErrorOnNon200(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "nope", http.StatusInternalServerError)
	}))
	defer srv.Close()
	c := New(srv.URL, "admin", "u", "t")
	if _, err := c.ListVersions(context.Background(), "felhom-agent"); err == nil {
		t.Fatal("expected an error on HTTP 500")
	}
}
