package controller

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

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

type ppidDataFilesController struct {
	ppidDataFilesService service.PpidDataFilesService
}

type PpidDataFilesController 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
}

func NewPpidDataFilesController(ppidDataFilesService service.PpidDataFilesService) PpidDataFilesController {
	return &ppidDataFilesController{
		ppidDataFilesService: ppidDataFilesService,
	}
}

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

	reqContext := request.PpidDataFilesQueryRequestContext{
		Title:       c.Query("title"),
		PpidDataId:  c.Query("ppidDataId"),
		Description: c.Query("description"),
		Unit:        c.Query("unit"),
		FileType:    c.Query("fileType"),
		StatusId:    c.Query("statusId"),
		IsPublish:   c.Query("isPublish"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

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

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

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

// Save PpidDataFiles
// @Summary      Create PpidDataFiles
// @Description  API for create PpidDataFiles
// @Tags         PPID Files
// @Security     Bearer
// @Produce      json
// @Param        files formData file true "Upload file"
// @Param        ppidDataId path int true "Ppid Data ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /ppid-data-files/{ppidDataId} [post]
func (_i *ppidDataFilesController) Save(c *fiber.Ctx) error {
	err := _i.ppidDataFilesService.Save(c)
	if err != nil {
		return err
	}

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

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

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

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

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

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

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

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

// Viewer PpidDataFiles
// @Summary      Create PpidDataFiles
// @Description  API for create PpidDataFiles
// @Tags         PPID Files
// @Security     Bearer
// @Param        filename path string true "Ppid Data Filename"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /ppid-data-files/viewer/{filename} [get]
func (_i *ppidDataFilesController) Viewer(c *fiber.Ctx) error {
	filename := c.Params("filename")
	return _i.ppidDataFilesService.Viewer(c, filename)
}
