package controller

import (
	"jaecoo-be/app/module/article_approvals/request"
	"jaecoo-be/app/module/article_approvals/service"
	"jaecoo-be/utils/paginator"
	"strconv"

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

	utilRes "jaecoo-be/utils/response"
	utilVal "jaecoo-be/utils/validator"
)

type articleApprovalsController struct {
	articleApprovalsService service.ArticleApprovalsService
	Log                     zerolog.Logger
}

type ArticleApprovalsController 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 NewArticleApprovalsController(articleApprovalsService service.ArticleApprovalsService, log zerolog.Logger) ArticleApprovalsController {
	return &articleApprovalsController{
		articleApprovalsService: articleApprovalsService,
		Log:                     log,
	}
}

// All get all ArticleApprovals
// @Summary      Get all ArticleApprovals
// @Description  API for getting all ArticleApprovals
// @Tags         ArticleApprovals
// @Security     Bearer
// @Param 	     req query request.ArticleApprovalsQueryRequest 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-approvals [get]
func (_i *articleApprovalsController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.ArticleApprovalsQueryRequestContext{
		ArticleId:       c.Query("articleId"),
		ApprovalBy:      c.Query("approvalBy"),
		StatusId:        c.Query("statusId"),
		Message:         c.Query("message"),
		ApprovalAtLevel: c.Query("approvalAtLevel"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

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

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

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

// Save create ArticleApprovals
// @Summary      Create ArticleApprovals
// @Description  API for create ArticleApprovals
// @Tags         ArticleApprovals
// @Security     Bearer
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param 	     payload body request.ArticleApprovalsCreateRequest 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-approvals [post]
func (_i *articleApprovalsController) Save(c *fiber.Ctx) error {
	req := new(request.ArticleApprovalsCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	authToken := c.Get("Authorization")

	// Get from context
	dataResult, err := _i.articleApprovalsService.Save(*req, authToken)
	if err != nil {
		return err
	}

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

// Update update ArticleApprovals
// @Summary      update ArticleApprovals
// @Description  API for update ArticleApprovals
// @Tags         ArticleApprovals
// @Security     Bearer
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param 	     payload body request.ArticleApprovalsUpdateRequest true "Required payload"
// @Param        id path int true "ArticleApprovals ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-approvals/{id} [put]
func (_i *articleApprovalsController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

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

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

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

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

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

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