package rest

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"os"
	"strings"
	"testing"
)

func TestHandleWalletConnectConfig(t *testing.T) {
	t.Setenv("WALLETCONNECT_PROJECT_ID", "test-project-id")
	server := NewServer(nil, 138)

	req := httptest.NewRequest(http.MethodGet, "/api/v1/walletconnect/config", nil)
	rec := httptest.NewRecorder()
	server.handleWalletConnectConfig(rec, req)

	if rec.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d", rec.Code)
	}

	var payload map[string]interface{}
	if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
		t.Fatalf("decode response: %v", err)
	}
	if payload["projectId"] != "test-project-id" {
		t.Fatalf("expected project id, got %#v", payload["projectId"])
	}
	if payload["fallbackAuth"] != "/api/v1/auth/wallet" {
		t.Fatalf("expected fallback auth path, got %#v", payload["fallbackAuth"])
	}
}

func TestHandleWalletConnectConnectDisabled(t *testing.T) {
	t.Setenv("WALLETCONNECT_PROJECT_ID", "")
	server := NewServer(nil, 138)

	req := httptest.NewRequest(http.MethodPost, "/api/v1/walletconnect/connect", strings.NewReader("{}"))
	rec := httptest.NewRecorder()
	server.handleWalletConnectConnect(rec, req)

	if rec.Code != http.StatusNotImplemented {
		t.Fatalf("expected 501, got %d", rec.Code)
	}
}

func TestHandleWalletConnectConnectClientMode(t *testing.T) {
	t.Setenv("WALLETCONNECT_PROJECT_ID", "test-project-id")
	server := NewServer(nil, 138)

	req := httptest.NewRequest(http.MethodPost, "/api/v1/walletconnect/connect", strings.NewReader("{}"))
	rec := httptest.NewRecorder()
	server.handleWalletConnectConnect(rec, req)

	if rec.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d", rec.Code)
	}

	var payload map[string]interface{}
	if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
		t.Fatalf("decode response: %v", err)
	}
	if payload["status"] != "client" {
		t.Fatalf("expected client status, got %#v", payload["status"])
	}
}

func TestHandleWalletConnectSessionMissing(t *testing.T) {
	server := NewServer(nil, 138)

	req := httptest.NewRequest(http.MethodGet, "/api/v1/walletconnect/session/demo-session", nil)
	rec := httptest.NewRecorder()
	server.handleWalletConnectSession(rec, req)

	if rec.Code != http.StatusNotFound {
		t.Fatalf("expected 404, got %d", rec.Code)
	}
}

func TestHandleWalletConnectSessionRegister(t *testing.T) {
	server := NewServer(nil, 138)

	body := strings.NewReader(`{"sessionId":"wc-demo","address":"0x4A666F96fC8764181194447A7dFdb7d471b301C8","chainId":138}`)
	req := httptest.NewRequest(http.MethodPost, "/api/v1/walletconnect/session", body)
	rec := httptest.NewRecorder()
	server.handleWalletConnectSessionRegister(rec, req)

	if rec.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String())
	}

	getReq := httptest.NewRequest(http.MethodGet, "/api/v1/walletconnect/session/wc-demo", nil)
	getRec := httptest.NewRecorder()
	server.handleWalletConnectSession(getRec, getReq)
	if getRec.Code != http.StatusOK {
		t.Fatalf("expected lookup 200, got %d", getRec.Code)
	}
}

func TestHandleWalletConnectRootIndex(t *testing.T) {
	_ = os.Setenv("WALLETCONNECT_PROJECT_ID", "")
	server := NewServer(nil, 138)

	req := httptest.NewRequest(http.MethodGet, "/api/v1/walletconnect", nil)
	rec := httptest.NewRecorder()
	server.handleWalletConnectRoot(rec, req)

	if rec.Code != http.StatusOK {
		t.Fatalf("expected 200, got %d", rec.Code)
	}
}
