package http

import (
	"fmt"
	"git.gocasts.ir/ebhomengo/niki/domain/authorization/service"
	"github.com/labstack/echo/v4"
	"net/http"
)

type RoleHandler struct {
	service service.Authorization
}

func NewRoleHandler(service service.Authorization) RoleHandler {
	return RoleHandler{service: service}
}

func (r RoleHandler) Store(c echo.Context) error {
	var request service.StoreRoleRequest
	if err := c.Bind(&request); err != nil {
		return c.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
	}

	role, err := r.service.Store(c.Request().Context(), request)
	if err != nil {
		return c.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
	}

	return c.JSON(http.StatusOK, echo.Map{"message": fmt.Sprintf("role %s created successfully", role.Title_fa)})
}

func (r RoleHandler) Update(c echo.Context) error {
	var request service.UpdateRoleRequest
	if err := c.Bind(&request); err != nil {
		return c.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
	}

	role, err := r.service.Update(c.Request().Context(), request)
	if err != nil {
		return c.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
	}

	return c.JSON(http.StatusOK, echo.Map{"message": fmt.Sprintf("role %s updated successfully", role.Title_fa)})
}
