package controller

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

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

type magazinesController struct {
	magazinesService service.MagazinesService
}

type MagazinesController 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 NewMagazinesController(magazinesService service.MagazinesService) MagazinesController {
	return &magazinesController{
		magazinesService: magazinesService,
	}
}

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

	var req request.MagazinesQueryRequest
	req.Pagination = paginate

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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