package controller

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

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

type ppidDataCategoriesController struct {
	ppidDataCategoriesService service.PpidDataCategoriesService
}

type PpidDataCategoriesController 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 NewPpidDataCategoriesController(ppidDataCategoriesService service.PpidDataCategoriesService) PpidDataCategoriesController {
	return &ppidDataCategoriesController{
		ppidDataCategoriesService: ppidDataCategoriesService,
	}
}

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

	reqContext := request.PpidDataCategoriesQueryRequestContext{
		Title:       c.Query("title"),
		Description: c.Query("description"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

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

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

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

// Save PpidDataCategories
// @Summary      Create PpidDataCategories
// @Description  API for create PpidDataCategories
// @Tags         PPID Categories
// @Security     Bearer
// @Param 	     payload body request.PpidDataCategoriesCreateRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /ppid-data-categories [post]
func (_i *ppidDataCategoriesController) Save(c *fiber.Ctx) error {
	req := new(request.PpidDataCategoriesCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	err := _i.ppidDataCategoriesService.Save(*req)
	if err != nil {
		return err
	}

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

// SaveThumbnail PpidDataCategories
// @Summary      Upload PpidDataCategories Thumbnail
// @Description  API for Upload PpidDataCategories Thumbnail
// @Tags         PPID Categories
// @Security     Bearer
// @Produce      json
// @Param        files formData file true "Upload thumbnail"
// @Param        id path int true "Ppid Data Category ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /ppid-data-categories/thumbnail/{id} [post]
func (_i *ppidDataCategoriesController) SaveThumbnail(c *fiber.Ctx) error {
	err := _i.ppidDataCategoriesService.SaveThumbnail(c)
	if err != nil {
		return err
	}

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

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

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

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

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

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

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

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

// Viewer PpidDataCategories
// @Summary      Viewer PpidDataCategories
// @Description  API for View Thumbnail of PpidDataCategories
// @Tags         PPID Categories
// @Security     Bearer
// @Param        id path string true "PPID Categories ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /ppid-data-categories/thumbnail/viewer/{id} [get]
func (_i *ppidDataCategoriesController) Viewer(c *fiber.Ctx) error {
	return _i.ppidDataCategoriesService.Viewer(c)
}
