package controller

import (
	"netidhub-saas-be/app/module/user_roles/request"
	"netidhub-saas-be/app/module/user_roles/service"
	"netidhub-saas-be/utils/paginator"
	utilRes "netidhub-saas-be/utils/response"
	utilVal "netidhub-saas-be/utils/validator"
	"strconv"

	"github.com/gofiber/fiber/v2"
	"github.com/rs/zerolog"
)

type userRolesController struct {
	userRolesService service.UserRolesService
	Log              zerolog.Logger
}

type UserRolesController interface {
	All(c *fiber.Ctx) error
	Show(c *fiber.Ctx) error
	Save(c *fiber.Ctx) error
	Update(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
}

func NewUserRolesController(userRolesService service.UserRolesService, log zerolog.Logger) UserRolesController {
	return &userRolesController{
		userRolesService: userRolesService,
		Log:              log,
	}
}

// All UserRoles
// @Summary      Get all UserRoles
// @Description  API for getting all UserRoles
// @Tags         UserRoles
// @Security     Bearer
// @Param 	     req query request.UserRolesQueryRequest false "query parameters"
// @Param 	     req query paginator.Pagination false "pagination parameters"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /user-roles [get]
func (_i *userRolesController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.UserRolesQueryRequestContext{
		Name:        c.Query("name"),
		Description: c.Query("description"),
		Code:        c.Query("code"),
		UserLevelId: c.Query("userLevelId"),
		StatusId:    c.Query("statusId"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

	userRolesData, paging, err := _i.userRolesService.All(req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"UserRoles list successfully retrieved"},
		Data:     userRolesData,
		Meta:     paging,
	})
}

// Show UserRoles
// @Summary      Get one UserRoles
// @Description  API for getting one UserRoles
// @Tags         UserRoles
// @Security     Bearer
// @Param        id path int true "UserRoles ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /user-roles/{id} [get]
func (_i *userRolesController) Show(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	userRolesData, err := _i.userRolesService.Show(uint(id))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"UserRoles successfully retrieved"},
		Data:     userRolesData,
	})
}

// Save UserRoles
// @Summary      Create UserRoles
// @Description  API for create UserRoles
// @Tags         UserRoles
// @Security     Bearer
// @Param        X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param 	     payload body request.UserRolesCreateRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /user-roles [post]
func (_i *userRolesController) Save(c *fiber.Ctx) error {
	req := new(request.UserRolesCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	authToken := c.Get("Authorization")

	err := _i.userRolesService.Save(*req, authToken)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"UserRoles successfully created"},
	})
}

// Update UserRoles
// @Summary      update UserRoles
// @Description  API for update UserRoles
// @Tags         UserRoles
// @Security     Bearer
// @Param        X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @Param 	     payload body request.UserRolesUpdateRequest true "Required payload"
// @Param        id path int true "UserRoles ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /user-roles/{id} [put]
func (_i *userRolesController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	req := new(request.UserRolesUpdateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	err = _i.userRolesService.Update(uint(id), *req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"UserRoles successfully updated"},
	})
}

// Delete UserRoles
// @Summary      delete UserRoles
// @Description  API for delete UserRoles
// @Tags         UserRoles
// @Security     Bearer
// @Param        X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @Param        id path int true "UserRoles ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /user-roles/{id} [delete]
func (_i *userRolesController) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	err = _i.userRolesService.Delete(uint(id))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"UserRoles successfully deleted"},
	})
}
