package controller

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

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

type masterModulesController struct {
	masterModulesService service.MasterModulesService
}

type MasterModulesController 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 NewMasterModulesController(masterModulesService service.MasterModulesService) MasterModulesController {
	return &masterModulesController{
		masterModulesService: masterModulesService,
	}
}

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

	reqContext := request.MasterModulesQueryRequestContext{
		Name:        c.Query("name"),
		Description: c.Query("description"),
		StatusId:    c.Query("statusId"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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