package controller

import (
	"github.com/gofiber/fiber/v2"
	"go-humas-be/app/module/user_levels/request"
	"go-humas-be/app/module/user_levels/service"
	"go-humas-be/utils/paginator"
	"strconv"

	utilRes "go-humas-be/utils/response"
	utilVal "go-humas-be/utils/validator"
)

type userLevelsController struct {
	userLevelsService service.UserLevelsService
}

type UserLevelsController 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 NewUserLevelsController(userLevelsService service.UserLevelsService) UserLevelsController {
	return &userLevelsController{
		userLevelsService: userLevelsService,
	}
}

// All UserLevels
// @Summary      Get all UserLevels
// @Description  API for getting all UserLevels
// @Tags         UserLevels
// @Security     Bearer
// @Param 	     req query request.UserLevelsQueryRequest 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-levels [get]
func (_i *userLevelsController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.UserLevelsQueryRequestContext{
		Name:          c.Query("name"),
		LevelNumber:   c.Query("levelNumber"),
		ParentLevelId: c.Query("parentLevelId"),
		ProvinceId:    c.Query("provinceId"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

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

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

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

// Save UserLevels
// @Summary      Create UserLevels
// @Description  API for create UserLevels
// @Tags         UserLevels
// @Security     Bearer
// @Param 	     payload body request.UserLevelsCreateRequest 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-levels [post]
func (_i *userLevelsController) Save(c *fiber.Ctx) error {
	req := new(request.UserLevelsCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

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

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

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

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

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

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

// Delete UserLevels
// @Summary      delete UserLevels
// @Description  API for delete UserLevels
// @Tags         UserLevels
// @Security     Bearer
// @Param        id path int true "UserLevels 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-levels/{id} [delete]
func (_i *userLevelsController) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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