/*
 * Copyright 2026 Oleg Borodin <onborodin@gmail.com>
 */
package router

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestRouterStatus(t *testing.T) {

	reqPath := "/hello"
	handler := NewRouter()
	helloHandler := func(rctx *Context) {
		rctx.SetStatus(http.StatusOK)
	}
	handler.Get(reqPath, helloHandler)

	request, err := http.NewRequest("GET", reqPath, nil)
	require.NoError(t, err)

	recorder := httptest.NewRecorder()
	handler.ServeHTTP(recorder, request)
	require.Equal(t, http.StatusOK, recorder.Code)

	fmt.Printf("Response code: %d\n", recorder.Code)
}

func TestRouterSendText(t *testing.T) {
	testText := "hello, world"
	reqPath := "/hello"
	rout := NewRouter()
	helloHandler := func(rctx *Context) {
		rctx.SendText(http.StatusOK, testText)
	}
	rout.Get(reqPath, helloHandler)

	request, err := http.NewRequest("GET", reqPath, nil)
	require.NoError(t, err)

	recorder := httptest.NewRecorder()
	rout.ServeHTTP(recorder, request)
	fmt.Printf("Response code: %d\n", recorder.Code)
	require.Equal(t, http.StatusOK, recorder.Code)

	bodyReader := recorder.Body
	bodyBytes, err := io.ReadAll(bodyReader)

	fmt.Printf("Response body: %s\n", string(bodyBytes))
	require.Equal(t, string(bodyBytes), testText)
}

func TestRouterSendJSON(t *testing.T) {

	type testStruct struct {
		Message string `json:"message"`
		Code    int64  `json:"code"`
	}
	testVar := testStruct{
		Message: "hello, world",
		Code:    123,
	}
	testData, err := json.Marshal(testVar)
	require.NoError(t, err)

	reqPath := "/hello"
	handler := NewRouter()

	helloHandler := func(rctx *Context) {
		rctx.SendJSON(http.StatusOK, &testVar)
	}
	handler.Get(reqPath, helloHandler)

	request, err := http.NewRequest("GET", reqPath, nil)
	require.NoError(t, err)

	recorder := httptest.NewRecorder()
	handler.ServeHTTP(recorder, request)
	fmt.Printf("Response code: %d\n", recorder.Code)
	require.Equal(t, http.StatusOK, recorder.Code)

	bodyReader := recorder.Body
	bodyBytes, err := io.ReadAll(bodyReader)
	require.NoError(t, err)
	bodyBytes = bytes.Trim(bodyBytes, "\n\r")

	fmt.Printf("Response body: %s\n", string(bodyBytes))
	require.Equal(t, string(testData), string(bodyBytes))
}

func TestRouterBindJSON(t *testing.T) {

	type testStruct struct {
		Message string `json:"message"`
		Code    int64  `json:code"`
	}
	testVar := testStruct{
		Message: "hello, world",
		Code:    123,
	}
	testData, err := json.Marshal(testVar)
	require.NoError(t, err)
	buffer := bytes.NewBuffer(testData)

	reqPath := "/hello"
	handler := NewRouter()

	helloHandler := func(rctx *Context) {
		handVar := testStruct{}
		rctx.BindJSON(&handVar)
		fmt.Printf("Received message: %s - %d\n", handVar.Message, handVar.Code)
		require.Equal(t, handVar.Code, int64(123))
		rctx.SendJSON(http.StatusOK, &handVar)
	}
	handler.Post(reqPath, helloHandler)

	request, err := http.NewRequest("POST", reqPath, buffer)
	require.NoError(t, err)

	recorder := httptest.NewRecorder()
	handler.ServeHTTP(recorder, request)

	fmt.Printf("Response code: %d\n", recorder.Code)
	require.Equal(t, http.StatusOK, recorder.Code)

	bodyReader := recorder.Body
	bodyBytes, err := io.ReadAll(bodyReader)
	require.NoError(t, err)
	bodyBytes = bytes.Trim(bodyBytes, "\n\r")

	fmt.Printf("Response body: %s\n", string(bodyBytes))
	require.Equal(t, string(testData), string(bodyBytes))
}

func TestRouterBindParams(t *testing.T) {

	reqPath := "/hello"
	handler := NewRouter()

	helloHandler := func(rctx *Context) {
		type Params struct {
			Name string `param:"name"`
			Code int64  `param:"code"`
		}
		params := &Params{}
		rctx.BindQuery(params)
		fmt.Printf("Received name: %s\n", params.Name)
		fmt.Printf("Received code: %d\n", params.Code)
		rctx.SendText(http.StatusOK, "hello")
	}
	handler.Get(reqPath, helloHandler)

	reqPath = reqPath + `?name=world&code=123`
	request, err := http.NewRequest("GET", reqPath, nil)
	require.NoError(t, err)

	recorder := httptest.NewRecorder()
	handler.ServeHTTP(recorder, request)

	fmt.Printf("Response code: %d\n", recorder.Code)
	require.Equal(t, http.StatusOK, recorder.Code)

	bodyReader := recorder.Body
	bodyBytes, err := io.ReadAll(bodyReader)
	require.NoError(t, err)
	bodyBytes = bytes.Trim(bodyBytes, "\n\r")

	fmt.Printf("Response body: %s\n", string(bodyBytes))
}

func BenchmarkLoggerL(b *testing.B) {
	reqPath := "/hello"
	helloHandler := func(rctx *Context) {
		rctx.SetStatus(http.StatusOK)
	}
	handler := NewRouter()
	handler.Get(reqPath, helloHandler)

	request, err := http.NewRequest("GET", reqPath, nil)
	require.NoError(b, err)

	for i := 0; i < b.N; i++ {
		recorder := httptest.NewRecorder()
		handler.ServeHTTP(recorder, request)
		require.Equal(b, http.StatusOK, recorder.Code)
	}
}
