package controller

import (
	"fmt"
	"jaecoo-be/app/module/article_files/request"
	"jaecoo-be/app/module/article_files/service"
	"jaecoo-be/utils/paginator"
	utilRes "jaecoo-be/utils/response"
	utilVal "jaecoo-be/utils/validator"
	"strconv"

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

type articleFilesController struct {
	articleFilesService service.ArticleFilesService
}

type ArticleFilesController 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
	Viewer(c *fiber.Ctx) error
	GetUploadStatus(c *fiber.Ctx) error
}

func NewArticleFilesController(articleFilesService service.ArticleFilesService) ArticleFilesController {
	return &articleFilesController{
		articleFilesService: articleFilesService,
	}
}

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

	reqContext := request.ArticleFilesQueryRequestContext{
		ArticleId: c.Query("articleId"),
		FileName:  c.Query("fileName"),
		StatusId:  c.Query("statusId"),
		IsPublish: c.Query("isPublish"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

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

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

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

// Save ArticleFiles
// @Summary      Upload ArticleFiles
// @Description  API for create ArticleFiles
// @Tags         Article Files
// @Security     Bearer
// @Produce      json
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param        files formData file true "Upload file" multiple true
// @Param        articleId path int true "Article ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-files/{articleId} [post]
func (_i *articleFilesController) Save(c *fiber.Ctx) error {
	// Get from context
	id, err := strconv.ParseUint(c.Params("articleId"), 10, 0)
	if err != nil {
		return err
	}

	err = _i.articleFilesService.Save(c, uint(id))
	if err != nil {
		return err
	}

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

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

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

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

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

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

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

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

// Viewer ArticleFiles
// @Summary      Viewer ArticleFiles
// @Description  API for Viewer ArticleFiles
// @Tags         Article Files
// @Security     Bearer
// @Param        filename path string true "Article File Name"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-files/viewer/{filename} [get]
func (_i *articleFilesController) Viewer(c *fiber.Ctx) error {
	// Get from context
	return _i.articleFilesService.Viewer(c)
}

// GetUploadStatus ArticleFiles
// @Summary      GetUploadStatus ArticleFiles
// @Description  API for GetUploadStatus ArticleFiles
// @Tags         Article Files
// @Security     Bearer
// @Param        uploadId path string true "Upload ID of ArticleFiles"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-files/upload-status/{uploadId} [get]
func (_i *articleFilesController) GetUploadStatus(c *fiber.Ctx) error {
	progress, _ := _i.articleFilesService.GetUploadStatus(c)
	progressMessage := fmt.Sprintf("Upload Progress: %d%%", progress)

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Upload Status Retrieve"},
		Data:     progressMessage,
	})
}
