package controller

import (
	"strconv"
	"web-qudo-be/app/module/article_nulis_ai/request"
	"web-qudo-be/app/module/article_nulis_ai/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 articleNulisAIController struct {
	articleNulisAIService service.ArticleNulisAIService
	Log                   zerolog.Logger
}

type ArticleNulisAIController interface {
	All(c *fiber.Ctx) error
	Show(c *fiber.Ctx) error
	Save(c *fiber.Ctx) error
	Update(c *fiber.Ctx) error
	Publish(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
}

func NewArticleNulisAIController(articleNulisAIService service.ArticleNulisAIService, log zerolog.Logger) ArticleNulisAIController {
	return &articleNulisAIController{
		articleNulisAIService: articleNulisAIService,
		Log:                   log,
	}
}

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

	reqContext := request.ArticleNulisAIQueryRequestContext{
		NulisAiId:       c.Query("nulisAiId"),
		ArticleId:       c.Query("articleId"),
		Title:           c.Query("title"),
		Description:     c.Query("description"),
		HtmlDescription: c.Query("htmlDescription"),
		CategoryId:      c.Query("categoryId"),
		CreatorId:       c.Query("creatorId"),
		Tags:            c.Query("tags"),
		ThumbnailPath:   c.Query("thumbnailPath"),
		IsPublish:       c.Query("isPublish"),
		PublishedAt:     c.Query("publishedAt"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

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

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

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

// Save create ArticleNulisAI
// @Summary      Create ArticleNulisAI
// @Description  API for create ArticleNulisAI
// @Tags         ArticleNulisAI
// @Security     Bearer
// @Param        X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @Param 	     payload body request.ArticleNulisAICreateRequest 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-nulis-ai [post]
func (_i *articleNulisAIController) Save(c *fiber.Ctx) error {
	req := new(request.ArticleNulisAICreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

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

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

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

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

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

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

// Publish publish ArticleNulisAI
// @Summary      publish ArticleNulisAI
// @Description  API for publish ArticleNulisAI
// @Tags         ArticleNulisAI
// @Security     Bearer
// @Param        X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @Param 	     payload body request.ArticleNulisAIUpdateRequest 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-nulis-ai/publish [post]
func (_i *articleNulisAIController) Publish(c *fiber.Ctx) error {
	req := new(request.ArticleNulisAICreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

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

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

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

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

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