package controller

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

	requestPpidApproval "go-humas-be/app/module/ppid_data_approval_histories/request"
	utilRes "go-humas-be/utils/response"
	utilVal "go-humas-be/utils/validator"
)

type ppidDatasController struct {
	ppidDatasService service.PpidDatasService
}

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

func NewPpidDatasController(ppidDatasService service.PpidDatasService) PpidDatasController {
	return &ppidDatasController{
		ppidDatasService: ppidDatasService,
	}
}

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

	reqContext := request.PpidDatasQueryRequestContext{
		Title:       c.Query("title"),
		Description: c.Query("description"),
		CategoryId:  c.Query("categoryId"),
		CreatedById: c.Query("createdById"),
		StatusId:    c.Query("statusId"),
		IsPublish:   c.Query("isPublish"),
	}

	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

// Show PpidDatas
// @Summary      Get one PpidDatas
// @Description  API for getting one PpidDatas
// @Tags         PPID Data
// @Security     Bearer
// @Param        id path string true "PpidDatas [ ID / Slug ]"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /ppid-datas/{id} [get]
func (_i *ppidDatasController) Show(c *fiber.Ctx) error {
	id := c.Params("id")
	ppidDatasData, err := _i.ppidDatasService.Show(id)
	if err != nil {
		return err
	}

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

// Save PpidDatas
// @Summary      Create PpidDatas
// @Description  API for create PpidDatas
// @Tags         PPID Data
// @Security     Bearer
// @Param        Authorization header string true "Insert your access token" default(Bearer <Add access token here>)
// @Param 	     payload body request.PpidDatasCreateRequest 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-datas [post]
func (_i *ppidDatasController) Save(c *fiber.Ctx) error {
	req := new(request.PpidDatasCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

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

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

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

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

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

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

// UpdateApprovalStatus PpidDatas
// @Summary      UpdateApprovalStatus PpidDatas
// @Description  API for UpdateApprovalStatus PpidDatas
// @Tags         PPID Data
// @Security     Bearer
// @Param 	     payload body requestPpidApproval.PpidDataApprovalHistoriesCreateRequest 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-datas/approval [put]
func (_i *ppidDatasController) UpdateApprovalStatus(c *fiber.Ctx) error {
	req := new(requestPpidApproval.PpidDataApprovalHistoriesCreateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

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

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

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

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

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