package controller

import (
	"campaign-pool-be/app/module/user_role_level_details/request"
	"campaign-pool-be/app/module/user_role_level_details/service"
	"campaign-pool-be/utils/paginator"
	"strconv"

	"github.com/gofiber/fiber/v2"

	utilRes "campaign-pool-be/utils/response"
	utilVal "campaign-pool-be/utils/validator"
)

type userRoleLevelDetailsController struct {
	userRoleLevelDetailsService service.UserRoleLevelDetailsService
}

type UserRoleLevelDetailsController 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 NewUserRoleLevelDetailsController(userRoleLevelDetailsService service.UserRoleLevelDetailsService) UserRoleLevelDetailsController {
	return &userRoleLevelDetailsController{
		userRoleLevelDetailsService: userRoleLevelDetailsService,
	}
}

// All get all UserRoleLevelDetails
// @Summary      Get all UserRoleLevelDetails
// @Description  API for getting all UserRoleLevelDetails
// @Tags         Task
// @Security     Bearer
// @Success      200  {object}  response.Response
// @Failure      401  {object}  response.Response
// @Failure      404  {object}  response.Response
// @Failure      422  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /user-role-level-details [get]
func (_i *userRoleLevelDetailsController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	var req request.UserRoleLevelDetailsQueryRequest
	req.Pagination = paginate

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

	return utilRes.Resp(c, utilRes.Response{
		Messages: utilRes.Messages{"UserRoleLevelDetails list successfully retrieved"},
		Data:     userRoleLevelDetailsData,
		Meta:     paging,
	})
}

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

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

	return utilRes.Resp(c, utilRes.Response{
		Messages: utilRes.Messages{"UserRoleLevelDetails successfully retrieved"},
		Data:     userRoleLevelDetailsData,
	})
}

// Save create UserRoleLevelDetails
// @Summary      Create UserRoleLevelDetails
// @Description  API for create UserRoleLevelDetails
// @Tags         Task
// @Security     Bearer
// @Body 	     request.UserRoleLevelDetailsCreateRequest
// @Success      200  {object}  response.Response
// @Failure      401  {object}  response.Response
// @Failure      404  {object}  response.Response
// @Failure      422  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /user-role-level-details [post]
func (_i *userRoleLevelDetailsController) Save(c *fiber.Ctx) error {
	req := new(request.UserRoleLevelDetailsCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

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

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

// Update update UserRoleLevelDetails
// @Summary      update UserRoleLevelDetails
// @Description  API for update UserRoleLevelDetails
// @Tags         Task
// @Security     Bearer
// @Body 	     request.UserRoleLevelDetailsUpdateRequest
// @Param        id path int true "UserRoleLevelDetails ID"
// @Success      200  {object}  response.Response
// @Failure      401  {object}  response.Response
// @Failure      404  {object}  response.Response
// @Failure      422  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /user-role-level-details/{id} [put]
func (_i *userRoleLevelDetailsController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

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

// Delete delete UserRoleLevelDetails
// @Summary      delete UserRoleLevelDetails
// @Description  API for delete UserRoleLevelDetails
// @Tags         Task
// @Security     Bearer
// @Param        id path int true "UserRoleLevelDetails ID"
// @Success      200  {object}  response.Response
// @Failure      401  {object}  response.Response
// @Failure      404  {object}  response.Response
// @Failure      422  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /user-role-level-details/{id} [delete]
func (_i *userRoleLevelDetailsController) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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