package controller

import (
	"strconv"

	"github.com/gofiber/fiber/v2"

	"web-qudo-be/app/module/popup_news_contents/request"
	"web-qudo-be/app/module/popup_news_contents/service"
	"web-qudo-be/utils/paginator"

	utilRes "web-qudo-be/utils/response"
	utilVal "web-qudo-be/utils/validator"
)

type popupNewsContentsController struct {
	service service.PopupNewsContentsService
}

type PopupNewsContentsController 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 NewPopupNewsContentsController(s service.PopupNewsContentsService) PopupNewsContentsController {
	return &popupNewsContentsController{
		service: s,
	}
}

// All Popup News Contents
// @Summary      Get all Popup News Contents
// @Description  API for getting all Popup News Contents
// @Tags         Popup News Contents
// @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       /popup-news-contents [get]
func (_i *popupNewsContentsController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	var req request.PopupNewsContentsQueryRequest
	req.Pagination = paginate

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

	return utilRes.Resp(c, utilRes.Response{
		Messages: utilRes.Messages{"Popup news contents list successfully retrieved"},
		Data:     data,
		Meta:     paging,
	})
}

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

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

	return utilRes.Resp(c, utilRes.Response{
		Messages: utilRes.Messages{"Popup news content successfully retrieved"},
		Data:     data,
	})
}

// Save Popup News Content
// @Summary      Create Popup News Content
// @Description  API for create Popup News Content
// @Tags         Popup News Contents
// @Security     Bearer
// @Body 	     request.PopupNewsContentsCreateRequest
// @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       /popup-news-contents [post]
func (_i *popupNewsContentsController) Save(c *fiber.Ctx) error {
	req := new(request.PopupNewsContentsCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

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

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

// Update Popup News Content
// @Summary      Update Popup News Content
// @Description  API for update Popup News Content
// @Tags         Popup News Contents
// @Security     Bearer
// @Body 	     request.PopupNewsContentsUpdateRequest
// @Param        id path int true "Popup News Content 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       /popup-news-contents/{id} [put]
func (_i *popupNewsContentsController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

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

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

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

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