package router_test

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

	"galaxy/model/rest"

	"galaxy/game/internal/controller"
	"galaxy/game/internal/router"
	"galaxy/game/internal/router/handler"

	"github.com/google/uuid"
	"github.com/stretchr/testify/assert"
)

func TestGetReport(t *testing.T) {
	root := t.ArtifactDir()

	r := router.SetupRouter(handler.NewDefaultConfigExecutor(func(p *controller.Param) { p.StoragePath = root }))

	payload := generateInitRequest(10)

	w := httptest.NewRecorder()
	req, _ := http.NewRequest("POST", "/api/v1/admin/init", asBody(payload))
	r.ServeHTTP(w, req)

	assert.Equal(t, http.StatusCreated, w.Code, w.Body)
	var initResponse rest.StateResponse
	assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &initResponse))
	assert.NoError(t, uuid.Validate(initResponse.ID.String()))
	assert.NotEqual(t, uuid.Nil, uuid.MustParse(initResponse.ID.String()))

	w = httptest.NewRecorder()
	req, _ = http.NewRequest("GET", "/api/v1/report", nil)
	r.ServeHTTP(w, req)
	assert.Equal(t, http.StatusBadRequest, w.Code, w.Body)

	w = httptest.NewRecorder()
	req, _ = http.NewRequest("GET", "/api/v1/report?player=", nil)
	r.ServeHTTP(w, req)
	assert.Equal(t, http.StatusBadRequest, w.Code, w.Body)

	w = httptest.NewRecorder()
	req, _ = http.NewRequest("GET", "/api/v1/report?player=&turn=0", nil)
	r.ServeHTTP(w, req)
	assert.Equal(t, http.StatusBadRequest, w.Code, w.Body)

	w = httptest.NewRecorder()
	req, _ = http.NewRequest("GET", "/api/v1/report?player=Race_01&turn=-1", nil)
	r.ServeHTTP(w, req)
	assert.Equal(t, http.StatusBadRequest, w.Code, w.Body)

	w = httptest.NewRecorder()
	req, _ = http.NewRequest("GET", "/api/v1/report?player=Race_01&turn=0", nil)
	r.ServeHTTP(w, req)
	assert.Equal(t, http.StatusOK, w.Code, w.Body)
}
