package org

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

	"github.com/gin-gonic/gin"
	"github.com/onsi/gomega"

	"github.com/pingcap-inc/clinic/pkg/api/env"
	"github.com/pingcap-inc/clinic/pkg/resource_limit"
)

var service = OrganizationService{
	quotalimiter: resource_limit.NewQuotaLimiter(),
}

func TestList(t *testing.T) {
	g := gomega.NewWithT(t)
	params := []gin.Param{
		{
			Key:   "oid",
			Value: "1",
		},
		{
			Key:   "x-csrf-token",
			Value: "1111",
		},
		{
			Key:   "cid",
			Value: "Test_01_1",
		},
	}
	status := env.EnvironmentInit("GET", service.List, params, nil, "", service.rbac, &service.cfg.SinkConfig.LogConfig)
	g.Expect(status).To(gomega.Equal(http.StatusOK))
}

func TestGet(t *testing.T) {
	g := gomega.NewWithT(t)
	params := []gin.Param{
		{
			Key:   "oid",
			Value: "1",
		},
		{
			Key:   "x-csrf-token",
			Value: "1111",
		},
	}
	status := env.EnvironmentInit("GET", service.Get, params, nil, "", service.rbac, &service.cfg.SinkConfig.LogConfig)
	g.Expect(status).To(gomega.Equal(http.StatusOK))
}

func TestListRoles(t *testing.T) {
	g := gomega.NewWithT(t)
	params := []gin.Param{
		{
			Key:   "oid",
			Value: "1",
		},
		{
			Key:   "x-csrf-token",
			Value: "1111",
		},
	}
	status := env.EnvironmentInit("GET", service.ListRoles, params, nil, "", service.rbac, &service.cfg.SinkConfig.LogConfig)
	g.Expect(status).To(gomega.Equal(http.StatusOK))
}

func TestCreate(t *testing.T) {
	g := gomega.NewWithT(t)
	params := []gin.Param{
		{
			Key:   "x-csrf-token",
			Value: "1111",
		},
	}
	reqMap := map[string]any{
		"clusterCount": 0,
		"description":  "test for create org",
		"id":           "1",
		"isStarred":    true,
		"lastUpdated":  0,
		"name":         "test_user",
		"roleNames":    []string{"admin"},
		"type":         "admin",
	}
	body, err := json.Marshal(reqMap)
	if err != nil {
		fmt.Println("TestPostReq json.Marshal err:", err)
		return
	}
	status := env.EnvironmentInit("POST", service.Create, params, body, "", service.rbac, &service.cfg.SinkConfig.LogConfig)
	g.Expect(status).To(gomega.Equal(http.StatusOK))
}

func TestUpdate(t *testing.T) {
	g := gomega.NewWithT(t)
	params := []gin.Param{
		{
			Key:   "x-csrf-token",
			Value: "1111",
		},
		{
			Key:   "oid",
			Value: "1",
		},
	}
	reqMap := map[string]any{
		"name":        "update_01",
		"description": "test for update org",
	}
	body, err := json.Marshal(reqMap)
	if err != nil {
		fmt.Println("TestPostReq json.Marshal err:", err)
		return
	}
	status := env.EnvironmentInit("PUT", service.Update, params, body, "", service.rbac, &service.cfg.SinkConfig.LogConfig)
	g.Expect(status).To(gomega.Equal(http.StatusOK))
}

func TestStar(t *testing.T) {
	g := gomega.NewWithT(t)
	params := []gin.Param{
		{
			Key:   "x-csrf-token",
			Value: "1111",
		},
		{
			Key:   "oid",
			Value: "1",
		},
	}

	status := env.EnvironmentInit("POST", service.Star, params, nil, "", service.rbac, &service.cfg.SinkConfig.LogConfig)
	g.Expect(status).To(gomega.Equal(http.StatusOK))
}

func TestUnStar(t *testing.T) {
	g := gomega.NewWithT(t)
	params := []gin.Param{
		{
			Key:   "x-csrf-token",
			Value: "1111",
		},
		{
			Key:   "oid",
			Value: "1",
		},
	}

	status := env.EnvironmentInit("DELETE", service.UnStar, params, nil, "", service.rbac, &service.cfg.SinkConfig.LogConfig)
	g.Expect(status).To(gomega.Equal(http.StatusOK))
}
