package controller

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

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

type articleCategoryDetailsController struct {
	articleCategoryDetailsService service.ArticleCategoryDetailsService
}

type ArticleCategoryDetailsController 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 NewArticleCategoryDetailsController(articleCategoryDetailsService service.ArticleCategoryDetailsService) ArticleCategoryDetailsController {
	return &articleCategoryDetailsController{
		articleCategoryDetailsService: articleCategoryDetailsService,
	}
}

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

	var req request.ArticleCategoryDetailsQueryRequest
	req.Pagination = paginate

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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