package controller

import (
	"narasi-ahli-be/app/module/user_role_accesses/request"
	"narasi-ahli-be/app/module/user_role_accesses/service"
	"narasi-ahli-be/utils/paginator"
	"strconv"

	"github.com/gofiber/fiber/v2"

	utilRes "narasi-ahli-be/utils/response"
	utilVal "narasi-ahli-be/utils/validator"
)

type userRoleAccessesController struct {
	userRoleAccessesService service.UserRoleAccessesService
}

type UserRoleAccessesController 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 NewUserRoleAccessesController(userRoleAccessesService service.UserRoleAccessesService) UserRoleAccessesController {
	return &userRoleAccessesController{
		userRoleAccessesService: userRoleAccessesService,
	}
}

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

	reqContext := request.UserRoleAccessesQueryRequestContext{
		MenuId:     c.Query("menuId"),
		UserRoleId: c.Query("userRoleId"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

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

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

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

// Save UserRoleAccesses
// @Summary      Create UserRoleAccesses
// @Description  API for create UserRoleAccesses
// @Tags         UserRoleAccesses
// @Security     Bearer
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param 	     payload body request.UserRoleAccessesCreateRequest 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-role-accesses [post]
func (_i *userRoleAccessesController) Save(c *fiber.Ctx) error {
	req := new(request.UserRoleAccessesCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

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

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

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

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

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

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

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

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

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