package controller

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

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

type articlesController struct {
	articlesService service.ArticlesService
}

type ArticlesController 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 NewArticlesController(articlesService service.ArticlesService) ArticlesController {
	return &articlesController{
		articlesService: articlesService,
	}
}

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

	reqContext := request.ArticlesQueryRequestContext{
		Title:       c.Query("title"),
		Description: c.Query("description"),
		Tags:        c.Query("tags"),
		CategoryId:  c.Query("categoryId"),
		TypeId:      c.Query("typeId"),
		StatusId:    c.Query("statusId"),
		IsPublish:   c.Query("isPublish"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// Viewer Articles
// @Summary      Viewer Articles Thumbnail
// @Description  API for View Thumbnail of Article
// @Tags         Articles
// @Security     Bearer
// @Param        thumbnailName path string true "Articles Thumbnail Name"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /articles/thumbnail/viewer/{thumbnailName} [get]
func (_i *articlesController) Viewer(c *fiber.Ctx) error {
	return _i.articlesService.Viewer(c)
}
