package openai

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

	edgeservice "iop/apps/edge/internal/service"
)

func TestSingleRequestChatStreamPumpProjectsOnlyFinalResult(t *testing.T) {
	execution := startSingleRequestAnthropicTestExecution(t, func(
		_ context.Context,
		req edgeservice.SingleRequestRequest,
		ctrl edgeservice.SingleRequestController,
	) error {
		for index, stage := range []edgeservice.SingleRequestState{
			edgeservice.SingleRequestStatePlanning,
			edgeservice.SingleRequestStateWorking,
			edgeservice.SingleRequestStateReviewing,
			edgeservice.SingleRequestStateFinalizing,
		} {
			envelope := edgeservice.SingleRequestEnvelope{
				RequestID: req.RequestID, Sequence: uint64(index + 1), Stage: stage,
			}
			if stage == edgeservice.SingleRequestStateFinalizing {
				envelope.Result = &edgeservice.SingleRequestResult{Output: "safe final"}
			}
			if err := ctrl.SubmitEnvelope(envelope); err != nil {
				return err
			}
		}
		return nil
	})
	w := httptest.NewRecorder()
	stream, err := newSingleRequestChatStream(w, "req_chat", "virtual-model")
	if err != nil {
		t.Fatal(err)
	}
	ticker := newManualSingleRequestAnthropicTicker()
	if err := pumpSingleRequestChatStream(context.Background(), execution, stream, func() singleRequestAnthropicTicker { return ticker }); err != nil {
		t.Fatal(err)
	}
	body := w.Body.String()
	if !strings.Contains(body, `"content":"safe final"`) || !strings.Contains(body, `"finish_reason":"stop"`) || strings.Count(body, "data: [DONE]") != 1 {
		t.Fatalf("unexpected Chat stream: %s", body)
	}
	for _, private := range []string{"Planning the requested work", "Executing the requested work", "Reviewing the completed work"} {
		if strings.Contains(body, private) {
			t.Fatalf("internal progress leaked: %s", body)
		}
	}
}

func TestSingleRequestChatStreamSanitizesExecutorFailure(t *testing.T) {
	execution := startSingleRequestAnthropicTestExecution(t, func(
		_ context.Context,
		req edgeservice.SingleRequestRequest,
		ctrl edgeservice.SingleRequestController,
	) error {
		if err := ctrl.SubmitEnvelope(edgeservice.SingleRequestEnvelope{RequestID: req.RequestID, Sequence: 1, Stage: edgeservice.SingleRequestStatePlanning}); err != nil {
			return err
		}
		return errors.New("PRIVATE_EXECUTOR_FAILURE")
	})
	w := httptest.NewRecorder()
	stream, err := newSingleRequestChatStream(w, "req_chat_error", "virtual-model")
	if err != nil {
		t.Fatal(err)
	}
	ticker := newManualSingleRequestAnthropicTicker()
	if err := pumpSingleRequestChatStream(context.Background(), execution, stream, func() singleRequestAnthropicTicker { return ticker }); err != nil {
		t.Fatal(err)
	}
	body := w.Body.String()
	if strings.Contains(body, "PRIVATE_EXECUTOR_FAILURE") || !strings.Contains(body, "single-request execution failed") || strings.Count(body, "data: [DONE]") != 1 {
		t.Fatalf("unexpected sanitized Chat error: %s", body)
	}
}

func TestMarkedSingleRequestSkipsLegacyWorkspaceToolBinding(t *testing.T) {
	dispatch := routeDispatch{
		Preset:        validSingleRequestPreset(),
		SingleRequest: newSingleRequestAnthropicTestBinding(t),
	}
	binding, pinArtifact, err := (&Server{}).compilePresetArtifactBinding(
		dispatch,
		"openai",
		[]byte(`{"model":"virtual-model","messages":[{"role":"user","content":"task"}]}`),
	)
	if err != nil || binding != nil || pinArtifact {
		t.Fatalf("marked single request entered legacy workspace binding: binding=%+v pin=%t err=%v", binding, pinArtifact, err)
	}
}
