package controller

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

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

type articleCategoriesController struct {
	articleCategoriesService service.ArticleCategoriesService
}

type ArticleCategoriesController interface {
	All(c *fiber.Ctx) error
	Show(c *fiber.Ctx) error
	Save(c *fiber.Ctx) error
	SaveThumbnail(c *fiber.Ctx) error
	Update(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
	Viewer(c *fiber.Ctx) error
}

func NewArticleCategoriesController(articleCategoriesService service.ArticleCategoriesService) ArticleCategoriesController {
	return &articleCategoriesController{
		articleCategoriesService: articleCategoriesService,
	}
}

// All ArticleCategories
// @Summary      Get all ArticleCategories
// @Description  API for getting all ArticleCategories
// @Tags         Article Categories
// @Security     Bearer
// @Param 	     req query request.ArticleCategoriesQueryRequest false "query parameters"
// @Param 	     req query paginator.Pagination false "pagination parameters"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-categories [get]
func (_i *articleCategoriesController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.ArticleCategoriesQueryRequestContext{
		Title:       c.Query("title"),
		Description: c.Query("description"),
		ParentId:    c.Query("parentId"),
		IsPublish:   c.Query("isPublish"),
		StatusId:    c.Query("statusId"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"ArticleCategories list successfully retrieved"},
		Data:     articleCategoriesData,
		Meta:     paging,
	})
}

// Show ArticleCategories
// @Summary      Get one ArticleCategories
// @Description  API for getting one ArticleCategories
// @Tags         Article Categories
// @Security     Bearer
// @Param        id path int true "ArticleCategories ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-categories/{id} [get]
func (_i *articleCategoriesController) Show(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"ArticleCategories successfully retrieved"},
		Data:     articleCategoriesData,
	})
}

// Save ArticleCategories
// @Summary      Create ArticleCategories
// @Description  API for create ArticleCategories
// @Tags         Article Categories
// @Security     Bearer
// @Param        Authorization header string true "Insert your access token" default(Bearer <Add access token here>)
// @Param 	     payload body request.ArticleCategoriesCreateRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-categories [post]
func (_i *articleCategoriesController) Save(c *fiber.Ctx) error {
	req := new(request.ArticleCategoriesCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	authToken := c.Get("Authorization")
	dataResult, err := _i.articleCategoriesService.Save(*req, authToken)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"ArticleCategories successfully created"},
		Data:     dataResult,
	})
}

// SaveThumbnail PpidDataCategories
// @Summary      Upload ArticleCategories Thumbnail
// @Description  API for Upload ArticleCategories Thumbnail
// @Tags         Article Categories
// @Security     Bearer
// @Produce      json
// @Param        files formData file true "Upload thumbnail"
// @Param        id path int true "ArticleCategories ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-categories/thumbnail/{id} [post]
func (_i *articleCategoriesController) SaveThumbnail(c *fiber.Ctx) error {
	err := _i.articleCategoriesService.SaveThumbnail(c)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Thumbnail of ArticleCategories successfully created"},
	})
}

// Update ArticleCategories
// @Summary      update ArticleCategories
// @Description  API for update ArticleCategories
// @Tags         Article Categories
// @Security     Bearer
// @Param 	     payload body request.ArticleCategoriesUpdateRequest true "Required payload"
// @Param        id path int true "ArticleCategories ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-categories/{id} [put]
func (_i *articleCategoriesController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

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

// Delete ArticleCategories
// @Summary      delete ArticleCategories
// @Description  API for delete ArticleCategories
// @Tags         Article Categories
// @Security     Bearer
// @Param        id path int true "ArticleCategories ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-categories/{id} [delete]
func (_i *articleCategoriesController) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

// Viewer ArticleCategories
// @Summary      Viewer ArticleCategories
// @Description  API for View Thumbnail of ArticleCategories
// @Tags         Article Categories
// @Security     Bearer
// @Param        id path string true "ArticleCategories ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-categories/thumbnail/viewer/{id} [get]
func (_i *articleCategoriesController) Viewer(c *fiber.Ctx) error {
	return _i.articleCategoriesService.Viewer(c)
}
