package controller

import (
	"strconv"
	"web-qudo-be/app/middleware"
	"web-qudo-be/app/module/article_comments/request"
	"web-qudo-be/app/module/article_comments/service"
	"web-qudo-be/utils/paginator"
	utilRes "web-qudo-be/utils/response"
	utilVal "web-qudo-be/utils/validator"

	"github.com/gofiber/fiber/v2"
	"github.com/rs/zerolog"
)

type articleCommentsController struct {
	articleCommentsService service.ArticleCommentsService
	Log                    zerolog.Logger
}

type ArticleCommentsController 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
	Approval(c *fiber.Ctx) error
}

func NewArticleCommentsController(articleCommentsService service.ArticleCommentsService, log zerolog.Logger) ArticleCommentsController {
	return &articleCommentsController{
		articleCommentsService: articleCommentsService,
		Log:                    log,
	}
}

// All get all ArticleComments
// @Summary      Get all ArticleComments
// @Description  API for getting all ArticleComments
// @Tags         ArticleComments
// @Security     Bearer
// @Param        X-Client-Key header string false "Insert the X-Client-Key"
// @Param 	     req query request.ArticleCommentsQueryRequest 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-comments [get]
func (_i *articleCommentsController) All(c *fiber.Ctx) error {
	// Get ClientId from context
	clientId := middleware.GetClientID(c)

	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.ArticleCommentsQueryRequestContext{
		Message:     c.Query("message"),
		ArticleId:   c.Query("articleId"),
		CommentFrom: c.Query("commentFrom"),
		ParentId:    c.Query("parentId"),
		IsPublic:    c.Query("isPublic"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

	articleCommentsData, paging, err := _i.articleCommentsService.All(clientId, req)
	if err != nil {
		return err
	}

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

// Show get one ArticleComments
// @Summary      Get one ArticleComments
// @Description  API for getting one ArticleComments
// @Tags         ArticleComments
// @Security     Bearer
// @Param        X-Client-Key header string false "Insert the X-Client-Key"
// @Param        id path int true "ArticleComments ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-comments/{id} [get]
func (_i *articleCommentsController) Show(c *fiber.Ctx) error {
	// Get ClientId from context
	clientId := middleware.GetClientID(c)

	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

// Save create ArticleComments
// @Summary      Create ArticleComments
// @Description  API for create ArticleComments
// @Tags         ArticleComments
// @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.ArticleCommentsCreateRequest 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-comments [post]
func (_i *articleCommentsController) Save(c *fiber.Ctx) error {
	// Get ClientId from context
	clientId := middleware.GetClientID(c)

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

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

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

// Update update ArticleComments
// @Summary      update ArticleComments
// @Description  API for update ArticleComments
// @Tags         ArticleComments
// @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.ArticleCommentsUpdateRequest true "Required payload"
// @Param        id path int true "ArticleComments ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-comments/{id} [put]
func (_i *articleCommentsController) Update(c *fiber.Ctx) error {
	// Get ClientId from context
	clientId := middleware.GetClientID(c)

	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

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

// Delete delete ArticleComments
// @Summary      delete ArticleComments
// @Description  API for delete ArticleComments
// @Tags         ArticleComments
// @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 "ArticleComments ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-comments/{id} [delete]
func (_i *articleCommentsController) Delete(c *fiber.Ctx) error {
	// Get ClientId from context
	clientId := middleware.GetClientID(c)

	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	err = _i.articleCommentsService.Delete(clientId, uint(id))
	if err != nil {
		return err
	}

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

// Approval ArticleComments
// @Summary      Approval ArticleComments
// @Description  API for Approval ArticleComments
// @Tags         ArticleComments
// @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.ArticleCommentsApprovalRequest 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-comments/approval [post]
func (_i *articleCommentsController) Approval(c *fiber.Ctx) error {
	// Get ClientId from context
	clientId := middleware.GetClientID(c)

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

	err := _i.articleCommentsService.Approval(clientId, req.ID, *req)
	if err != nil {
		return err
	}

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