package http

import (
	"git.gocasts.ir/ebhomengo/niki/domain/campaign/service"
	param "git.gocasts.ir/ebhomengo/niki/domain/campaign/service"
	"git.gocasts.ir/ebhomengo/niki/pkg/claim"
	httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
	"git.gocasts.ir/ebhomengo/niki/types"
	"github.com/labstack/echo/v4"
	"net/http"
)

type Handler struct {
	svc service.CampaignService
}

func NewHandler(svc service.CampaignService) Handler {
	return Handler{svc: svc}
}

func (h Handler) createCampaign(c echo.Context) error {

	claims := claim.GetClaimsFromEchoContext(c)

	var req param.CreateCampaignRequest

	req.AdminID = types.ID(claims.UserID)
	if err := c.Bind(&req); err != nil {
		return c.JSON(http.StatusBadRequest, map[string]string{
			"error": "invalid request body",
		})
	}

	createdID, err := h.svc.CreateCampaign(c.Request().Context(), req)
	if err != nil {
		msg, code := httpmsg.Error(err)
		c.Logger().Errorf("Service error creating campaign: %v (Code: %d)", err, code)
		return c.JSON(code, msg)
	}

	return c.JSON(http.StatusCreated, map[string]interface{}{
		"message": "campaign created successfully",
		"id":      createdID,
	})

}
