package handler_test

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

	"github.com/gin-gonic/gin"
	"github.com/user-management-system/internal/api/handler"
	"github.com/user-management-system/internal/service"
)

// =============================================================================
// Settings Handler Tests - TDD approach
// =============================================================================

func TestSettingsHandler_GetSettings(t *testing.T) {
	gin.SetMode(gin.TestMode)

	settingsSvc := service.NewSettingsService()
	h := handler.NewSettingsHandler(settingsSvc)

	t.Run("获取系统设置成功", func(t *testing.T) {
		w := httptest.NewRecorder()
		c, _ := gin.CreateTestContext(w)
		c.Request = httptest.NewRequest("GET", "/api/v1/admin/settings", nil)

		h.GetSettings(c)

		if w.Code != http.StatusOK {
			t.Errorf("期望状态码 %d, 得到 %d", http.StatusOK, w.Code)
		}

		var resp map[string]interface{}
		if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
			t.Fatalf("解析响应失败: %v", err)
		}

		if resp["code"].(float64) != 0 {
			t.Errorf("期望 code=0, 得到 %v", resp["code"])
		}

		data := resp["data"].(map[string]interface{})
		if data["system"] == nil {
			t.Error("system 不应为空")
		}
	})
}
