package controller

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

	"github.com/gofiber/fiber/v2"

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

type userLevelsController struct {
	userLevelsService service.UserLevelsService
}

type UserLevelsController interface {
	All(c *fiber.Ctx) error
	Show(c *fiber.Ctx) error
	ShowByAlias(c *fiber.Ctx) error
	Save(c *fiber.Ctx) error
	Update(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
	EnableApproval(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,
	})
}

// ShowByAlias UserLevels
// @Summary      Get one UserLevels
// @Description  API for getting one UserLevels
// @Tags         UserLevels
// @Security     Bearer
// @Param        alias path string true "UserLevels Alias"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /user-levels/alias/{alias} [get]
func (_i *userLevelsController) ShowByAlias(c *fiber.Ctx) error {
	alias := c.Params("alias")
	userLevelsData, err := _i.userLevelsService.ShowByAlias(alias)
	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        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @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
	}

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

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

// Update UserLevels
// @Summary      update UserLevels
// @Description  API for update UserLevels
// @Tags         UserLevels
// @Security     Bearer
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @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        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @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"},
	})
}

// EnableApproval Articles
// @Summary      EnableApproval Articles
// @Description  API for Enable Approval of Article
// @Tags         UserLevels
// @Security     Bearer
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param 	     payload body request.UserLevelsApprovalRequest 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/enable-approval [post]
func (_i *userLevelsController) EnableApproval(c *fiber.Ctx) error {
	req := new(request.UserLevelsApprovalRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	ids := strings.Split(req.Ids, ",")
	for _, id := range ids {
		idUint, err := strconv.ParseUint(id, 10, 64)
		if err != nil {
			return err
		}
		err = _i.userLevelsService.EnableApproval(uint(idUint), req.IsApprovalActive)
		if err != nil {
			return err
		}
	}

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