package controller

import (
	"github.com/gofiber/fiber/v2"
	"netidhub-saas-be/app/module/master_statuses/request"
	"netidhub-saas-be/app/module/master_statuses/service"
	"netidhub-saas-be/utils/paginator"
	"strconv"

	utilRes "netidhub-saas-be/utils/response"
	utilVal "netidhub-saas-be/utils/validator"
)

type masterStatusesController struct {
	masterStatusesService service.MasterStatusesService
}

type MasterStatusesController 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 NewMasterStatusesController(masterStatusesService service.MasterStatusesService) MasterStatusesController {
	return &masterStatusesController{
		masterStatusesService: masterStatusesService,
	}
}

// All MasterStatuses
// @Summary      Get all MasterStatuses
// @Description  API for getting all MasterStatuses
// @Tags         Untags
// @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       /master-statuses [get]
func (_i *masterStatusesController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	var req request.MasterStatusesQueryRequest
	req.Pagination = paginate

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

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

// Show MasterStatuses
// @Summary      Get one MasterStatuses
// @Description  API for getting one MasterStatuses
// @Tags         Untags
// @Security     Bearer
// @Param        id path int true "MasterStatuses 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-statuses/{id} [get]
func (_i *masterStatusesController) Show(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

// Save MasterStatuses
// @Summary      Create MasterStatuses
// @Description  API for create MasterStatuses
// @Tags         Untags
// @Security     Bearer
// @Body 	     request.MasterStatusesCreateRequest
// @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-statuses [post]
func (_i *masterStatusesController) Save(c *fiber.Ctx) error {
	req := new(request.MasterStatusesCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

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

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

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

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

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

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

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

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

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