package governance

import (
	"github.com/gin-gonic/gin"

	"fonchain-fiee/api/governance"
	"fonchain-fiee/pkg/model/login"
	"fonchain-fiee/pkg/service"
)

func Create(ctx *gin.Context) {
	var req governance.CreateReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	user := login.GetUserInfoFromC(ctx)
	// user := login.Info{
	// 	Name: "test" + strconv.Itoa(rand.Intn(10)),
	// 	ID:   uint64(rand.Intn(10)),
	// }
	req.Operator = user.Name
	req.OperatorId = int32(user.ID)
	resp, err := service.GovernanceProvider.Create(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func Delete(ctx *gin.Context) {
	var req governance.DeleteReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.GovernanceProvider.Delete(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func Display(ctx *gin.Context) {
	resp, err := service.GovernanceProvider.Display(ctx, &governance.DisplayReq{})
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func List(ctx *gin.Context) {
	var req governance.ListReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.GovernanceProvider.List(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}

func Edit(ctx *gin.Context) {
	var req governance.EditReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	user := login.GetUserInfoFromC(ctx)
	req.Operator = user.Name
	req.OperatorId = int32(user.ID)
	resp, err := service.GovernanceProvider.Edit(ctx, &req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
}
