package controller

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

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

type magazineFilesController struct {
	magazineFilesService service.MagazineFilesService
}

type MagazineFilesController 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 NewMagazineFilesController(magazineFilesService service.MagazineFilesService) MagazineFilesController {
	return &magazineFilesController{
		magazineFilesService: magazineFilesService,
	}
}

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

	var req request.MagazineFilesQueryRequest
	req.Pagination = paginate

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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