package controller

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

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

type masterMenusController struct {
	masterMenusService service.MasterMenusService
}

type MasterMenusController 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 NewMasterMenusController(masterMenusService service.MasterMenusService) MasterMenusController {
	return &masterMenusController{
		masterMenusService: masterMenusService,
	}
}

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

	reqContext := request.MasterMenusQueryRequestContext{
		Name:         c.Query("name"),
		Description:  c.Query("description"),
		ParentMenuId: c.Query("parentMenuId"),
		ModuleId:     c.Query("moduleId"),
		StatusId:     c.Query("statusId"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

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

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

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

// Save create MasterMenus
// @Summary      Create MasterMenus
// @Description  API for create MasterMenus
// @Tags         MasterMenus
// @Security     Bearer
// @Param 	     payload body request.MasterMenusCreateRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /master-menus [post]
func (_i *masterMenusController) Save(c *fiber.Ctx) error {
	req := new(request.MasterMenusCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

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

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

// Update MasterMenus
// @Summary      Update MasterMenus
// @Description  API for update MasterMenus
// @Tags         MasterMenus
// @Security     Bearer
// @Body 	     request.MasterMenusUpdateRequest
// @Param        id path int true "MasterMenus 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       /master-menus/{id} [put]
func (_i *masterMenusController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

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

// Delete MasterMenus
// @Summary      Delete MasterMenus
// @Description  API for delete MasterMenus
// @Tags         MasterMenus
// @Security     Bearer
// @Param        id path int true "MasterMenus ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /master-menus/{id} [delete]
func (_i *masterMenusController) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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