package catalogclient

import (
	"context"
	"encoding/json"
	"errors"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"git.agentview.dev/profit/golangLAKEHOUSE/internal/catalogd"
)

func TestRegister_Success(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/catalog/register" || r.Method != http.MethodPost {
			http.Error(w, "wrong route", http.StatusNotFound)
			return
		}
		w.Header().Set("Content-Type", "application/json")
		_ = json.NewEncoder(w).Encode(RegisterResponse{
			Manifest: &catalogd.Manifest{Name: "x", DatasetID: "id-1"},
			Existing: false,
		})
	}))
	defer srv.Close()

	c := New(srv.URL)
	resp, err := c.Register(context.Background(), &RegisterRequest{
		Name:              "x",
		SchemaFingerprint: "sha256:abc",
	})
	if err != nil {
		t.Fatal(err)
	}
	if resp.Manifest.DatasetID != "id-1" {
		t.Errorf("dataset_id: got %s, want id-1", resp.Manifest.DatasetID)
	}
}

func TestRegister_ConflictMapsToSentinel(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		http.Error(w, "fingerprint conflict", http.StatusConflict)
	}))
	defer srv.Close()

	c := New(srv.URL)
	_, err := c.Register(context.Background(), &RegisterRequest{
		Name:              "x",
		SchemaFingerprint: "sha256:abc",
	})
	if !errors.Is(err, ErrFingerprintConflict) {
		t.Fatalf("expected ErrFingerprintConflict, got %v", err)
	}
}

func TestList_Success(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/catalog/list" || r.Method != http.MethodGet {
			http.Error(w, "wrong route", http.StatusNotFound)
			return
		}
		w.Header().Set("Content-Type", "application/json")
		_, _ = w.Write([]byte(`{"manifests":[{"name":"a"},{"name":"b"}],"count":2}`))
	}))
	defer srv.Close()

	c := New(srv.URL)
	got, err := c.List(context.Background())
	if err != nil {
		t.Fatal(err)
	}
	if len(got) != 2 || got[0].Name != "a" || got[1].Name != "b" {
		t.Errorf("List: got %+v, want [a b]", got)
	}
}

func TestList_Non200Errors(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		http.Error(w, "boom", http.StatusInternalServerError)
	}))
	defer srv.Close()

	c := New(srv.URL)
	_, err := c.List(context.Background())
	if err == nil || !strings.Contains(err.Error(), "list status 500") {
		t.Fatalf("expected status 500 error, got %v", err)
	}
}
