package services

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

	"github.com/stretchr/testify/assert"
	"go.uber.org/zap"
)

func TestStreamService_StartProcessing(t *testing.T) {
	// Setup mock server
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		assert.Equal(t, "/internal/jobs/transcode", r.URL.Path)
		assert.Equal(t, "POST", r.Method)
		assert.Equal(t, "application/json", r.Header.Get("Content-Type"))

		var req TranscodeRequest
		err := json.NewDecoder(r.Body).Decode(&req)
		assert.NoError(t, err)
		assert.Equal(t, "123", req.TrackID)
		assert.Equal(t, "/path/to/file", req.FilePath)

		w.WriteHeader(http.StatusOK)
	}))
	defer server.Close()

	logger := zap.NewNop()
	service := NewStreamService(server.URL, logger)

	err := service.StartProcessing(context.Background(), 123, "/path/to/file")
	assert.NoError(t, err)
}

func TestStreamService_StartProcessing_Error(t *testing.T) {
	// Setup mock server that returns error
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusInternalServerError)
	}))
	defer server.Close()

	logger := zap.NewNop()
	service := NewStreamService(server.URL, logger)

	err := service.StartProcessing(context.Background(), 123, "/path/to/file")
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "stream server returned status: 500")
}
