package admin

import (
	"github.com/gin-gonic/gin"
	v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
	"github.com/go-nunu/nunu-layout-advanced/api/v1/admin"
	"github.com/go-nunu/nunu-layout-advanced/internal/handler"
	adminService "github.com/go-nunu/nunu-layout-advanced/internal/service/admin"
	"github.com/mcuadros/go-defaults"
	"net/http"
)

type LogHandler struct {
	*handler.Handler
	LogService adminService.LogService
}

func NewLogHandler(
    handler *handler.Handler,
    LogService adminService.LogService,
) *LogHandler {
	return &LogHandler{
		Handler:      handler,
		LogService: LogService,
	}
}

func (h *LogHandler) GetLog(ctx *gin.Context) {
	var req admin.LogId
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(&req)
	res, err := h.LogService.GetLog(ctx, req.Id)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *LogHandler) GetLogList(ctx *gin.Context) {
	var req admin.SearchLogParams
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(&req)
	res, err := h.LogService.GetLogList(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}
