package controllers

import (
	"github.com/gogf/gf/v2/net/ghttp"
	"go.uber.org/dig"
	"yx-dataset-server/app/errors"
	"yx-dataset-server/app/schema"
	"yx-dataset-server/library/gplus"
)

// Inject 注入 controllers
func Inject(container *dig.Container) {
	_ = container.Provide(NewChatAssistant)
	_ = container.Provide(NewChatSession)
	_ = container.Provide(NewChatMessage)

	_ = container.Provide(NewLogin)

	//_ = container.Provide(NewRobotConfig)
}

func ResPage(r *ghttp.Request, data interface{}, pr *schema.PaginationResult) {
	result := schema.HTTPList{
		List: data,
		Pagination: &schema.HTTPPagination{
			Current:  gplus.GetPageIndex(r),
			PageSize: gplus.GetPageSize(r),
		},
	}
	if data != nil {
		result.Pagination.Total = pr.Total
	}
	r.Response.WriteJson(&schema.HttpResponse{
		Code:    0,
		Message: "success",
		Data:    result,
	})
	r.Exit()
}

func ResList(c *ghttp.Request, v interface{}) {
	ResSuccess(c, schema.HTTPList{List: v})
}

func ResSuccess(r *ghttp.Request, result interface{}) {
	r.Response.WriteJson(&schema.HttpResponse{
		Code:    0,
		Message: "success",
		Data:    result,
	})
	r.Exit()
}
func ResError(r *ghttp.Request, err error, status ...int) {
	var res *errors.ResponseError
	if err != nil {
		if e, ok := err.(*errors.ResponseError); ok {
			res = e
		} else {
			res = errors.UnWrapResponse(errors.Wrap500Response(err))
		}
	} else {

		res = errors.UnWrapResponse(errors.ErrInternalServer)
	}
	if res.Code == 0 {
		res.Code = -1
	}

	r.Response.WriteJson(&schema.HttpResponse{
		Code:    res.Code,
		Message: err.Error(),
		Data:    nil,
	})
	r.Exit()
}
