// Reply-path tests: a fake Grader plus an httptest server standing in for
// Discord's REST API. No live gateway is involved anywhere.

package gateway

import (
	"context"
	"encoding/json"
	"io"
	"net/http"
	"net/http/httptest"
	"sync"
	"sync/atomic"
	"testing"
	"time"
)

type fakeGrader struct {
	mu    sync.Mutex
	resp  json.RawMessage
	err   error
	calls []string
}

func (f *fakeGrader) Grade(_ context.Context, query string) (json.RawMessage, error) {
	f.mu.Lock()
	defer f.mu.Unlock()
	f.calls = append(f.calls, query)
	return f.resp, f.err
}

func (f *fakeGrader) callCount() int {
	f.mu.Lock()
	defer f.mu.Unlock()
	return len(f.calls)
}

type captured struct {
	method, path, auth string
	body               []byte
}

// newTestBot wires a Bot to a Grader and an httptest stand-in for Discord.
func newTestBot(t *testing.T, g Grader, handler http.HandlerFunc) *Bot {
	t.Helper()
	srv := httptest.NewServer(handler)
	t.Cleanup(srv.Close)
	b := New("tok-123", g)
	b.rest = srv.URL
	return b
}

// capture records each request and replies 200 {}.
func capture(got chan<- captured) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		body, _ := io.ReadAll(r.Body)
		got <- captured{r.Method, r.URL.Path, r.Header.Get("Authorization"), body}
		_, _ = w.Write([]byte(`{}`))
	}
}

func TestGradeReplyIsRelayedVerbatimAsAReply(t *testing.T) {
	grade := json.RawMessage(`{"embeds":[{"title":"grade:Phoenix"}]}`)
	got := make(chan captured, 1)
	b := newTestBot(t, &fakeGrader{resp: grade}, capture(got))

	b.handleMessage(context.Background(), testMsg("<@999> Phoenix", mentioning(botID)), botID)

	c := <-got
	if c.method != http.MethodPost || c.path != "/channels/77/messages" {
		t.Fatalf("want POST /channels/77/messages, got %s %s", c.method, c.path)
	}
	if c.auth != "Bot tok-123" {
		t.Fatalf("want bot-token auth, got %q", c.auth)
	}
	var top map[string]json.RawMessage
	if err := json.Unmarshal(c.body, &top); err != nil {
		t.Fatalf("reply body not JSON: %v", err)
	}
	// The embed must be byte-for-byte what Python returned — the verbatim
	// relay is the no-drift guarantee.
	if string(top["embeds"]) != `[{"title":"grade:Phoenix"}]` {
		t.Fatalf("embeds not relayed verbatim: %s", top["embeds"])
	}
	if string(top["message_reference"]) != `{"message_id":"42"}` {
		t.Fatalf("bad message_reference: %s", top["message_reference"])
	}
	// The locked-down allowed_mentions is the anti-@everyone control; it must
	// be present exactly.
	if string(top["allowed_mentions"]) != `{"parse":[],"replied_user":true}` {
		t.Fatalf("bad allowed_mentions: %s", top["allowed_mentions"])
	}
}

func TestEmptyQueryRepliesWithHelpWithoutGrading(t *testing.T) {
	got := make(chan captured, 1)
	g := &fakeGrader{}
	b := newTestBot(t, g, capture(got))

	b.handleMessage(context.Background(), testMsg("<@999>", mentioning(botID)), botID)

	c := <-got
	var body struct {
		Content string `json:"content"`
	}
	if err := json.Unmarshal(c.body, &body); err != nil {
		t.Fatalf("help body not JSON: %v", err)
	}
	if body.Content != helpText {
		t.Fatalf("want help text, got %q", body.Content)
	}
	if g.callCount() != 0 {
		t.Fatal("help reply must not hit the grade callback")
	}
}

func TestSilentMessagesMakeNoRequests(t *testing.T) {
	var hits atomic.Int32
	g := &fakeGrader{resp: json.RawMessage(`{}`)}
	b := newTestBot(t, g, func(w http.ResponseWriter, r *http.Request) {
		hits.Add(1)
	})

	b.handleMessage(context.Background(), testMsg("just chatting"), botID)

	if g.callCount() != 0 || hits.Load() != 0 {
		t.Fatalf("silent message leaked: grades=%d posts=%d", g.callCount(), hits.Load())
	}
}

func TestGradeFailureStaysSilent(t *testing.T) {
	var hits atomic.Int32
	g := &fakeGrader{err: context.DeadlineExceeded}
	b := newTestBot(t, g, func(w http.ResponseWriter, r *http.Request) {
		hits.Add(1)
	})

	// Must log-and-return, not panic or post a broken reply.
	b.handleMessage(context.Background(), testMsg("<@999> Phoenix", mentioning(botID)), botID)

	if hits.Load() != 0 {
		t.Fatal("failed grade must not produce a reply")
	}
}

func TestFailedReplyIsSwallowed(t *testing.T) {
	g := &fakeGrader{resp: json.RawMessage(`{"content":"x"}`)}
	b := newTestBot(t, g, func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "boom", http.StatusInternalServerError)
	})

	// A failed reply must never kill the caller (in production: the read
	// loop) — returning normally is the assertion.
	b.handleMessage(context.Background(), testMsg("<@999> Phoenix", mentioning(botID)), botID)
}

func TestReplyRetriesOnceOn429(t *testing.T) {
	var hits atomic.Int32
	bodies := make(chan []byte, 2)
	g := &fakeGrader{resp: json.RawMessage(`{"content":"x"}`)}
	b := newTestBot(t, g, func(w http.ResponseWriter, r *http.Request) {
		body, _ := io.ReadAll(r.Body)
		bodies <- body
		if hits.Add(1) == 1 {
			w.WriteHeader(http.StatusTooManyRequests)
			_, _ = w.Write([]byte(`{"retry_after":0.01}`))
			return
		}
		_, _ = w.Write([]byte(`{}`))
	})

	b.handleMessage(context.Background(), testMsg("<@999> Phoenix", mentioning(botID)), botID)

	if hits.Load() != 2 {
		t.Fatalf("want 1 retry after 429, got %d requests", hits.Load())
	}
	first, second := <-bodies, <-bodies
	if string(first) != string(second) {
		t.Fatal("retry must resend the identical payload")
	}
}

func TestNoChannelMeansNoPost(t *testing.T) {
	var hits atomic.Int32
	g := &fakeGrader{resp: json.RawMessage(`{"content":"x"}`)}
	b := newTestBot(t, g, func(w http.ResponseWriter, r *http.Request) {
		hits.Add(1)
	})

	b.handleMessage(context.Background(),
		testMsg("<@999> Phoenix", mentioning(botID), func(m *gwMessage) { m.ChannelID = "" }), botID)

	if hits.Load() != 0 {
		t.Fatal("a message without a channel id must not be answered")
	}
}

func TestInjectReplyFieldsWithoutMessageIDLeavesBodyAlone(t *testing.T) {
	out, err := injectReplyFields([]byte(`{"content":"x"}`), "")
	if err != nil {
		t.Fatalf("inject: %v", err)
	}
	var top map[string]json.RawMessage
	if err := json.Unmarshal(out, &top); err != nil {
		t.Fatalf("out not JSON: %v", err)
	}
	if _, ok := top["message_reference"]; ok {
		t.Fatal("no triggering id => no message_reference")
	}
	if _, ok := top["allowed_mentions"]; ok {
		t.Fatal("no triggering id => no allowed_mentions")
	}
}

// --- dispatch: same reply, off the read loop ---------------------------------

func TestDispatchDeliversTheSameReplyViaAGoroutine(t *testing.T) {
	// Mirrors the Python test_dispatch_delivers_the_same_reply_via_a_thread:
	// the concurrency offload (goroutine + semaphore here, asyncio.to_thread
	// there) must not change what gets sent.
	grade := json.RawMessage(`{"embeds":[{"title":"grade:Phoenix"}]}`)
	got := make(chan captured, 1)
	b := newTestBot(t, &fakeGrader{resp: grade}, capture(got))
	sess := &session{userID: botID}
	d, err := json.Marshal(testMsg("<@999> Phoenix", mentioning(botID)))
	if err != nil {
		t.Fatalf("marshal: %v", err)
	}

	b.dispatch(context.Background(), payload{Op: opDispatch, T: "MESSAGE_CREATE", D: d}, sess)

	select {
	case c := <-got:
		if c.path != "/channels/77/messages" {
			t.Fatalf("want /channels/77/messages, got %s", c.path)
		}
		var top map[string]json.RawMessage
		if err := json.Unmarshal(c.body, &top); err != nil {
			t.Fatalf("reply body not JSON: %v", err)
		}
		if string(top["embeds"]) != `[{"title":"grade:Phoenix"}]` {
			t.Fatalf("embeds not relayed verbatim: %s", top["embeds"])
		}
		if string(top["message_reference"]) != `{"message_id":"42"}` {
			t.Fatalf("bad message_reference: %s", top["message_reference"])
		}
	case <-time.After(2 * time.Second):
		t.Fatal("dispatched message never produced a reply")
	}
}

func TestMessageFloodIsBoundedWithoutBlockingTheReadLoop(t *testing.T) {
	g := &fakeGrader{resp: json.RawMessage(`{}`)}
	b := New("tok", g)
	b.sem = make(chan struct{}, 1)
	b.sem <- struct{}{} // occupy the only slot: simulate a reply in flight
	sess := &session{userID: botID}
	d, err := json.Marshal(testMsg("<@999> Phoenix", mentioning(botID)))
	if err != nil {
		t.Fatalf("marshal: %v", err)
	}

	done := make(chan struct{})
	go func() {
		b.dispatch(context.Background(), payload{Op: opDispatch, T: "MESSAGE_CREATE", D: d}, sess)
		close(done)
	}()
	select {
	case <-done:
		// dispatch returned promptly — the read loop would not have stalled
	case <-time.After(time.Second):
		t.Fatal("dispatch blocked on a full semaphore")
	}
	time.Sleep(20 * time.Millisecond)
	if g.callCount() != 0 {
		t.Fatal("over-the-bound message must be dropped, not queued")
	}
}
