package controller

import (
	"strconv"
	"web-qudo-be/app/middleware"
	"web-qudo-be/app/module/article_categories/request"
	"web-qudo-be/app/module/article_categories/service"
	"web-qudo-be/utils/paginator"

	"github.com/gofiber/fiber/v2"

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

type articleCategoriesController struct {
	articleCategoriesService service.ArticleCategoriesService
}

type ArticleCategoriesController interface {
	All(c *fiber.Ctx) error
	Show(c *fiber.Ctx) error
	ShowByOldId(c *fiber.Ctx) error
	ShowBySlug(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        X-Client-Key header string false "Insert the X-Client-Key"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @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
	}

	authToken := c.Get("Authorization")
	clientId := middleware.GetClientID(c)

	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(clientId, req, authToken)
	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        X-Client-Key header string false "Insert the X-Client-Key"
// @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
	}

	clientId := middleware.GetClientID(c)

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

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

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

	clientId := middleware.GetClientID(c)

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

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

// ShowBySlug ArticleCategories
// @Summary      Get one ArticleCategories
// @Description  API for getting one ArticleCategories
// @Tags         Article Categories
// @Security     Bearer
// @Param        X-Client-Key header string false "Insert the X-Client-Key"
// @Param        slug path string true "ArticleCategories Slug"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-categories/slug/{slug} [get]
func (_i *articleCategoriesController) ShowBySlug(c *fiber.Ctx) error {
	slug := c.Params("slug")
	clientId := middleware.GetClientID(c)

	articleCategoriesData, err := _i.articleCategoriesService.ShowBySlug(clientId, slug)
	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        X-Client-Key header string false "Insert the X-Client-Key"
// @Param        X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @Param        Authorization header string false "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")
	clientId := middleware.GetClientID(c)

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

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

// SaveThumbnail ArticleCategories
// @Summary      Upload ArticleCategories Thumbnail
// @Description  API for Upload ArticleCategories Thumbnail
// @Tags         Article Categories
// @Security     Bearer
// @Produce      json
// @Param        X-Client-Key header string false "Insert the X-Client-Key"
// @Param        X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @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 {
	clientId := middleware.GetClientID(c)
	err := _i.articleCategoriesService.SaveThumbnail(clientId, 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        X-Client-Key header string false "Insert the X-Client-Key"
// @Param        X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @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
	}

	clientId := middleware.GetClientID(c)
	err = _i.articleCategoriesService.Update(clientId, 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        X-Client-Key header string false "Insert the X-Client-Key"
// @Param        X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @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
	}

	clientId := middleware.GetClientID(c)
	err = _i.articleCategoriesService.Delete(clientId, 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        X-Client-Key header string false "Insert the X-Client-Key"
// @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)
}
